如何在 JMeter 中读取 XML 文件? [英] How to read XML file in JMeter?

查看:31
本文介绍了如何在 JMeter 中读取 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

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

解决方案

尝试以下操作:

  1. Beanshell Sampler添加到您的测试计划
  2. 将以下代码放入采样器的脚本"区域:

    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);扔前;}

  3. 添加调试采样器查看结果树 监听你的测试计划

  4. 运行测试
  5. 确保 Beanshell Sampler 为绿色并且设置了 ${content} 变量.如果没有 - 查看 jmeter.log 文件并搜索行 Failed to read "test.xml" file.如果此行下方的异常堆栈跟踪没有告诉您任何信息 - 请在此处发布.

参见 如何使用 BeanShell:JMeter 最喜欢的内置组件

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:

  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.

这篇关于如何在 JMeter 中读取 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆