如何在 JMeter 中读取 XML 文件? [英] How to read XML file in JMeter?
问题描述
我试过了:
//${__FileToString(C:\QC\qa\Testlink\Jmeter\Expected\test.xml,ASCII,${xmlFile})};
>
发现错误信息:org.apache.jorphan.util.JMeterException:调用 bsh 方法时出错:eval 在文件中:内联评估:``//<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/At . . . . . . . . . . . . . '' 在第 2 行第 1 列遇到<".
此外,我尝试使用 ${__StringFromFile}
并得到相同的错误消息,甚至使用 beanshell 脚本:
import org.apache.jmeter.services.FileServer;//打开文件FileInputStream fstream = new FileInputStream("C://QC//qa//Testlink//Jmeter//Expected//test.xml");//获取DataInputStream的对象DataInputStream instream = new DataInputStream(fstream);BufferedReader br = new BufferedReader(new InputStreamReader(instream));
尝试以下操作:
- 将Beanshell Sampler添加到您的测试计划
将以下代码放入采样器的脚本"区域:
import org.apache.commons.io.FileUtils;尝试 {String content = FileUtils.readFileToString(new File("C:/QC/qa/Testlink/Jmeter/Expected/test.xml"));vars.put("content", content);} catch (Throwable ex) {log.info("无法读取"test.xml"文件", ex);扔前;}
- 运行测试
- 确保 Beanshell Sampler 为绿色并且设置了
${content}
变量.如果没有 - 查看 jmeter.log 文件并搜索行Failed to read "test.xml" file
.如果此行下方的异常堆栈跟踪没有告诉您任何信息 - 请在此处发布.
参见 如何使用 BeanShell:JMeter 最喜欢的内置组件有关在 JMeter 测试中使用 Beanshell 的更多信息的指南.
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));
Try out the following:
- Add Beanshell Sampler to your Test Plan
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; }
Add Debug Sampler and View Results Tree listener to your Test Plan
- Run the test
- Make sure that Beanshell Sampler is green and
${content}
variable is set. If not - look into jmeter.log file and search for the lineFailed 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.
这篇关于如何在 JMeter 中读取 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!