本文介绍了循环遍历JMeter中的数据并存储要在其他采样器中使用的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JMeter的XML响应中有以下数据:

I have following data in XML response in JMeter:

<details>
<srNo>1</srNo>
<key>123</key>
<Name>Inspector</piName>
<age>89</age>
<country>India</country>
</details>
....................................
...................................
<details>
<srNo>1</srNo>
<key>123</key>
<Name>Inspector</piName>
<age>89</age>
<country>America</country>
</details>

假设从XML文件的响应中我有n个这样的数据.我想读取键"的值.例如1我必须读取"1"并存储在变量中.对于1这样的响应,我正在XPath提取器中读取它并获取正确的值,但是现在我必须遍历它以在变量中获取指定数量的键值.假设我想要1000个这样的键,那么我必须循环直到1000次才能获取变量中的所有值.

suppose I have n number of such data, from the response of the XML file. I want to read value of "key". for eg. 1 I have to read "1" and store in a variable.For 1 such response i am reading it in XPath extractor and getting the correct value, but now I have to loop through it to get specified amount of key value in a variable. Suppose if I want 1000 such keys then I have to loop till 1000 times to get all the value in variables.

在变量中获得该值后,我必须在另一个采样器中使用该值例如:$ {key1}

After getting that value in variables I have to used that value in another Samplerfor eg: ${key1}

推荐答案

尝试使用 Beanshell带有beanshell/java代码的PostProcessor 可从xml响应中提取所有值,并将它们存储在变量中或写入文件.

Try to use Beanshell PostProcessor with beanshell/java code to extract all the values from xml-response and store they in variables or write to file.

  1. 将Beanshell PostProcessor作为子项附加到采样器,该采样器从上面返回xml响应.
  2. 在PostProcessor中使用以下代码(从外部文件或插入脚本"字段)来提取和保存密钥:
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import org.apache.jmeter.samplers.SampleResult;

// set here your xpath expression (to extract EVERY key, not any separate one)
String xpathExpr = "//serviceResponse/details/key/text()";

try {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();

    // access result of parent sampler via "ctx" BeanShell variable
    SampleResult result = ctx.getPreviousResult();
    InputSource is = new InputSource(new StringReader(result.getResponseDataAsString()));
    Document doc = builder.parse(is);

    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathExpr);
    NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);

    // extract all the keys in loop
    for (int i = 0; i < nodes.getLength(); i++) {
        String key = nodes.item(i).getNodeValue();

        System.out.println(key);

        // save extracted key as separate jmeter variables
        String keyName = "key_" + Integer.toString(i);
        vars.put(keyName,key);
    }
} catch (Exception ex) {
    IsSuccess = false;
    log.error(ex.getMessage());
    ex.printStackTrace();
}

您还可以将所有提取的密钥保存到文件中,然后通过 CSV数据集配置进行读取.

You can also save all the extracted keys into file and then read via CSV Data Set Config.

同样,您也可以阅读有关Java XPath API的好文章附带示例,以防万一您在脚本实现上遇到困难.

As well you can look into good article about Java XPath API with examples, in case if you meet any difficulties in script implementation.

希望这会有所帮助.

这篇关于循环遍历JMeter中的数据并存储要在其他采样器中使用的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!