Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
WebElemental is a small library to make writing Selenium tests easier.

WebElemental introduces the Element class, which works with an API modeled on jQuery.
This means that web tests can be written in a way that is familiar to those used to CSS selectors and jQuery.

There are several key motivations for the creation of this library:

  • It is often just as necessary to verify the absence of elements as the presence of them - this is something that is challenging with the org.openqa.selenium.WebDriver interface
  • When using the Page Object pattern being bound to specific data can often make tests quite fragile. This has led to constructs such as Element.any() and Element.first()
  • Selection of elements is most naturally performed using CSS selectors by Web Developers - Selenium places equal weight on Xpath selection, which experience has shown tends to be more fragile.

So, onwards with some examples:
Firstly, to leverage this library, you need to create some simple way of instantiating new Elements. For example, if you are using the Page Object pattern, you may have a base class with access to the org.openqa.selenium.WebDriver. In this class you can add:

 protected Element find(By selector) {
     List<WebElement> findElements = getDriver().findElements(selector);
     return new Element(findElements);
 protected Element find(String selector) {
     return find(By.cssSelector(selector));
Once you have obtained an Element, you can interact with it in the following way:


Element wraps a group of org.openqa.selenium.WebElements, that is, it isn't necessarily a single element on the web page, but is all the elements that matched the selection criteria. What this means is that when you interact with an element, you interact generally with all selected elements. This allows us to do things like this:

 find(".entry").click(); // click everything
 find(".entry").get(1).click(); // click the second item
 find("input").any().text("Updated"); // update the text of one of the inputs, it doesnt matter which

When we get down to it, Selenium tests have limited value without assertions. Experience has shown that the audience for web tests is usually wider and less specifically skilled in java than it would be for unit tests. JUnit tests, particularly with Hamcrest matchers, have a conceptual overhead which can limit the audience. To help remedy this, to support the Element class this library supplies the ElementMatcher class. You can obtain an ElementMatcher for an Element by calling the Element.verify() method.

 find(".entry").verify().isVisible(); //assert that at least one WebElement matched by the '.entry' selector is visible
ElementMatcher methods provide a fluent syntax (or chainable interface) so that many related assertions can be made inline easily.

For those of you comfortable using regular JUnit assertions, ElementMatchers provides static factory methods for creating Hamcrest matchers specific to the Element class.

package com.atomicleopard.webelemental;

New to GrepCode? Check out our FAQ X