在 xpath 提取器中使用 jmeter 变量 [英] Using jmeter variables in xpath extractor

查看:27
本文介绍了在 xpath 提取器中使用 jmeter 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历来自 html 响应的一组表行(例如,每一行包含我想在另一个请求中使用的数据).为此,我设置了一个名为 COUNTER 的变量,并且设置了一个 XPath 提取器,并将 XPath 查询字段设置为

I want to iterate over a set of table rows from a html response (e.g. each row contains data that I want to use in another request). To do this I have set up a variable called COUNTER and I have setup an XPath Extractor with the XPath Query field set to

//table//tr[${COUNTER}]/td[0]

然而,无论 COUNTER 的值如何,这都无法获得结果.如果我用数值替换 ${COUNTER},例如

However this fails to obtain a result irrespective of the value of COUNTER. If I replace ${COUNTER} with a numeric value, e.g.

//table//tr[4]/td[0]

它按预期工作.

以下错误表明此功能应在 2.5.1 中https://issues.apache.org/bugzilla/show_bug.cgi?id=51885 但它在 2.5.1 或 2.6 中对我不起作用

The following bug indicates that this functionality should be in 2.5.1 https://issues.apache.org/bugzilla/show_bug.cgi?id=51885 but it doesn't work for me in 2.5.1 or 2.6

在 XPath 表达式中使用变量在 jmeter 中一定非常有用,但我在网上找不到任何关于如何执行此操作的讨论.我愿意接受其他建议,但正则表达式似乎并不是正确的解决方案.

Using variables in XPath expressions must be very useful in jmeter but I can't find any talk of how to do this on the web. I'm open to alternative suggestions but Regular Expressions doesn't immediately appear to be the right solution.

推荐答案

尝试使用 Beanshell带有 beanshell/java 代码的 PostProcessor 使用 xpath 查询从 xml-response 中提取所有值.

Try to use Beanshell PostProcessor with beanshell/java code to extract all the values from xml-response using xpath query.

  1. 将 Beanshell PostProcessor 作为子项附加到返回您的 html 响应的采样器.
  2. 在后处理器中使用以下代码(从外部文件或插入脚本"字段)提取并保存密钥:

  1. Attach Beanshell PostProcessor as child to the sampler which returns your html response.
  2. Use the following code in PostProcessor (from external file or insert into "Script" field) to extract and save the keys:

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 = "//table//tr/td/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);
    }
} catch (Exception ex) {
    IsSuccess = false;
    log.error(ex.getMessage());
    ex.printStackTrace();
}


使用 xpathExpr = "//table//tr/td/text()" 将为您提供所有行中的所有列.
要获得更具体的选择,您可以:


Using xpathExpr = "//table//tr/td/text()" will give you all the columns in all the rows.
To get more specific selection you can:

  • 优化xpath查询,例如//table//tr/td[1]/text();
  • 提取所有值,然后遍历结果列表以获得您真正需要的值.

希望这会有所帮助.

这篇关于在 xpath 提取器中使用 jmeter 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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