How to read XML file in JMeter?

筅森魡賤 提交于 2019-12-21 20:18:27

问题


I have tried:

//${__FileToString(C:\\QC\\qa\\Testlink\\Jmeter\\Expected\\test.xml,ASCII,${xmlFile})};

Found error message : org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``//<?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/At . . . '' Encountered "<" at line 2, column 1.

also, I tried with ${__StringFromFile} and got the same error message and even with beanshell script that is:

import org.apache.jmeter.services.FileServer;

  //Open the file  
  FileInputStream fstream = new FileInputStream("C://QC//qa//Testlink//Jmeter//Expected//test.xml");  
  //Get the object of DataInputStream  
  DataInputStream instream = new DataInputStream(fstream);  
  BufferedReader br = new BufferedReader(new InputStreamReader(instream));

回答1:


Try out the following:

  1. Add Beanshell Sampler to your Test Plan
  2. Put the following code into the sampler's "Script" area:

    import org.apache.commons.io.FileUtils;
    
    try {
        String content = FileUtils.readFileToString(new File("C:/QC/qa/Testlink/Jmeter/Expected/test.xml"));
        vars.put("content", content);
    
    } catch (Throwable ex) {
        log.info("Failed to read \"test.xml\" file", ex);
        throw ex;
    }
    
  3. Add Debug Sampler and View Results Tree listener to your Test Plan

  4. Run the test
  5. Make sure that Beanshell Sampler is green and ${content} variable is set. If not - look into jmeter.log file and search for the line Failed to read "test.xml" file. If exception stacktrace below this line tells you nothing - post it here.

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Beanshell in your JMeter test.



来源:https://stackoverflow.com/questions/35423876/how-to-read-xml-file-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!