Watir 6.5 Released!

Written by: Titus Fortner on July 25, 2017

Watir 6.5.0 is now available on RubyGems! We’ve added an exciting new locator feature.

To install:

gem install watir

or in your Gemfile:

gem "watir", "~> 6.5"


Locate elements based on presence/absence of multiple classes

The :class locator strategy has always been intended to work for a single class; just like how Selenium works. Because of how Watir was implemented, though, class value matching is done by string comparison with what is in the DOM. This means that the class locator will match on multiple classes, but only if the order is the same as what is in the DOM.

For instance, previously this:

    <div class="a b c">Locate me!</div>

would get located by:

browser.div(class: 'a')
browser.div(class: 'b')
browser.div(class: 'c')
browser.div(class: 'a b')
browser.div(class: 'b c')
browser.div(class: 'a b c')

but would not get located by:

browser.div(class: 'c b a')
browser.div(class: 'c b')
browser.div(class: 'c a')
browser.div(class: 'b a')
browser.div(class: 'a c')

Because this was unintended functionality, String values of the :class locator that include multiple classes will now receive a deprecation warning.

Support for multiple classes will now require using an Array.

# Locate the first element that contains all of the specified classes in any order
browser.element(class: ["c", "a"])

# Locate the first element that does not contain the negated class
browser.element(class: ["!a"])

# Locate the first element that contains all of the specified classes but not the negated class
browser.element(class: ["a", "!c", "b"])

Be careful updating your locator code directly from a String to an Array. Order no longer matters so you might match on an element you don’t expect.


See the Changelog for the complete history of updates.