Object Identification and its management is heart of any test automation. this document explains different ways for object identification in selenium, and also importance of its maintenance.
3. For testing different applications i.e. Application Under Test (AUT), we need to
identify the object it is made of. It is consisting of different objects like below:
Button
Text Box
Link
Drop Down
Radio Button
List Box
Slider
Object identification is very important part of automation activity. So the
automation people should be able to identify the objects for automation purpose.
4. For identifying these objects Selenium uses a concept of ‘Locators’. These locators
are used to identify the objects which are mostly used while automating the test
cases using Selenium.
Locators: There are number of locators present. These are among them.
ID
Name
XPath
CSS
Class
tagName
Link Text
Filters
Finding an element and confirming the expected result requires locators.
5. The 'By' class is used in WebDriver to locate the elements. These are as below:
Locator : ID
Syntax :-
By.id(id);
Locator : Name
Syntax :-
By.name(name);
Locator : Class
Syntax :-
By.class(class);
Locator : tagName
Syntax :-
By.tagName(name);
Locator : Link Text
Syntax :-
By.linkText(linkText);
Locator : CSS
Syntax :-
By.cssSelector(selector);
Locator : XPath
Syntax :-
By.xpath(xpath);
Locator : Filters
Syntax :-
By.partialLinkText(partialLinkTe
xt);
6. Different ways of writing the XPaths
The following are different ways of writing the XPaths:-
Based upon location: A node element can be identified based upon its Absolute or Relative
path.
Absolute path :- it starts from the root element of the html code.
//html/body/div[@id="WebPartWPQ2"]/div[1]/div[2]/div[1]/div[1]/div[5]
Relative path :- it starts from a preceding string of the html code.
//*[@id="lst-ib"]
Based upon attributes
Single
//button[@id='gbqfba']
Multiple
//button[@id='gbqfba'] [@name=‘xyz]
Based upon relative
//div[@id='gbqfbwa']/button“
Based upon position
//div[2]/button
7. XPath Axes
Ancestor
To find nearest ancestor/
parent element that contains
an element that has an
attribute with a certain value
Syntax:-
//*[ancestor::foo[bar[@attr="v
al"]]]
Descendants
To find nearest Descendants/
child element that contains an
element that has an attribute
with a certain value
Syntax:-
//id('books')/descendant::td[@cl
ass='title']
following-sibling
To find all following sibling
element
Syntax:-
//ul/li[following-
sibling::li='roger']
Parent
Each element and attribute
has one parent
Syntax:-
//*[@class='bodywrapper']/pa
rent::*
Child
Child returns all the children in
the current node.
Syntax:-
//*[@class='sphinxsidebarwra
pper']/child::*
preceding-sibling
To find preceding sibling
element
Syntax:-
//ul/li[preceding-
sibling::li='bob' ]
8. XPath Axes
Self
To returns the current node.
Syntax:-
//*[@class='footer']/self::div
Attribute
It returns all the attributes in
the current node
Syntax:-
//*[@class='sphinxsidebarwra
pper']/attribute::*
following
Following returns all in the
document after the closing
tag of the current node.
Syntax:-
//*[@class='clearer']/following
::*
preceding
Preceding is a reverse of
Following; Preceding returns
all in the document before the
current node
Syntax:-
//*[@class='bodywrapper']/pr
eceding::*
Contains()
Contains() is a method used in XPath
expression. It is used when the value of
any attribute changes dynamically
The contain feature has an ability to find
the element with partial text
Syntax:-
//*[contains(@type,'sub')]
//*[contains(@name,'btn')]
//*[contains(text(),'here')]
//*[contains(@href,'guru99.com')]
10. Object management
Test automation predominantly revolves around GUI, objects i.e. various UI controls.
Ability of an automation tool to deal with objects is certainly important when selecting
an automation tool for your Application-Under-Test (AUT). At the same time, how you
look at using it for Object Management is just as important.
Object Management is not mere object identification, but a lot more than that. The
following list will throw some light on what I mean:
Object Identification – Tool algorithm used to identify objects – Name, ID, Link, XPath, etc. that
will work across browsers. Do I need technical skills to identify objects?
Object Repository – Let me know when objects are added by other users to avoid relearning
and improve reusability
Object Maintenance - Let me know when objects are added or removed within AUT for me
understand the impact of object change
11. Object identification
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 WebDriver.
SELENIUM -WEBDRIVER offers a range of locators to point to a specific object
in AUT. To name a few – ID, Name, XPath, CSS, etc.
There are tools such as Firebug 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.
12. Object repository
Object repository is a collection of object and properties with which
automation tool will be able to recognize the objects and act on it.
Having a centralized Object Repository is an absolute necessity for any test
automation initiative to be sustainable.
In a multi-user environment, Object Repository reduces duplication of effort
involved in learning objects if another user has already defined those objects.
Only new or changed objects need to be dealt with. Most automation tools
would provide for some form of Object Repository.
Selenium WebDriver offers no object repository implementation by default.
Page object model help us to overcome the drawback of Selenium
WebDriver.
13. Object Maintenance
A change in application can be introduction of new UI or change in existing UI. In
either of the case one 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 you get unnecessary failures during execution.
An automation tool that can help you understand the impact of any object
change would go a long way in addressing the headaches associated with Object
Management. For e.g.:
Functionality that provides you a list of test cases that will be impacted by an object
change instantly will help faster decision-making and changes.
Ability to update an object once and all test cases using that object are automatically
updated
Most currently available tools provide this information post an execution failure –
basically.
14. 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
Suffice to say that Object Management needs a deeper thought and hence, it
is fair to conclude that Object Management mechanism cannot be taken
lightly when using Selenium WebDriver as your automation technology.