How to execute a web request for x times until it gets an expected response?

Hi,
i want to execute a web request for x times until reponse contains “success”
If the reponse is success then i want to continue my test.

for (int i = 0 ; i < 5 ; i++)
{
//Execute web request for x time until i get success in response
request = findTestObject(Object Repository/mytest/myWebRequest)
response = WS.sendRequest(request)
MyResponse = slurper.parseText(response.getResponseText())[1]
println (MyResponse.stats)
}

if if ((MyResponse.stats) == ‘success’){
println “the response is succes”
}
Here i want to continue with another request.

Is my For loop is correct?
Thank you

When you get a success, use the break statement. The question then is, what are you going to do if you fail to get a success after "x’ times?

ok for the break, id the response is fail i want to excute the request until the response is sucess, maybe i should replace for by while.

Yes, academically, it probably should be a while loop, but it really doesn’t matter. Either way, you have to handle both success and failure. For the failure, you might want to consider issuing a StepFailedException.

Also, inside the loop, you should introduce a delay. At the moment you are hammering the endpoint with exceedingly rapid requests (until it succeeds). Add WebUI.delay(1) or make that a 2 or 3 to give the server some breathing space.

Hi
you can close this thread.
Your advice was helpfull for me.

1 Like