如何在JMeter中对数组对象使用'vars.put'

igetnqfo  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(288)

方案:我的父文件夹是C:\Downloads\RANDOMDICOMIMAGES\在这个我有三个主要文件夹SMALLMEDIUMLARGE。每个这些主要文件夹有子文件夹和每个子文件夹包含图像(可能是一个图像或可能是多个图像)。
目标:我想从SMALLMEDIUMLARGE中选择任意一个随机文件夹,然后从其中选择任意一个文件夹,并计算它包含的图像数量以及带有图像名称的完整路径

脚本:

import groovy.io.FileType

def list = [];
def listNew = [];
def imageFileName = [];
def dir = new File("${__groovy(vars.get("dirName"),)}"); //value of dirName is C:\Downloads\RANDOMDICOMIMAGES\

    //get the directory structure for parent directory
    dir.eachDir() { dirList -> 
        list << dirList
    }

    //get the directory structure for sub parent directory
    Random rnd = new Random()
    def targetDirName = list[rnd.nextInt(list.size())]
    def selectDir = new File("${targetDirName}");
        selectDir.eachDir() { secDirList -> 
            listNew << secDirList

        }

    def targetDirNewName = listNew[rnd.nextInt(listNew.size())]
    def countTheNumberOfImage =  new File("${targetDirNewName}").listFiles().size();
    def totalImageInsideFolder = countTheNumberOfImage.toString(); // Get the total number of files present inside the folder

    //log.info('Total Image -------------->'+totalImageInsideFolder);
    vars.put("noOfDicomImages", totalImageInsideFolder.toString());

    def retrieveFileName = new File("${targetDirNewName}");
    File[] files = retrieveFileName.listFiles(); // Get all the names of the files present in the given directory 

    // Retrieving the list of files present inside the folder
    for (int i=0; i < files.size(); i++) {
        //log.info('File Name-------------->'+files[i].getName().toString());
        def fileNameToAppend = "${targetDirNewName}"+"\\\\"+files[i].getName().toString();
        imageFileName.add(fileNameToAppend.toString())

    }
    //log.info('-------------->'+imageFileName);
    vars.put("ImageFilesExtension", imageFileName);

这在image count中运行良好,但它无法发送包含图像的确切路径的数组对象(请检查下面的错误)。是否有方法可以在vars.put中传递数组对象或如何解决此问题?我在JSR223预处理器中使用此代码

2022-10-12 20:21:51,384 INFO o.a.j.m.J.Count the total number of files: Total Image -------------->1
2022-10-12 20:21:51,386 INFO o.a.j.m.J.Count the total number of files: -------------->[C:\Downloads\RANDOMDICOMIMAGES\SMALL\SPL\5MBFIDDLER.dcm]
2022-10-12 20:21:51,390 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, Count the total number of files
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (String, ArrayList) values: [ImageFilesExtension, [C:\Downloads\RANDOMDICOMIMAGES\SMALL\SPL\5MBFIDDLER.dcm]]
Possible solutions: put(java.lang.String, java.lang.String), get(java.lang.String), putAt(java.lang.String, java.lang.Object), wait(), dump(), any()
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_191]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:224) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:45) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:950) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
7kqas0il

7kqas0il1#

put()函数接受两个字符串作为参数。
如果你想存储其他的东西,可以使用vars.putObject()函数
更多信息:Top 8 JMeter Java Classes You Should Be Using with Groovy

相关问题