Forum Discussion

jsreesoap's avatar
jsreesoap
Contributor
12 years ago

How to stop execution when it gets out of loop

// run ten random requests
for( i in 1..10 )
{
if( Math.random() > 0.5 )
testRunner.runTestStepByName( "Request 1")
else
testRunner.runTestStepByName( "Request 2")
}

// do something else

would run ten random requests before executing the remaining script.
The above I picked it up from Controlling Flow .....
Here it says it would run ten random requests before executing the remaining script. How do I stop executing the remaining script once the loop is done. I tried break but it still runs once after completing the loop.

My Script:
def int j
import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
for (y in 0..2){
if (y==0){testRunner.testCase.testSuite.setPropertyValue("ExmType","A")}
sleep 300
if (y==1){testRunner.testCase.testSuite.setPropertyValue("ExmType","B")}
sleep 300
if (y==2){testRunner.testCase.testSuite.setPropertyValue("ExmType",C")}
sleep 300

j=0
sleep 600
for(x in j..2){

log.info "x"+x
log.info "j" + j
if (x==0){testRunner.testCase.testSuite.setPropertyValue("exeType", "A")
testRunner.testCase.testSuite.setPropertyValue("AccType", "Q")}
sleep 300
if (x==1){testRunner.testCase.testSuite.setPropertyValue("AccType", "S")}
sleep 300
if (x==2){testRunner.testCase.testSuite.setPropertyValue("AccType", D")}


testRunner.runTestStepByName("DataGen")
testRunner.runTestStepByName("ClosingDate groovy")
testRunner.runTestStepByName("Groovy Script")
testRunner.runTestStepByName( "Authheader to Create a Private Placement-test")
def httpResponseHeadersAuth = context.testCase.testSteps["Authheader to Create a Private Placement-test"].testRequest.response.responseHeaders
def httpStatusAuth = httpResponseHeadersAuth["#status#"]
def httpStatusCodeAuth = (httpStatusAuth =~ "[1-5]\\d\\d")[0]
testRunner.runTestStepByName( "PropertyTransfer 1")
testRunner.runTestStepByName( "Create Private Placement-test")
def httpResponseHeaders = context.testCase.testSteps["Create Private Placement-test"].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
log.info("HTTP status code: " + httpStatusCode)
if (y==2 && x==2 && httpStatusCode==201){exit}
}

}

I want it to run only 9 times but after exiting the loop it runs one more time. How do I stop the execution once it gets out of loop?
The test case structure is in screenshot.
Thanks in advance

10 Replies

  • Hi,

    Why don't you run the loop for( i in 1..9) ?
    Otherwise, you can keep track of the iteration over i using a TestCase property and if you have run 9 times, use the "return" command if that condition is met outside the loop so that what comes after the loop will not execute.

    //retrieve value of i from TestCase property (ex: starting from 1)
    //use an if statement to run the statements that were within the for loop
    if( i <= 9)
    {
    ...
    }
    //increment i
    //set value of i as TestCase property

    //test if i = 9 then return

    //rest of script

    Regards,

    Giscard
    SmartBear Support
  • For each iteration I am passing different values and executing a test steps.
    Here is my script:
    for(x in 0..10)
    {
    log.info "x"+x

    if (x==0)
    {
    testRunner.testCase.testSuite.setPropertyValue("exeType", "A")

    }
    if (x==1){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "W")

    }
    if (x==2){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "E")

    }
    if (x==3){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "B")

    }
    if (x==4){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "Q")

    }
    if (x==5){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "X")

    }
    sleep 50
    //from here Execution type is B
    if (x==6){
    testRunner.testCase.testSuite.setPropertyValue("exeType", "B")

    }
    if (x==7){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "W")

    }
    if (x==8){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "E")

    }
    if (x==9){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "B")

    }
    if (x==10){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "Q")

    }
    if (x==11){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "X")
    }

    testRunner.runTestStepByName("DataGen")
    testRunner.runTestStepByName( "Groovy Script")
    testRunner.runTestStepByName("ClosingDate groovy")
    testRunner.runTestStepByName( "Authheader _A_B")
    testRunner.runTestStepByName( "PropertyTransfer 1")
    testRunner.runTestStepByName( "Create P504b_i_A_B")
    }

    when x==11 I want it to execute those test steps and stop.
    I will your suggestion.
    Thanks
  • I am wondering whether I can have an assertion in each iteration here. We have some error codes returned based on the input. I want to use one test case with iterations to pass values (like i am doing in the script shown here) to produces the error code and have an assertion to check in each iteration. So in one test case in each iteration I will be checking for a different error code. Could you pls let me know how I can do it?

    I am using the below to capture the error code/response.
    def responseAsXml = context.expand( '${Method1 - Create a PP-A_A_B#ResponseAsXml#//Fault[1]/e[1]/errorCode[1]}' )

    Thanks in advance
  • In that for loop for(x in 0..10), x will not have value 11. So it seems like the if(x==11) clause won't be executed.
    It seems like you are setting property value 11 times (x = 0 through 10), then 1 more time (11th time) before you run the TestSteps after the loop.
    In that case you can close the loop after if(x==10). Check the suggestion below.


    ...
    if (x==9){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "B")

    }
    if (x==10){
    testRunner.testCase.testSuite.setPropertyValue("AccType", "Q")

    }
    }//end for loop

    //continue script
    testRunner.testCase.testSuite.setPropertyValue("AccType", "X")

    testRunner.runTestStepByName("DataGen")
    testRunner.runTestStepByName( "Groovy Script")
    testRunner.runTestStepByName("ClosingDate groovy")
    testRunner.runTestStepByName( "Authheader _A_B")
    testRunner.runTestStepByName( "PropertyTransfer 1")
    testRunner.runTestStepByName( "Create P504b_i_A_B")

    //end script
  • Hi,

    Is this thread resolved or is their still an issue with the script?



    Regards,
    Marcus
    SmartBear Support
  • Hi,

    Then please reiterate what the current problem is. It's hard to tell from the previous posts. Is the problem with creating assertions or stopping execution? If it is stopping execution did you try the last script posted by SmartBear Support?



    Regards,
    Marcus
    SmartBear Support