scope - 如何在 jmeter 中重用测试片段?

标签 scope include jmeter

我有一个 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 `<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6b7b2bbbfb896b3aeb7bba6bab3f8b5b9bb" rel="noreferrer noopener nofollow">[email protected]</a>` 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 `<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="593837362d313c2b0c2a3c2b193c21383429353c773a3634" rel="noreferrer noopener nofollow">[email protected]</a>` 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

但是,由于用户定义的变量的作用域为线程组,并且似乎并不关心它们出现的顺序,因此第二个赋值会覆盖第一个赋值,即使对于第一个登录请求也是如此。换句话说,两个登录请求都是使用相同的电子邮件和密码(即其他用户的电子邮件和密码)发出的。

如何使该片段可重用且可参数化?

最佳答案

documentation 中所述:

  1. 测试计划中的所有 UDV 元素都会在执行开始时进行处理。
  2. UDV按照它们在测试计划中出现的顺序从上到下进行处理。

因此,如果您添加多个包含具有相同名称的变量的 UDV 元素,则后面的变量将覆盖前面的变量。

如果您想在第二次调用Test Fragment之前更改变量,可以使用这两行代码添加JSR223 Sampler(不要忘记选择Groovy作为一种语言(如果您使用较旧的 JMeter 版本):

vars.put("email","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a5aaabb0aca1b691b7a1b684a1bca5a9b4a8a1eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>")
vars.put("password","superSecurePW")

现在您的变量将具有更新的值。

关于scope - 如何在 jmeter 中重用测试片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42706329/

相关文章:

PHP在一页中多次包含文件

java - 当 Jmeter.bat 中的堆大小增加时无法打开 Jmeter?

javascript - 难以理解 javascript 函数级范围

c++ - 有选择地隐藏类(class)成员

ruby-on-rails - 如何加入具有 Active Record 中范围的 has_many 关联?

ruby-on-rails - rails 事件记录选择

java - 从数组中获取随机列表?

api - Jmeter - POST API 不工作

javascript闭包局部变量超出范围?

jquery - document.ready 中声明的函数未定义?