首页 > 解决方案 > 放心。HTML。Response.prettyPrint() 返回错误数据

问题描述

HTML 示例链接

我发出一个 GET 请求,该请求返回一个带有 HTML 代码示例的响应:

<!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
            <!-- SCROLL TOP BUTTON -->
            <!--===================================================-->
            <button id="scroll-top" class="btn"><i class="fa fa-chevron-up"></i> 
            </button>
            <!--===================================================-->
    </body>
    </html>

如您所见,没有诸如“type='submit'”之类的属性。

1.然后如果你运行下一个代码:

given().body("{\"fileid\":\"FURRL6Q91QTN\"}")
                    .post("https://tryit.w3schools.com/code_datas.php")
                    .prettyPrint();

你会看到,响应正文有 type="submit" 的按钮:

<html lang="en">
  <head/>
  <body>
    <button type="submit" class="btn" id="scroll-top">
      <i class="fa fa-chevron-up"/>
    </button>
  </body>
</html>

2. 如果您尝试像这样验证放心的响应正文:

given().body("{\"fileid\":\"FURRL6Q91QTN\"}")
                .post("https://tryit.w3schools.com/code_datas.php")
                .then()
                .body("**.findAll{it.@id == 'scroll-top' && it.@type =='submit'}.size()", Matchers.is(0));

你会看到断言失败消息:

java.lang.AssertionError: 1 期望失败。

XML 路径 **.findAll{it.@id == 'scroll-top' && it.@type == 'submit'}.size() 不匹配。

预期:是 <0>

实际:1

3. 但是如果之前只使用 asString() 方法打印正文响应:

String responseStr = given().body("{\"fileid\":\"FURRL6Q91QTN\"}")
                .post("https://tryit.w3schools.com/code_datas.php")
                .asString();
System.out.println(responseStr);

代码会很好,并且该按钮上不会有任何类型: 图片

==================================

问题:在1-2个情况下会发生什么?

标签: javahtmlcssrest-assuredpretty-print

解决方案


推荐阅读