Validate HTML with Rest Assured with sample code

This tutorial will guide you through the validation of HTML using Rest Assured. It's not so common, but Rest Assured has support for HTML. We'll show the working example where we get the first page from and validate some elements on the page we got. 

We'll test Amazon search, so first, we'll write search terms in the search box and open Chrome Developer Tools (you have a similar tool on Firefox if you're using it). In Developer Tools, Network tab, you'll see the URL that returns an HTML page. So, we'll try to call this URL, just this time through the Rest Assured code where we'll see how to validate HTML with Rest Assured.


So, url we see there is

As you can see, this url has two params:

  • url with value search-alias=aps (this "=" symbol is encoded as %3D)
  • field-keywords - we decided for our search term to be "phone holder for car"

Validate HTML with Rest Assured

So, we'll use Postman (you can check page source, or locate title in Response tab in Developer tools) for better preview of HTML and we'll locate title tag:

postman amazon get


Test for this situation will be:

package restassured.html.demo;

import static io.restassured.RestAssured.given;
import static io.restassured.path.xml.XmlPath.CompatibilityMode.HTML;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import io.restassured.http.ContentType;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.Response;

public class SearchBoxTest {

	public void searchBoxTest() {
		Response response = given().param("url", "search-alias=aps").param("field-keywords", "phone holder for car")
		assertEquals(response.getStatusCode(), 200);
		XmlPath htmlPath = new XmlPath(HTML, response.getBody().asString());
		assertEquals(htmlPath.getString("html.head.title"), " phone holder for car");

You'll notice that we're converting received HTML into XmlPath because it's the easiest way to access XML nodes within HTML through Java code.

It's pretty straightforward to validate title because we know that the title tag is located under the html->head tag, so we will show another a bit more complicated example. As you can see in the above image from Amazon, the search box on the resulting page contains our search terms. So, we'll use the element inspector to locate the search box and text within it. Right-click and Copy/Copy full XPath. This path locates elements within the HTML file. 

chrome developer tools amazon get call

Path you'll get will be something like this "/html/body/div[1]/header/div/div[1]/div[3]/div/form/div[3]/div[1]/input". Replace "/" with dots since they are node separators accepted by XmlPath. In the end you should have something like this: html.body.div[1].header.div.div[1].div[3].div.form.div[3].div[1].input. Pay attention to indexes of array elements, it's possible you would need to lower it by 1 because indexes in java start from 0.