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

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

问题描述

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

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

在jmeter中,在XPath表达式中使用变量必须非常有用,但是我在网上找不到有关如何执行此操作的话题.我愿意接受其他建议,但正则表达式似乎并不能立即成为正确的解决方案.

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响应中提取所有值.

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. 在PostProcessor中使用以下代码(从外部文件或插入脚本"字段)来提取和保存密钥:

  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天全站免登陆