Forum Discussion

jaypas's avatar
jaypas
New Contributor
15 years ago

For Loop in a Test Case

Hello,
I'm just starting to learn soapUI. I would like to know how to put a loop in a test case. Here's what I want to do:
1. Initialize count to 0
2. Send a transaction request
3. Increment count by 1
4. Delay for 10 seconds
5. Conditional goto: If count == 10, break from the loop. Otherwise, goto step 2.
6. End

How do I initialize count to 0?
How do I increment count by 1?
How do I access count in a conditional goto?
How do I check that count == 10 in the conditional goto?

Thanks,
jaypas
  • Hi,
    This can be achieved using groovy script.
    Go to add step and click on groovy script.
    Now, like any other programming language, write the desired code for 'for' loop.
    It can be like:
    for(i=<initial value>; i<=10; i++)
    {
    if(i<10)
    {
    testRunner.runTestStepByName("<step name 1>")
    }
    else
    {
    testRunner.runTestStepByName("<step name 2>")
    }
    }

    Here, please ensure first that you want to jump to the target test step before completion of the current test step. If you want current step to complete first and then jump to target test step, use

    testRunner.gotoTestStepByName("<step name>")

    instead of

    testRunner.runTestStepByName("<step name>")

    Hope it solves the purpose.

    Regards,
    -
  • jaypas's avatar
    jaypas
    New Contributor
    Hello,
    Thanks for the response regarding using groovy. I was wondering, can I also do this by using a property?
    1. Create a property called count, and initialize count to 0
    2. Send a transaction request
    3. Increment the property called count by 1
    4. Delay for 10 seconds
    5. Conditional goto: Get the value of count. If count == 10, break from the loop. Otherwise, goto step 2.
    6. End

    Thanks,
    jaypas
  • tkumark's avatar
    tkumark
    Occasional Contributor
    Hi Ankita

    Can you also provided where you found these syntaxes. Link where i can look them up

    thanks
    Kumar
  • Hi Kumar,

    Most of the things are there on this forum itself. soapUI website is also helpful.

    Regards,
    Ankita
  • sirba01's avatar
    sirba01
    Occasional Contributor
    Thanks Ankitha.

    I have a question.
    I want to loop the test case based on the response i am going to get, how do i read/load the response in Groovy script? do you have any syntax for this?

    Thanks,
    Balaji
  • You can read values from a previous response like this:

    ${TestStepName#Response#//nodeA[1]/nodeB[1]}

    Using Xpath to select which XML node you want.
  • tkumark's avatar
    tkumark
    Occasional Contributor
    Where/how exactly do i use ${TestStepName#Response#//nodeA[1]/nodeB[1]}.

    I would like to get the response in Groovy Script step.

    Thanks
  • def count = context.expand( '${TestStepName#Response#//nodeA[1]/nodeB[1]}' )


    Lets say your response looks like:

    <serviceResponse>
    <nodeA>
    <nodeB>100</nodeB>
    </nodeA>
    </serviceResonse>


    then in groovy step (of same test case) do:
    def count = context.expand( '${TestStepName#Response#//serviceResponse[1]/nodeA[1]/nodeB[1]}' )


    Now variable 'count' holds a string with value "100"