Jmeter While loop

家住魔仙堡 提交于 2019-12-11 19:55:09

问题


I'd like to perform a while loop using jmeter. Within the loop I'm using xpath extract to pull information from the server response, and storing it in a variable. I'd like to quit the loop if that variable has any data in it (if the request has been successful) - otherwise I'd like to fail if it doesn't respond correctly in x number of attempts. Is this something that JMeter can do?


回答1:


I found a solution for this If you know the response that you are trying to extract from Xpath extractor, With the help of response assertions and while loop its possible..

here is my answer

First of all add a beanshell sampler to the test plan before while loop. In the beanshell sampler add the following 2 lines

vars.put("counter","1"); 
vars.put("txtFound","FALSE")

Next Add a While controller with the following condition

${__javaScript("${txtFound}" == "FALSE" && parseInt(${counter})<=3,)}

Above expression evaluates to true if both conditions are true.here 3 represents the number of attempts.

Now in the while loop add your request . To the same request add a response assertion and add the pattern(the text you are trying to extract using Xpath)

to the same request add a beanshell post processor and copy the following code to it

int counter = Integer.parseInt(vars.get("counter"));
if(counter==3)
vars.put("txtFound","TRUE");
counter++;
vars.put("counter",Integer.toString(counter)); 

in above code 3 represents number of attempts. The code will increment number of attempts by one for each iteration and if it reaches max attempts it sets txtFound to TRUE to stop the test.

Add an if condition below the request as shown below

In if loop add a bean shell sampler and set the txtFound value to TRUE as shown below

When response assertion fails if condition will not be executed and if the response assertion passes if condition is set to true and the elements in If will be executed

The test stops if it finds the correct response ant time or it will stop if it reaches max number of attempts In my case i kept 3 as response assertion so if it finds 3 it will stop or if it reaches max number of 3 attempts

Please follow this link for more information on
while controller



来源:https://stackoverflow.com/questions/50609441/jmeter-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!