/** * @return a DOM document parsed from the String via an SwingEvent2SaxAdapter * and TolerantSaxBuilder. * Not thread-safe! * @see TolerantSaxDocumentBuilder */ public Document parse(String htmlString) throws SAXException, IOException { return parse(new StringReader(htmlString)); }
TolerantSaxDocumentBuilder tolerantSaxDocumentBuilder = new TolerantSaxDocumentBuilder(XMLUnit.newTestParser()); HTMLDocumentBuilder htmlDocumentBuilder = new HTMLDocumentBuilder(tolerantSaxDocumentBuilder); XMLAssert.assertXMLEqual(htmlDocumentBuilder.parse(expectedHTML), htmlDocumentBuilder.parse(actualHTML));
/** * @return a DOM document parsed from the String via an SwingEvent2SaxAdapter * and TolerantSaxBuilder. * Not thread-safe! * @see TolerantSaxDocumentBuilder */ public Document parse(String htmlString) throws SAXException, IOException { return parse(new StringReader(htmlString)); }
public class TestHTML { public static void main(String[] args) throws Exception { String result = "<div class=\"action-button\" title=\"actionButton\"> </div>"; assertHTMLEquals("<div class=\"action-button\" title=\"actionButton\"></div>", result); // ok! // notice it is badly formed String expectedHtml = "<html><title>Page Title</title>" + "<body><h1>Heads<ul>" + "<li id='1'>Some Item<li id='2'>Another item"; String actualMalformedHTML = expectedHtml.replace(" ", " "); // just added some spaces, wont matter assertHTMLEquals(expectedHtml, actualMalformedHTML); // ok! actualMalformedHTML = actualMalformedHTML.replace("Heads", "Tails"); assertHTMLEquals(expectedHtml, actualMalformedHTML); // assertion fails } public static void assertHTMLEquals(String expectedHTML, String actualHTML) throws Exception { TolerantSaxDocumentBuilder tolerantSaxDocumentBuilder = new TolerantSaxDocumentBuilder(XMLUnit.newTestParser()); HTMLDocumentBuilder htmlDocumentBuilder = new HTMLDocumentBuilder(tolerantSaxDocumentBuilder); XMLAssert.assertXMLEqual(htmlDocumentBuilder.parse(expectedHTML), htmlDocumentBuilder.parse(actualHTML)); } }