0

I am receiving the error messages in HTML responses for few API's, which is not validated by Groovy current script.

Requesting some ideas or help to validating the html assertions using groovy since i am new to groovy.

Welcome if you can possible share some good links which explains the same.

Response Data :

<html>
<head>
<title>502 Bad Gateway</title>
</head>
</html>

Trying something like this to capture html responses:

import groovy.json.JsonSlurper;

def failureMessage = "";
def jsonResponse = null;
def xmlResponse = null;
JsonSlurper JSON = new JsonSlurper ();
XmlSlurper XML = new XmlSlurper ();

try {
    xmlResponse = XML.parseText(prev.getResponseDataAsString());
} catch (Exception e) {
    failureMessage += "Invalid XML.\n"
}

if(!"200".equals(prev.getResponseCode())){
    failureMessage += "Expected <response code> [200] but we got instead [" +
        prev.getResponseCode() + "]\n\n" ;
}

if (!xmlResponse.text().contains(["text"] )) {
    failureMessage += "The json response body has wrong structure or error msg.\n\n";
}

Assertion Error :

Assertion failure message: javax.script.ScriptException: 
groovy.lang.MissingMethodException: No signature of method: 
java.lang.String.contains() is applicable for argument types: 
(java.util.ArrayList)
Possible solutions: contains(java.lang.CharSequence), 
contains(java.lang.CharSequence), toString(), toString(), toString(), notify()
cfrick
  • 35,203
  • 6
  • 56
  • 68
KK_3353
  • 95
  • 1
  • 10
  • now there is a simple error in your code. just remove array from this line: `xmlResponse.text().contains(["text"] )`. like this: `xmlResponse.text().contains( "text" )` – daggett Jun 25 '19 at 09:30
  • btw, it's not always possible to parse html with xml parser. in this case you could use `jsoup` library: https://stackoverflow.com/questions/51379483/how-to-parse-xml-comments-in-groovy/51384655#51384655 – daggett Jun 25 '19 at 09:33
  • Oh yes that was the silly mistake and Thanks much for the correction :). Also would like to know is that i can use the jsoup code in JMeter using JSR/BeanShell assertions ? – KK_3353 Jun 25 '19 at 11:04
  • why not. however i would use asserts: `assert xmlResponse.text().contains("text") : "error message here"` – daggett Jun 25 '19 at 11:07

0 Answers0