尝试将Beanshell
PostProcessor与Beanshell
/ java代码一起使用,以从xml响应中提取所有值并将它们存储在变量中或写入文件中。
- 将Beanshell PostProcessor作为子项附加到采样器,该采样器从上面返回xml响应。
- 在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数据集配置读取。
此外,如果遇到脚本实现方面的任何困难,您也可以阅读有关Java XPath
API的优秀文章和示例。
希望这可以帮助。



