我有一个JMeter测试片段,它允许我以特定用户身份登录我的应用程序。它看起来像这样:
- Send a Login http request providing email and password
- Extract a token from the response into a variable I can use later to make authenticated requests
此片段获取电子邮件和密码,以提供来自用户定义变量的http请求。
使用此片段如下所示:
- Set User Defined Variables email and password
- Use the Include Controller to include the login fragment
- Expect the `token` variable to be set
这非常有效,但只有一次......由于电子邮件和密码是作为用户定义的变量提供给片段的,我认为我可以这样做:
- Set email and password to `admin@example.com` and `admin123!`
- Use the Include Controller to include the login fragment (logging in as admin)
- Use `token` to make requests as that admin
- Set email and password to `anotherUser@example.com` and `superSecurePW`
- Use the Include Controller to include the login fragment (logging in as another user)
- Use `token` to make requests as that other user
但是,由于用户定义的变量的范围限定为线程组,并且似乎并不关心它们在事物的顺序中出现的位置,因此第二个赋值会覆盖第一个赋值,即使对于第一个登录请求也是如此。换句话说,两个登录请求都使用相同的电子邮件和密码,而其他用户的登录请求。
如何使这个片段可重用且可参数化?
答案 0 :(得分:1)
如documentation中所述:
因此,如果你添加几个包含相同名称的变量的 UDV 元素,后面的变量将覆盖以前的变量。
如果要在调用测试片段之前更改变量,第二次可以使用这两行代码添加 JSR223 Sampler (不要忘记选择Groovy作为一种语言,如果您使用较旧的JMeter版本):
vars.put("email","anotherUser@example.com")
vars.put("password","superSecurePW")
现在你的变量将有更新的值。
答案 1 :(得分:0)
在你的ThreadGroup中,你应该添加一个< CSV数据集配置'。这将允许您在JMeter可以迭代的外部文件中指定您的输入。完成后,您可以在测试片段中使用这些参数/变量。输入CSV数据集文件中的条目数作为循环次数(在ThreadGroup节点上设置)。
这可以为您提供所需的结果。
答案 2 :(得分:0)
还要尝试Test Fragment
,将不同的值传递给public class Test {
static Deque<Integer> bfs = new ArrayDeque<>();
public static void main(String[] args) {
bfs.add(1);
bfs.add(2);
bfs.add(3);
Iterator<Integer> revbfs = bfs.descendingIterator();
while(revbfs.hasNext()) {
System.out.println(revbfs.next());
}
// output is 3, 2, 1
}
}
元素(可能更多次调用:来自更多控制器,更多线程;仍然只定义一次),以调用相同的块一个可重复使用的代码&#34;具有不同的值:无论这些值是否只是常量(即来自CSV文件),还是从运行时流中生成和进一步处理的值。