如何在jmeter 3.2中的beanshell脚本中循环遍历数组

时间:2017-10-20 06:55:30

标签: jmeter beanshell

是否可以循环遍历数组并使用passwd值从ord[i]中选择一个字符?代码

             String ordinal = vars.get("ordinal");
             String[] ord = ordinal.split(",");
             log.info(ord[0]);

             String passwd = vars.get("password");
             requiredOrd = new ArrayList();
             for(int i=0; i< ord.length; i++)
             {

                    requiredOrd.add((passwd.charAt(ord[i])));

              }

          log.info(requiredOrd[0]); 

返回错误消息,如下所示:

2017-10-23 10:23:26,270错误o.a.j.u.BeanShellInterpreter:调用bsh方法时出错:eval源文件:内联评估:String ordinal = vars.get("ordinal"); String[] ord = ordinal.split(","); log.inf . . . '' : Error in method invocation: Method charAt( java.lang.String ) not found in class'java.lang.String' 2017-10-23 10:23:26,270 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: String ordinal = vars.get(“ordinal”); String [] ord = ordinal.split(“,”); log.inf。 。 。 '':方法调用出错:在类'java.lang.String'中找不到方法charAt(java.lang.String)

3 个答案:

答案 0 :(得分:0)

使用Beanshell,您可以使用String方法charAt,它返回每个索引的字符。

数组中的索引从0开始,因此更改为i = 0passwd.length()调用:

for(int i=0;i<passwd.length(); i++) {
  log.info(passwd.charAt(i))

答案 1 :(得分:0)

因为JMeter中的JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting所以请考虑切换到Groovy语言:

迭代字符串的相关代码如下:

def password = 'foo'
for (char c : password.toCharArray()) {
    log.info('Current char: ' + c)
}

JMeter Groovy Iterate String

答案 2 :(得分:0)

  

id_ALL =“ fbe9ca5c-942b-48d8-9ffd-5f92ae64437a”,   “ 4f90ef0f-c317-47ea-9013-53729477c379”,   “ 98fe94ae-7e1b-42bb-8c2f-cae0f31105a0”,   “ b3f8ed32-e273-41d8-8caf-2024feeccd8c”

StatusMFP_ALL="true","true","false","false"

StatusIwb_ALL= "true", "false", "false", "true"


String[] ids = {${id_ALL}};

String[] StatusM = {${StatusMFP_ALL}};

String[] StatusI = {${StatusIwb_ALL}};

//To get array length

int count = Integer.parseInt(vars.get("id_matchNr"));
log.info( "countest" + vars.get("id_matchNr"));

ArrayList c = new ArrayList();

    for (int i = 0; i <=count-1; i++)
       {
         String M=StatusM[i];
         String I=StatusI[i];
              if(M=="true" || I=="true" )      
                {                  
                    String x=ids[i];                
                    c.add(x);                                                                                                         
                }      
      }            
        for (int i = 0; i < c.size(); i++) 
        {
              log.info(c.get(i));           
        }
        log.info(c.get(0));