Enhanced WebElement

As the name suggests is an Enhanced form of WebElement feature provided by the WebDriver. It gives following advantages.

  • Multiple filtering options on a particular set of WebElement’s
  • Support dynamic element identification in Page Object Factory implementation.
  • Support for Jquery like features. For ex. parent, child, siblings etc.

Creating/Using Enhanced WebElement

There are multiple ways of using the enhanced webelement:

  • Using Page Object Factory
  • Object initialization

Using Page Object Factory

To use TAF enhanced webelement with Page Object Factory initialization just define your elements with type EWebElement instead of WebElement. During the usage of CustomPageFactory class page factory initialization these elements will automatically be initialized. Look at the example below.

public class GoogleLocator{
	
	@FindBy(how=How.CSS,using="#gbqfq")
	public EWebElement searchField;
	
	@FindBy(how = How.CSS,using=".gbqfb")
	public EWebElement submitButton;
	
	@FindBy(how = How.CSS,using="h3.r > a")
	public EWebElement searchResult;
}

EWebElement class by default store all the elements identified by a give locator in its instance, its similar to doing driver.findElements(By.id(“selector”)). In case you need to get a particular element for the enhanced webelement object you can use certain given methods like firstElement, lastElement, nthWebElement, etc. You can also get the enhanced webelement object similar to webelement.

Using Object Initialization

You can create instances of enhanced webelement class object by passing a single or a list of WebElement object to the constructor of EWebElement class Look at the example below:

EWebElement eWebElement1 = new EWebElement(driver.findElement(By.id("selector"));

EWebElement eWebElement2 = new EWebElement(driver.findElements(By.id("selector"));

Filtering with Enhanced WebElement

We all know that while using Page Object Factory we wont be able to identify WebElements with dynamic values as the annotations only accepts static values. Also in case we want to select only a set of elements based on certain criteria we have to write our own logic to do so. To overcome these issues the enhanced webelement comes with a filtering option. This allows users to filter a set of webelements using multiple filter criteria. For ex.

Consider I have a page with following html content:

 
Pharmacy
Small 1
Small 2
Small 3
Small 4
 

If I want to select an element having tag “span” and which starts-with value “name” and have a class value “large”, I can simple do that as follows:

@FindBy(tagName = "span")
EWebElement elements;

EWebElement filteredElements = elements.filter( whereClass(equalsValue("large")), 
													whereName(startsWith("name")));

  

There are lot of options available for Filtering and text-matching for available methods just take a look at the Java documentation of following.

For simplicity all the util methods under Filter and TextMatching class are static methods, so in case you need better readability you can simply do a static import of these classes in your Util classes as mentioned below:


import static org.imaginea.test.automation.framework.dom.filter.Filter.*;
import static org.imaginea.test.automation.framework.dom.filter.TextMatching.*;
 

Jquery Features

While doing automation many a times we need to do different operations on a WebElemnt object like finding siblings, finding a child, moving to the parent etc. Writing code for such operations may be very cumbersome and time consuming. To solve this enhanced webelement contains certain JQuery like methods in it which allows user to easily do webelement related operations like prev, next, parent, child, siblings etc.

To get the list of all the methods available just look at the EWebElement documentation