Thursday, August 6, 2015

Element Identification Strategy using Selenium-WebDriver

One of the fundamental challenges in Test Automation is Element Identification. Almost every tool-QTP, Selenium etc - has its own mechanism in identifying the elements. Here, I would like to focus mainly on Selenium locators.

Selenium employs different methods to identify elements - ID,Name, CSS, XPath, LinkText, PartialLinkText etc. Though all these methods are equally important while designing the Automation framework, it is necessary for the test-developer to know what to use and what order to go for locating elements. The reason is that some locators may work faster than others but may not be accurate. Below is table showing the brief information about each locator based on my experience.

Selenium Locators

Order to be followed in choosing the locators:
ID -> Name -> LinkText -> Partial LinkText -> CSS Locator -> XPATH Locator

Always try to use ID or Name as your first choice. If not available, see if the development team can help you in assigning the ID/Name attributes. LinkText and PartialLinkText are for Links. If the above are not working, then go for CSS locator. Since this works fine with IE, the tests will be multi-browser compatible. If none of the above and other mechanisms work, Xpath can be used as a last option.
Both Xpath and CSS more or less use similar technique(DOM) to identify the elements, CSS is preferred to Xpath as CSS does not pose issues with IE where Xpath might do sometimes.

***Having said the above, Automation developers have to acknowledge the fact that CSS and Xpath are most likely going to fail the moment HTML-DOM structure of the page-elements change.

No comments:

How J2EE components work together in any Container - Spring or Application Server

In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for B...