質問

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?

役に立ちましたか?

解決

You could use jsoup or JTidy instead of XML parsing and use your second strategy.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top