jsoup-hamcrest brings the power of css matchers to your tests.
<dependencies>
<dependency>
<groupId>net.oneandone.jsoup</groupId>
<artifactId>jsoup-hamcrest</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
You can check the latest version here.
jsoup-hamcrest uses a fluent api design to create your assertions, while still offering the comparability of Hamcrest matchers.
import static net.oneandone.jsoup.hamcrest.fluent.DocumentAssertions.anElement; // (1)
import static net.oneandone.jsoup.hamcrest.fluent.JsoupAssertions.html; // (1)
html(source) // (2)
.expect(anElement("#exampleInputEmail") // (3)
.hasAttribute("placeholder") // (4)
.hasAttribute("placeholder", "Email") // (5)
.hasAttribute("placeholder", equalTo("Email"))); // (6)
-
static import of factory methods for better readability (optional)
-
create an instance of
JsoupAssertions
usinghtml
factory method -
create an expectation, you can create multiple expectations for a document by chaining
expect
or its aliasand
.anElement
creates a matcher for a single element with the given css query. (There is alsoeachElement
andelements
for performing assertions on each element or on the collection of found elements respectively) -
hasAttribute
asserts the existence of theplaceholder
attribute on the element -
hasAttribute
asserts that the value of theplaceholder
attribute isEmail
-
hasAttribute
asserts that the value of theplaceholder
attribute isEmail
using a hamcrest matcher, you could also usecontainsString
for example.
For more examples take a look at the AllMatcherHappyTest or FluentExampleTest to see usage examples.