More details are here: matchers.jcabi.com. Also, read this blog post: XML/XPath Matchers for Hamcrest.
First, you add this to your pom.xml
:
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<version>1.8.0</version>
</dependency>
The library contains a collection of convenient Hamcrest matchers:
import com.jcabi.matchers.XhtmlMatchers;
import org.hamcrest.MatcherAssert;
MatcherAssert.assertThat(
"<test><name>Jeff</name></test>",
XhtmlMatchers.hasXPath("/test/name[.='Jeff']")
);
To match XHTML documents you need to specify namespaces in your XPath expressions:
MatcherAssert.assertThat(
"<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'><body>Hello, world!</body></html>",
XhtmlMatchers.hasXPath("/xhtml:html/xhtml:body[.='Hello, world!']")
);
Here, we use xhtml
predefined namespace. There are also
xsl
, xs
, xsi
, and svg
namespaces
provided off-the-shelf. However, you can define your own too, for example:
MatcherAssert.assertThat(
"<foo xmlns='my-own-namespace'><bar/></foo>",
XhtmlMatchers.hasXPath("/ns1:foo/ns1:bar", "my-own-namespace")
);
Here, my-own-namespace
is called ns1
inside the XPath expression.
Fork the repository, make changes, submit a pull request.
We promise to review your changes same day and apply to
the master
branch, if they look correct.
Please run Maven build before submitting a pull request:
mvn clean install -Pqulice