Jmeter While循环

扫码查看
本文介绍了Jmeter While循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jmeter执行while循环.在循环中,我使用xpath提取从服务器响应中提取信息,并将其存储在变量中.如果该变量中包含任何数据(如果请求成功),我想退出循环-否则,如果它在x次尝试中未正确响应,则我想失败. JMeter可以做到这一点吗?

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?

推荐答案

我为此找到了解决方案如果您知道尝试从Xpath提取器提取的响应,借助响应断言和while循环,它是可能的.

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

这是我的答案

首先,在while循环之前将一个beanshell采样器添加到测试计划中.在beanshell采样器中,添加以下2行

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")

下一步添加同时具有以下条件的控制器

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

如果两个条件都成立,则上述表达式的计算结果为true.此处3表示尝试次数.

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

现在在while循环中添加您的请求.向同一请求添加响应断言并添加模式(文本为正在尝试使用Xpath提取)

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)

向同一请求添加一个beanshell后处理器,并将以下代码复制到其中

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));

上面代码3中的

表示尝试次数.该代码将使每次迭代的尝试次数增加一,如果达到最大尝试次数,它将txtFound设置为TRUE来停止测试.

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.

在请求下方添加一个if条件,如下所示

Add an if condition below the request as shown below

在if循环中,添加一个bean shell采样器,并将txtFound值设置为TRUE,如下所示

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

如果条件声明不执行且条件声明为true且If元素将被执行时,如果响应声明失败,则

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

如果找到正确的响应蚂蚁时间,则测试将停止;如果达到最大尝试次数,则测试将停止以我为例,我将3保留为响应断言,因此,如果发现3它将停止或达到3次尝试的最大次数

The test stops if it finds the correct response ant time or it will stop if it reaches max number of attemptsIn 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

请点击此链接以获取有关
的更多信息 while控制器

Please follow this link for more information on
while controller

这篇关于Jmeter While循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:25
查看更多