ses
ses

Reputation: 13362

Does something like assertHTMLEquals exist for unit testing

In my test I check:

assertEquals("<div class=\"action-button\" title=\"actionButton\">"</div>", result);

If someone changes the html (result), putting there SPACE, the HTML still valid, but my test would fail.

Is there some way to compare two html pieces if those are equal as HTML. Like assertHTMLEquals


XML UNIT says that this two lines are equal:

string1:

<ldapConfigurations>
<ldap tenantid="" active="false">
</ldap>
</ldapConfigurations>

string2:

<ldapConfigurations>
<ldapdd tenantid="" active="false">
</ldap>
</ldapConfigurations>

but they are not, as you can see. (see: ldapdd )

Upvotes: 2

Views: 1044

Answers (3)

Cl&#233;ment Picou
Cl&#233;ment Picou

Reputation: 4062

A quick way to do that is to parse the HTML String into a Document with Jsoup and to convert it again into a string :

public static void assertHtmlEquals(String expected, String actual) {
        var expectedDoc = Jsoup.parse(expected);
        var actualDoc = Jsoup.parse(actual);

        assertEquals(expectedDoc.html(), actualDoc.html());
    }

Upvotes: 0

acdcjunior
acdcjunior

Reputation: 135812

You can achieve malformed HTML asserting throught the TolerantSaxDocumentBuilder utility of XMLUnit.

TolerantSaxDocumentBuilder tolerantSaxDocumentBuilder =
                         new TolerantSaxDocumentBuilder(XMLUnit.newTestParser());
HTMLDocumentBuilder htmlDocumentBuilder =
                         new HTMLDocumentBuilder(tolerantSaxDocumentBuilder);
XMLAssert.assertXMLEqual(htmlDocumentBuilder.parse(expectedHTML),
                         htmlDocumentBuilder.parse(actualHTML));

To support badly formed HTML (such as elements without closing tags - unthinkable in XML), you must make use of an additional document builder, the TolerantSaxDocumentBuilder, along with the HTMLDocumentBuilder (this one will allow asserting on web pages). After that, assert the documents as usual.

Working code example:

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));
    }
}

Notice that XML functions, such as XPath, will be available to your HTML document as well.

If using Maven, add this to your pom.xml:

<dependency>
    <groupId>xmlunit</groupId>
    <artifactId>xmlunit</artifactId>
    <version>1.4</version>
</dependency>

Upvotes: 3

Daniel Kaplan
Daniel Kaplan

Reputation: 67474

This won't necessarily work for your case, but if your HTML happens to be valid XML it will. You can use this tool called xmlunit. With it, you can write an assert method that looks like this:

public static void assertXMLEqual(Reader reader, String xml) {
    try {
        XMLAssert.assertXMLEqual(reader, new StringReader(xml));
    } catch (Exception ex) {
        ex.printStackTrace();
        XMLAssert.fail();
    }
}

If that doesn't work for you, maybe there's some other tool out there meant for HTML comparisons. And if that doesn't work, you may want to end up using a library like jtagsoup (or whatever) and comparing if all the fields it parses are equal.

Upvotes: 4

Related Questions