简体   繁体   English

验证 JMeter Groovy 脚本中的 XPath

[英]validate XPath in JMeter Groovy Script

I want the value of meta keywords and it returns null in both the cases我想要元关键字的值,它在这两种情况下都返回 null

  1. When it is really null当它真的是 null
  2. and when XPath doesn't even exist, which is wrong because when XPath does not present it should give an error that there is no such thing as meta keyword on the page.当 XPath 甚至不存在时,这是错误的,因为当 XPath 不存在时,它应该给出一个错误,即页面上没有 meta 关键字之类的东西。

So what I want is to look for the value of meta keyword only if below XPath exists.所以我想要的是仅当存在低于 XPath 时才查找 meta 关键字的值。

//html/head/meta[@name='keywords']/@content

if (keywordXPath.exists()){ // ----> how to do it in Groovy JSR223 Assertion?
   if (keyword.equals("") || keyword == null) {
    // to do 
   } else {
    // to do 
   }
}

How can I check XPath present or not in Groovy JSR223 Assertion?如何在 Groovy JSR223 断言中检查 XPath 是否存在?

It sounds you're going into wrong direction.听起来你走错了方向。

  1. It's possible to evaluate an XPath expression in Groovy - see ie How do I create an XPath function in Groovy question for details It's possible to evaluate an XPath expression in Groovy - see ie How do I create an XPath function in Groovy question for details
  2. Groovy provides its own method to work with XML data - GPath Groovy 提供了自己的方法来处理 XML 数据 - GPath

However these methods assume your response is valid XML or XHTML , any inconsistency - and your code will fail但是,这些方法假定您的响应是有效的 XML 或 XHTML ,任何不一致 - 您的代码将失败

So I would rather use JMeter's XPath2 Extractor to store the "interesting" values from the response into JMeter Variables and then use vars shorthand for JMeterVariables class instance to see whether the variable is defined or not like:因此,我宁愿使用JMeter 的 XPath2 Extractor将响应中的“有趣”值存储到JMeter 变量中,然后对JMeterVariables class 实例使用vars简写来查看变量是否已定义:

if (vars.get('variable from XPath2 Extractor`) != null) {
    //your code here
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM