Our UI designers would like to validate that the HTML they specify should appear on a page does, in fact appear on the page as generated by a .jsp. They need to specify a particular set of HTML nodes with rules like 'this node set appears 1-3 times' 'this node set must appear' 'this node must contain these fixed attributes and these variable attributes'. There would be one testcase per rule, with up to about 100 testcases on one page.

I have automated navigating to the page in question using Selenium in Java. Once on the page, is there a good Java API or automated tool for doing these types of rule-based validations of the DOM?

Another approach I considered is writing up an xpath expression for each expected result. My concern is that they could be very hard to read. Also, I would like to hand off the creation of these rules to the UI developers, and I do't know if they will be able to (easily) create them in xpath.

2 Answers 2


Here's a sketch of the solution in Java:

List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]"));
verifyThat(list.size(), both(greaterThanOrEqualTo(1).and(lessThanOrEqualTo(3)));

where I use:

  • standard WebDriver methods to get a list of matching nodes
  • verifyThat that in contrast with assertThat is a soft assertion: it does not terminate the test after first failure but will report all expressions for which the page failed.
  • Hamcrest matchers that let you write more complex assertions (like 'this node set appears 1-3 times') and provide improved failure reporting.

Note, your UI devs can create XPath expressions with Chrome through Inspect element -> Copy XPath for a selected element. Automatically generated expressions might be hard to read, though, but you would need to run small usability lab with them to understand that.

I don't think using Selenium is the fastest solutions, but you are already working with Selenium and have access to the application and page state with Selenium, so this is the most straightforward solution. If you need faster solution, you would need to work directly on HTTP and HTML level, e.g., using RestAssured framework.


I think that you can use Apache JMeter as a tool and it's Assertions like

do to your checks.

JMeter is a protocol-based tool so it'll be able to deal with your use case much faster than Selenium does, besides you'll be able to run your tests in multithreaded manner, using command-line headless mode, Ant, Maven, Jenkins plugin or whatever.

For more information on how to deal with JMeter Assertions see How to Use JMeter Assertions in 3 Easy Steps guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.