如何在JMeter中读取XML文件?

时间:2021-12-04 13:29:15

I have tried:

我努力了:

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

//${__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.

发现错误消息:org.apache.jorphan.util.JMeterException:调用bsh方法时出错:eval文件:内联评估:``// <?xml version =“1.0”encoding =“UTF-8”?>

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

另外,我尝试使用$ {__ StringFromFile}并得到相同的错误消息,甚至使用beanshell脚本:

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 个解决方案

#1


3  

Try out the following:

试试以下内容:

  1. Add Beanshell Sampler to your Test Plan
  2. 将Beanshell Sampler添加到您的测试计划中
  3. 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;
    }
    
  4. Add Debug Sampler and View Results Tree listener to your Test Plan

    将Debug Sampler和View Results Tree监听器添加到测试计划中

  5. Run the test
  6. 运行测试
  7. 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.
  8. 确保Beanshell Sampler为绿色且设置了$ {content}变量。如果不是 - 查看jmeter.log文件并搜索无法读取“test.xml”文件的行。如果此行下面的异常堆栈跟踪告诉您什么 - 在此处发布。

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

有关在JMeter测试中使用Beanshell的更多信息,请参见如何使用BeanShell:JMeter最喜欢的内置组件指南。

#1


3  

Try out the following:

试试以下内容:

  1. Add Beanshell Sampler to your Test Plan
  2. 将Beanshell Sampler添加到您的测试计划中
  3. 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;
    }
    
  4. Add Debug Sampler and View Results Tree listener to your Test Plan

    将Debug Sampler和View Results Tree监听器添加到测试计划中

  5. Run the test
  6. 运行测试
  7. 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.
  8. 确保Beanshell Sampler为绿色且设置了$ {content}变量。如果不是 - 查看jmeter.log文件并搜索无法读取“test.xml”文件的行。如果此行下面的异常堆栈跟踪告诉您什么 - 在此处发布。

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

有关在JMeter测试中使用Beanshell的更多信息,请参见如何使用BeanShell:JMeter最喜欢的内置组件指南。