简体   繁体   中英

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. 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. This approach works well but not all HTML comes with closing tags and parsing it as XML fails in such cases.

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解析,并使用第二种策略。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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