这是我的场景我正在j meter中的bean shell sampler中运行一个java程序,并在STD OUT控制台中获取所需的字符串。即使我已成功将所需的字符串写入输出文件。我需要从文件或控制台中提取字符串并将其传递给其他样本(所需的字符串与输出中的大量信息一起出现,因此我需要提取字符串,如“必需的字符串:跟随字符”,提前感谢
答案 0 :(得分:0)
至少有两个选项:
答案 1 :(得分:0)
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("path/to/file/output.txt")), true));
说我正在调用一个包含在测试计划中的外部java文件(作为一些jar文件)
在Beanshell Sampler中编写此代码
import userdir.UserClass; UserClass obj1 =new UserClass(); obj1.method1(); System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("path/to/file/output.txt")), true));
现在,在STD OUT中由Userclass产生的任何输出将被定向到ouput.txt
import java.io.FileNotFoundException; import java.util.Scanner; import org.apache.jmeter.samplers.SampleResult; File file = new File("path/to/file/output.txt"); String word = "Req String1"; String word2 = "Req String2"; Scanner scanner = null; String line; String s1; String s2; try { scanner = new Scanner(file); } catch(FileNotFoundException e) { //handle this } //now read the file line by line while (scanner.hasNextLine()) { line = scanner.nextLine(); if(line.contains(word)) { log.info(line); s1=line; //now the entire line along with Req String1 will be stored in s1 } if(line.contains(word2)) { log.info(line); s2=line; } } log.info("The value in Encrypt str ="+s1); log.info("The value in Signatr str ="+s2); String[] s1 = s1.split(":"); String[] s2 =s2.split(":"); //since my value in the line is Req String1:needed data props.put("s1",s1[1]); props.put("s2",s2[1]); log.info("The value in ORIGINAL Req String1 ="+props.get("s1")); log.info("The value in ORIGINAL Req String2 ="+props.get("s2")); scanner.close();
希望这对所有
都有用