简体   繁体   中英

validate XPath in JMeter Groovy Script

I want the value of meta keywords and it returns null in both the cases

  1. When it is really 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.

So what I want is to look for the value of meta keyword only if below XPath exists.

//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?

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
  2. Groovy provides its own method to work with XML data - GPath

However these methods assume your response is valid XML or XHTML , any inconsistency - and your code will fail

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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