This Tech Bite was brought to you by Azra Siručić, Junior Test Engineer at Atlantbh.

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

Locating Elements (Watir) 

Watir (Web Application Testing in Ruby) is an open-source Ruby library for automating web browsers. It interacts with different browsers (Firefox, Chrome, Safari…) the same way people do: clicking buttons, links, populating forms and validating text. Watir allows users to locate elements in many ways which represents one of its most important features. With Watir, the user can locate any element by its text, not just links.

No more XPath! 

Directly using powerful :css & :xpath selectors are supported in Watir, but Watir’s goal is to minimize the need to rely on these selectors, because for example, XPath can be difficult to read.

Standard Watir Locators

ID & Index

browser.div(id: ‘user_id’)

#When combined with other locators, index is the last filter to be applied

browser.div(index: 1)

Name & Class Name

browser.text_field(name: ‘new_user_email’)

#This is for locating with a single class only

browser.text_field(class: ‘name’)

Tag Name

browser.element(tag_name: ‘div’)

#You can use browser.element but it is highly recommended to leverage this locator by using the element’s associated method:

browser.div

Text & Visible Text

#Text evaluates what is in the DOM and Visible Text evaluates what the user can see on the page

browser.button(text: ‘Button’)
browser.button(visible_text: ‘Start’)

 

Leave a Reply