Saturday, 12 April 2014

Object Identification in Selenium




Object Identification:

Object identification is a very crucial part of any automation effort irrespective of the automation tool we may use like QTP, selenium or any other. For every automation engineer, It is very important to identify the objects used in the automation effort effectively.

Object identification, to be more precise, objects being identified accurately demands certain steps to be followed and is certainly not a straight forward experience in Selenium RC or Selenium WebDriver.

Selenium WebDriver offers a range of locators to capture a specific object in AUT, like – ID, Name, Xpath, CSS, etc. Most of the times, user has to make use of Selenium IDE, Firebug or Firepath to identify the objects. There are few limitations with the traditional approach:

§  User wants to verify text without triggering an event on the application (like Click)
§  Selenium-IDE frequently generates index-based Xpath, which is not the right approach as maintainability becomes an issue
§  Selenium -IDE locators work only on single HTML reference at a time. Whereas web technology allows nested HTML structure with frames. If you want your Selenium WebDriver to act on an object which belongs to frame/iframe, you must select the frame first.

There are some tools such as Firebug and Firepath that can provide a much deeper understanding of the objects. The drawback here is that it demands understanding of HTML DOM to help you decide how to deal with objects to derive the best object identification locator.

Identifying Objects is Selenium (WebDriver) is pretty simple if you are working on an application built on Firefox, but it gets very tricky if you are working on IE application. So it would be best to master the object identification techniques.

If you have used the Selenium IDE for any recording you would have noticed that there are multiple ways of identifying an object. By default the Selenium IDE tries to recognize the object by the ID or name attribute. Selenium IDE identifies the objects by the attributes mentioned in the order below:

§  ID
§  Name
§  Link Text
§  CSS Selector
     Based on Combination of HTML tag, id, class, and attributes
     Tag and ID (e.g., css=a#test)
     Tag and Class (e.g., css=a.test)
  • Tag and Attribute (e.g., css=input[name=lastName])
§  Xpath – Attributes
§  Xpath – Relative
§  Xpath – Absolute or Position
§  DOM
     getElementById
     getElementsByName
     dom:name
     dom:index

Example:  When we are identifying a search text box, we can locate the object using different locators as shown below.




When we record a simple step of clicking on the “Google Search” button on the home page. If you can convert the recorded test step to Java – JUnit 4 – WebDriver (Options -> Format -> JUnit 4 – WebDriver). The code for clicking the “Google Search” button using ID attribute would look like this -

driver.findElement(By.id("gbqfb")).click();

for name

driver.findElement(By.name("btnG")).click();

for cssSelector

driver.findElement(By.cssSelector("#gbqfb")).click();

for Xpath Attributes

driver.findElement(By.xpath("//button[@id='gbqfb']")).click();

for Xpath Relative

driver.findElement(By.xpath("//div[@id='gbqfbw']/button")).click();

for Xpath Position / Absolute

driver.findElement(By.xpath("//button")).click();







If you prefer not to use Selenium IDE you can use Firebug or Firepath to identify the objects

Firebug:



Example: Identifying a object locator using Firebug



Firepath:



Example: Identifying a object locator using Firepath


Object Maintenance:

How do we manage objects in the repository efficiently so that we can adapt any change in future in a very efficient manner?

Applications go through constant change with the additional of new screens / objects or changes to the existing. We must know which objects are already present in the repository and which objects in AUT have undergone a change and require update in Object Repository. Many are lost in this phase, ending up with relearning the objects / making changes to object without knowing the impacted test cases. As a result you have to maintain duplicate objects or notice unnecessary failures during execution.

As mentioned earlier, the first step to help with Object Maintenance is Object Identification. Smarter object identification helps ensure that you only have to deal with object maintenance, if an object has truly undergone a change.

An automation tool that can help you understand the impact of any object change, like “the list of test cases impacted by an object change” will save lot of maintenance time. Unfortunately, most of the available automation tools identify the change in application only after a failure has occurred during execution!

Impact of Object Management on Automation ROI

Object management that looks beyond the basics of object identification will have a much larger impact on ROI from a test automation exercise. Incomplete and under-utilized Object Management will have adverse consequences such as:

§     Productivity slows down
§     Maintenance efforts increase
§     Overall cost is increased as against anticipated savings
§     Above all, readiness of automation suite is not guaranteed when required



 By
Automation Mentor

We provide hands-on training on automation tools and frameworks

2 comments:

  1. Most of your contents were really spectacular, I hope you would keep posting more content on software studies.
    selenium Classes in chennai
    selenium course
    selenium Testing Training

    ReplyDelete
  2. Thank you for sharing such valuable information and tips. This can give insights and inspirations for us; very helpful and informative! Would love to see more updates from you in the future.
    Best software testing training institute in chennai
    Software testing courses in chennai

    ReplyDelete