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
#When combined with other locators, index is the last filter to be applied
Name & Class Name
#This is for locating with a single class only
#You can use browser.element but it is highly recommended to leverage this locator by using the element’s associated method:
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’)