使用JMeter中值列表中的请求值

时间:2011-06-14 09:30:18

标签: java testing jmeter web-testing

我确信我过去已经这样做了但不知何故我怎么弄不清楚;-) 所以,这是我的问题:

我正在尝试创建一个JUnit测试计划,其中每次迭代都会通过更改特定参数来修改HTTP请求。因此,例如在五次迭代中,我希望进行以下HTTP请求:

http://localhost:8080/test/foo.html?id=1
http://localhost:8080/test/foo.html?id=2
http://localhost:8080/test/foo.html?id=3
http://localhost:8080/test/foo.html?id=4
...

我想为测试计划全局配置标识符值,并在HTTP请求采样器中使用它们,如下所示:

Path: /test/foo.html?id=${categoryId}

现在的问题:如何全局配置标识符值(我想要使用StringFromFile)以及如何在采样器中引用它们?

4 个答案:

答案 0 :(得分:17)

关于实施${__StringFromArrayAtRandomIndex('3', '2', '54', '42')}

假设您可以使用以下方法轻松实现您的方案: BeanShell Sampler / BeanShell PostProcessor带有一些代码。

E.g:

  • 设置源变量(例如User Defined Variables):

    Name        Value          
    categories  3,2,54,42,37
    

    (即使用逗号作为分隔符,逗号前后不能有空格)。

  • 使用以下代码添加BeanShell Sampler / PostProcessor:

    import java.util.Random;
    
    String[] categories = (vars.get("categories")).split(",");
    
    int idx = new Random().nextInt(categories.length);
    String category = (categories[idx]);
    
    vars.put("categoryId", category);
    
  • 在脚本中进一步引用随机选择的表单列表值${categoryId}

答案 1 :(得分:16)

有几种方法可以做到这一点。

鉴于您有一系列值,您可以:

  • 尝试使用预处理器"User Parameters",创建一个条目 对于每个用户/循环
  • 使用Beanshell 脚本创建数组,选择 随意一个并填充你的 变量

您完全按照列出的方式引用它们:${varName}

答案 2 :(得分:7)

要从列表中获取随机变量值,首先使用前缀和增量索引将列表或可用值声明为用户变量:

country_1     Spain 
country_2     France  
country_3     Portugal  
country_4     Italy 
country_5     England

然后,您可以从列表中获取随机值,并将该前缀与间隔中的随机索引连接起来:

${__V(country_${__Random(1,6,)})}  --> "Spain", "France", "Portugal", etc...

请查看this answer以获取完整说明。

答案 3 :(得分:0)

无变量的最简单解决方案可以使用/test/foo.html?id=${__groovy(vars.getIteration())}

完成
$('.categories').on('click','.item',function(){
      //your code
   if(!$('#todo'+thisId).children().hasClass('todoinput')){
      //your code
   }
}