Checking HTML document with REST Assured

I checked your code. The thing is that XmlPath of Restassured isn't Xpath, but uses a property access syntax. If you add a body content to your sample HTML you will see that your XPath doesn't do much. The actual name of the query language is GPath. The following example works, note also the use of CompatibilityMode.HTML, which has the right config for you need:

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.jayway.restassured.path.xml.XmlPath;
import com.jayway.restassured.path.xml.XmlPath.CompatibilityMode;

public class HtmlDocumentTest {

    @Test
    public void titleShouldBeHelloWorld() {
        XmlPath doc = new XmlPath(
                CompatibilityMode.HTML,
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
                        + "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
                        + "<head><title>Hello world</title></head>"
                        + "<body>some body"
                        + "<div class=\"content\">wrapped</div>"
                        + "<div class=\"content\">wrapped2</div>"
                        + "</body></html>");

        String title = doc.getString("html.head.title");
        String content = doc.getString("html.body.div.find { it.@class == 'content' }");
        String content2 = doc.getString("**.findAll { it.@class == 'content' }[1]");

        assertEquals("Hello world", title);
        assertEquals("wrapped", content);
        assertEquals("wrapped2", content2);
    }
}

If you're using the DSL (given/when/then) then XmlPath with CompatibilityMode.HTML is used automatically if the response content-type header contains a html compatible media type (such as text/html). For example if /index.html contains the following html page:

<html>
    <title>My page</title>
    <body>Something</body>
</html>

then you can validate the title and body like this:

when().
        get("/index.html").
then().
        statusCode(200).
        body("html.title", equalTo("My page"), 
             "html.body",  equalTo("Something"));

Tags:

Rest Assured