首页 > 解决方案 > 在空手道中匹配 html 响应

问题描述

我在将我的响应错误与 html 匹配时遇到问题。

我试过这样

错误:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Error: Unexpected object!</pre>
</body>
</html>

我正在这样做,scnario 将被停止!

When method post
* if (responseStatus == 500 ) karate.abort()
  Then status 200
* match $.errors == '#notpresent'

如何将响应匹配为 html 文本?

标签: karate

解决方案


抱歉,空手道仅适用于格式良好的 XML。您可以尝试替换 HTML 中的内容以进行清理。或者您可以只进行字符串contains匹配等。或者您可以编写一些 JS 或 Java 代码进行自定义检查。

这将起作用(在删除<meta>格式不正确的标签之后。

* def response =
"""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Error</title>
</head>
<body>
<pre>Error: Unexpected object!</pre>
</body>
</html>
"""
* match //pre == 'Error: Unexpected object!'

推荐阅读