FDMediagroep / hamcrest-jsoup

hamcrest matchers for jsoup
GNU General Public License v2.0
7 stars 2 forks source link

hamcrest-jsoup All Contributors

The Hamcrest JSoup library provides a set of matchers for JSoup elements to assert the contents of those objects. With this library you can easily unit test HTML contents in your backend.

The JavaDoc can be found at: http://files2.fd.nl/development/hamcrest-jsoup/javadoc/ or via maven central!

How to use hamcrest-jsoup in your project:

you can also download the latest release from http://files2.fd.nl/development/hamcrest-jsoup/binaries/0.1.7/hamcrest-jsoup-0.1.7.jar

Standard Maven dependency

With a lot of tries we managed to get this into maven central, so you can add a dependency from your project directly! http://search.maven.org/#search%7Cga%7C1%7Chamcrest-jsoup

Example Usage

Here we provide a code sample to give you a rough idea how easy to use this library is.

    String html = "<html><body> <div class=\"content container\"><h1>Some user form</h1> "
                +"<form role=\"form\" method=\"POST\" action=\"/users/save\"> "
                +"<div class=\"form-group\">"
                +"<label for=\"username\">Username</label>"
                +"<input type=\"text\" id=\"username\" name=\"username\" />"
                +"</div>"
                +"<div class=\"form-group\">"
                +"<label for=\"password\">Password</label>"
                +"<input type=\"password\" id=\"password\" name=\"password\" />"
                +"</div>"
                +"</form>"
                +"</div>"
                +"</body>"
                +"</html>";

    Document document = Jsoup.parse(html);

    Elements formElements = document.select("div.content > form");
    assertThat(formElements.get(0), hasAttribute("action", "/users/save"));

Or when using MockMVC:

    MvcResult result = mockMvc.perform(get("/users/new")).andReturn();
    assertThat(result.getResponse().getStatus(), is(200));

    String content = result.getResponse().getContentAsString();
    Document document = Jsoup.parse(content);

    Elements formElements = document.select("div.content > form");
    assertThat(formElements.get(0), hasAttribute("action", "/users/save"));

The result of a mock http get request is parsed into a JSoup document of which a specific element is selected using a css selector. The ElementWithAttrribute matcher is used to check if the form action has value "/users/save".

Matchers

The following matchers are provided:

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Joram Hannema
Joram Hannema

💻 📖 🚧
Jeroen M Snoeij
Jeroen M Snoeij

💻 📖 🚧
Quinten Krijger
Quinten Krijger

💻 📖
Mohammad Naghavi
Mohammad Naghavi

💻

This project follows the all-contributors specification. Contributions of any kind welcome!