我收到了一些API的HTML响应中的错误消息,但未经Groovy当前脚本验证。
由于我是groovy的新手,所以要求一些想法或帮助使用groovy验证html断言。
如果可以的话,欢迎分享一些很好的链接。
响应数据:
<html>
<head>
<title>502 Bad Gateway</title>
</head>
</html>
尝试这样的方法来捕获html响应:
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 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()