Watir 6.15

Written by: Titus Fortner on November 7, 2018

Watir 6.15 is now available on RubyGems. A few new element methods, new contributors, and some minor performance improvements. The equivalent functionality in Python has also been released in Nerodia 0.12.

To install:

gem install watir

or in your Gemfile:

gem "watir", "~> 6.15"

to install Nerodia (Python)

pip install -U nerodia

For more information visit: https://github.com/watir/nerodia/blob/master/README.rst

  • Thanks to Ryan Baumann for his contribution to our project
  • Thanks also to Thomas Walpole from Capybara for helping us improve our Regular Expression matching

New Element Methods

  • Elements#selected_text: This is the reflexive method to Element#select_text.
  • Element#classes: Since Element#class_name returns a String of all of the classes for the given element with white space, it makes more sense to be able to return an Array of class values if there are multiple classes expected.
  • Element#obscured?: The intent here is to evaluate whether Element#click will result in a ElementClickInterceptedError before having to make the click in the first place.

Locator Updates

A major feature of Watir over using Selenium directly is the extended number of locators available. Watir allows you to combine multiple locators to more accurately describe a unique element without needing to resort to writing difficult to read (and likely brittle) XPath or CSS strings. Watir takes a Hash of locators and runs it through a SelectorBuilder implementation. By default this is implementated with XPath. This class will convert the provided Hash into an ugly but accurate XPath locator that the end user thankfully never needs to look at but is exactly what the driver needs to locate the element. Any locators that can not be entirely converted into a single XPath expression (things pertaining to visibility, or values with complicated Regular Expressions), the Locator class will use whatever could be built into a partially matching XPath expression, locate all elements that match it, and then iterate through each of those elements attempting to match the remaining locators.

The new code in this version will locate more elements on the first try without requiring iteration, and does a better job filtering out the number of elements that need to be iterated over when using complex Regexp.

Tips on using Regexp values in locators:

  • Use only word characters: For instance /random-/ should be preferred when possible over more complex ways of matching, like: /random-\d\d\d/
  • “Starts with” can also be entirely captured in one call, so you can do /^random-/ if that addition makes it unique
  • If you need to use more anything more complicated, make it as specific as possible to minimize the number of elements that need to be iterated over to find the exact element you are looking for.

See the Changelog for the complete history of updates.


Thoughts about this article? Let us know!