Presentation on Object Identification presented during Selenium Conference 2016 at Bangalore. The talk was delivered by Dharmesh Vaya (Twitter - @DRVaya).
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Hands-on Guide to Object Identification
1. Hands-on guide to
Object Identification
Speaker : Dharmesh Vaya
Presented On : 24.06.2016
Twitter : @DRVaya
Web : http://drvaya.wordpress.com/
2. Test Automation Consultant with 10+ yrs experience
Expertise with designing/development of Selenium
based Automation frameworks (as well as other tools)
Speaker at various forums - Google DevFests, Selenium
Conference
Trainer/Mentor - to different MNCs/Startups.
Dharmesh Vaya
a.k.a.
Dharam
3. Agenda
★ Basics of Object Identification
★ What are CSS Selectors/Locator’s ?
★ What are Xpath Locators ?
★ Let’s go advanced …
★ Cheat Sheet
★ Best Practices from experience
5. Approach ...
★ More than locators you need to understand the underlying
Web development techniques.
★ Carefully understand the page source and identify the libraries
used. This will help in building robust scripts.
★ Using Firebug/Developer tools.
★ Test your element even before you do it with Selenium.
(Developer toolbars are meant for that).
★ Ultimately, Locators come first, Logic is secondary and will be
built upon the locators.
7. CSS Selectors...
★ Absolute Selectors
World -> Continent -> Country -> State -> City -> Street
★ Relative Selectors
Street Number or House Number
★ Class or ID based Selectors
★ Finding using other attributes
★ Partial Matching
9. Xpath Locators...
★ Absolute Xpath
World -> Continent -> Country -> State -> City -> Street
★ Relative Xpath
Street Number or House Number
★ Index based Xpath
★ Finding using other attributes
★ Partial Matching
11. Complex Selectors
★ Partial Matching
Description CSS Xpath
Starting with ^= starts-with()
Ending with $= ends-with()
Contains *= contains()
12. Few more, Complex Selectors
★ Elements based on relationships
Xpath : ancestor, descendent, preceding, following
CSS: :first-child, :last-child, :nth-child(n)
★ Based on UI state
CSS: enabled, disabled, checked, :not()
14. Handy Reference for CSS/Xpath
Approach Description/HTML CSS Xpath
Finding by ID <input id='user'> input#user //input[@id='user']
Finding by Class <input class='user'> input.user //input[@class='user']
Any attribute <input name='text'> input[name='text'] //input[@name='text']
Direct Child <ul> has several <li> ul > li //ul/li
Any Child/Grandchild <ul> has several <li> ul li //ul//li
nth-child 4th <li> from <ul> ul li:nth-of-type(4) //ul/li[4]
Direct Parent <ul> is parent of <li class='active'> li.active:parent //li[@class='active']/..
Next Sibling 2nd <li> tag from several <li> tags li.active + li //li[@class='active']/../li[2]
By Text Link with Sign In text a:contains('Sign In') //a[contains(text(), 'Sign In')]
16. Make scripts more robust...
★ Avoid fragile selectors like ones having numbers or text that
appears to be variable
★ Don’t rely on a hierarchy of elements above 3-4 levels
★ Avoid using more than 2 attributes for a composite selector
★ Ensure the selectors in all browsers using the console
★ Store Element locators outside your classes … Excel/XML/Text
anywhere but not in your code.
★ At times, you may avoid GUI automation too !!!
17. Happy to answer any questions/queries
related to Automation testing …..
Slides will be available on -
http://drvaya.wordpress.com/