Skip to content

JSoup Hamcrest Matchers brings the power of css matchers to your tests.

License

Notifications You must be signed in to change notification settings

1and1/jsoup-hamcrest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsoup hamcrest
jsoup hamcrest
jsoup hamcrest

JSoup Hamcrest Matchers

jsoup-hamcrest brings the power of css matchers to your tests.

Maven

<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.

Usage

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)
  1. static import of factory methods for better readability (optional)

  2. create an instance of JsoupAssertions using html factory method

  3. create an expectation, you can create multiple expectations for a document by chaining expect or its alias and. anElement creates a matcher for a single element with the given css query. (There is also eachElement and elements for performing assertions on each element or on the collection of found elements respectively)

  4. hasAttribute asserts the existence of the placeholder attribute on the element

  5. hasAttribute asserts that the value of the placeholder attribute is Email

  6. hasAttribute asserts that the value of the placeholder attribute is Email using a hamcrest matcher, you could also use containsString for example.

For more examples take a look at the AllMatcherHappyTest or FluentExampleTest to see usage examples.