栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

循环遍历JMeter中的数据并存储要在其他采样器中使用的数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

循环遍历JMeter中的数据并存储要在其他采样器中使用的数据

尝试将Beanshell
PostProcessor
与Beanshell
/ java代码一起使用,以从xml响应中提取所有值并将它们存储在变量中或写入文件中。

  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数据集配置读取。

此外,如果遇到脚本实现方面的任何困难,您也可以阅读有关Java XPath
API的优秀文章和示例。

希望这可以帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392141.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号