简体   繁体   English

测试生成的HTML代码的规范方法是什么?

[英]What is the canonical way to test generated HTML code?

The two approaches I usually follow are: 我通常遵循的两种方法是:

  1. Convert the HTML to a string, and then test it against a target string. 将HTML转换为字符串,然后针对目标字符串进行测试。 The problem with this approach is that it is too brittle, and there'll be very frequent false negatives due to say, things like extra whitespace somewhere. 这种方法的问题是它太脆了,并且由于诸如某些多余空格之类的东西而经常会出现假阴性。

  2. Convert the HTML to a string and parse it back as an XML, and then use XPath queries to assert on specific nodes. 将HTML转换为字符串并将其解析为XML,然后使用XPath查询在特定节点上进行断言。 This approach works well but not all HTML comes with closing tags and parsing it as XML fails in such cases. 这种方法效果很好,但并非所有HTML都附带结束标记并在XML失败的情况下对其进行解析。

Both these approaches have serious flaws. 这两种方法都有严重的缺陷。 I imagine there must be a well-established approach (or approaches) for this sort of tests. 我认为对于这种测试,必须有一种完善的方法。 What is it? 它是什么?

您可以使用jsoupJTidy代替XML解析,并使用第二种策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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