Jmeter写出文件

时间:2016-03-16 18:53:41

标签: jmeter

我有一个使用JUnit采样器在Jmeter中运行的selenium脚本。我的selenium程序有一些system.pritnln语句,我在Jmeter运行时在控制台中看到它,如何将它们写入文件?

enter image description here

3 个答案:

答案 0 :(得分:1)

我会选择Sample Variables,因此您可以将用户名包含在.jtl结果文件中,添加如下代码:

  1. 在JUnit中:

    JUnitSampler sampler = new JUnitSampler();
    JMeterVariables vars = sampler.getThreadContext().getVariables();
    vars.put("username", your_username_variable);
    vars.put("elapsed", your_total_time_variable);
    sampler.getThreadContext().setVariables(vars);
    
  2. 在JMeter的 user.properties 文件中:

    sample_variables=username,elapsed
    
  3. 如果您想要一个单独的文件 - 只需将所有System.out.println("");替换为:

    FileUtils.writeStringToFile(new File("/path/to/file"),"what you need to write", true);
    

    有关使用JMeter运行JUnit测试的更多信息,请参阅How to Use JUnit With JMeter文章

答案 1 :(得分:0)

如果您不想创建单独的文件并且可以使用日志文件,则下面的语句将写入日志文件

log.info(" TEXT ");

如果要创建单独的文件,则

import org.apache.jmeter.services.FileServer;

f = new FileOutputStream("c:/output/result.txt", true); 
p = new PrintStream(f); 
p.println(" Hello World "); // update here what you want to write
p.close();
f.close();

答案 2 :(得分:0)

1:将JSR223采样器添加到您的测试计划

2:写下面的代码:

FileWriter fstream = new FileWriter("D:\\subid.csv",true); //Create New file with name "subid"

BufferedWriter out = new BufferedWriter(fstream);

out.write(vars.get("variable1"));//write value of variable 1

out.write(",");

out.write(vars.get("variable2"));//write value of variable 2

out.write(System.getProperty("line.separator"));//insert new line

out.close();
fstream.close();
相关问题