Watir 2.0

number 2

Watir 2.0

Recently, Watir 2.0 got released! This post will try to explain all the important changes what this version brought to the automated testing world.

About FireWatir

Old Hand Made Whiskey Bottle

There is no FireWatir 2.0 and there won’t be any new releases of FireWatir anymore due to the fact that JSSH extension, which is used to control Firefox by FireWatir, is not available for Firefox versions 4 and newer. This means that we’ve decided that it’s time for the FireWatir to retire. We’ve released a version 1.9.3 which doesn’t have any new functionality, but has a less strict dependency requirement for common-watir gem so you could use Watir 2.0 and FireWatir 1.9.3 together in your Bundler’s Gemfile if really needed. We recommend you to start using Watir-WebDriver Firefox driver instead if you want to test your application on Firefox.

Zero based indexing

10

The next big change is zero based indexing. As you might know then Watir has used one based indexing so far. For example, if you are using Watir 1.x and you’d like to access first div element on the page, you’d write code like this:

browser.div(:index => 1)

In Watir 2.0 and newer, you have to write code like this instead:

browser.div(:index => 0)

That change is done to be more compatible with Watir-WebDriver and the Ruby (and many other) programming language itself. For example, the first element in the array has an index of 0. That’s it, no more remembering if the indexing started from 0 or 1 in Watir. It’s consistently 0. This also applies to tables, rows and cells:

table = browser.table(:class => "sometable")
# will get the first row
first_row = table[0]
# will get the first cell
first_cell = first_row[0]

You can revert to one-based-indexing to make the integration from Watir 1.x to Watir 2.0 easier and faster for your existing tests by using a special option:

require "watir"
Watir.options[:zero_based_indexing] = false

Remember that this option will be probably removed in the not-too-soon-future, so you’d better edit your tests to start working with zero-based-indexing. I’m pretty sure you want to do that since starting from now Watir will become more and more compatible with Watir-WebDriver.

Multiple locators

Starting from Watir 2.0 ALL elements support multiple locators for searching. Even browser#element. In case you don’t know what this feature is, then here is an example for searching for the span element with a specific name AND class:

# only span with name "something" AND class "else" will be found
browser.span(:name => "something", :class => "else")

You can also search all elements by :xpath and :css locators now, but please remember that these are considerably slower than any other locators.

Locators for collections

When you were dealing with element collections in Watir 1.x then you had to use Ruby’s Enumerable methods for filtering out elements with specific criteria. Let’s say that we’re interested only in the images, which have a name of "logo":

browser.images.find_all {|image| image.name == "logo"}.each do |image|
# only images with the name of "logo" will print out their html
puts image.html
end

In Watir 2.0 you can also specify locators for the collections methods so it’s a lot easier to filter out the elements in interest. The above example in Watir 2.0 would be:

browser.images(:name => "logo").each do |image|
puts image.html
end

Of course you can use multiple locators for the collection methods too!

Default locator

Have you done something like this in your tests:

browser.span(:class => "something").div(:index => 1).span(:class => "else")

Notice the usage of :index => 1 to specify that the span with class "else" should be inside of the first div. In Watir 2.0 you can use the syntax below instead:

browser.span(:class => "something").div.span(:class => "else")

In other words – if no other locators are specified, then :index => 0 will be used as a default. Convenient, eh?

Aliased methods

Have you typed browser.tr instead of browser.row? What about browser.td instead of browser.cell? Since most of the html elements are accessible by the same method name as their tag then it just makes sense to have methods for these elements too. In Watir 2.0 you can use #tr, #trs, #td, #tds, #a, #as, #img and #imgs! Consistency is good!

Conclusion

Watir 2.0 tries to be the best “driver” for IE browser by adding all these missing features and incompatibilities between Watir-WebDriver. Our goal is to minimize that gap even further. Stay tuned for future releases!

About these ads

29 thoughts on “Watir 2.0

  1. Pingback: Watir 2.0 at Željko Filipin's Blog on Software and Testing

  2. It’s great to see that the watir team is still working hard to improve watir with each new release. I applaud your efforts towards improving this wonderful tool.

    As I was reading about the new features of watir 2.0 I was hoping I would come across something new for my two biggest problems, javascript pop-ups and waiting for the browser to load.

    Javascript pop-ups are a big problem for me and my testers that use watir. I would love for a more consistent means of dealing with them to be developed. I’ve read everything out there about watir and pop-ups. Some techniques work some of the time but none work all the time or even consistently.

    As far as waiting for the browser to load, nothing has worked for me and my team. I’ve tried every method I read about here and on the wiki to no avail. The application I use relies heavily on ajax to load page elements and I suspect this might be why I’m having trouble but I know I’m not the only one in this situation.

    Again, I want to reiterate that I LOVE watir and it is an excellent tool! I would hate to have to abandon it over these two issues but if I don’t find solutions to my problems stated above I will have to. Cheers

    • What problems do you have with Javascript pop-ups?

      There are mostly two ways to handle Javascript pop-ups – one way would be to override the relevant Javascript functions (“Simplest way to stop JavaScript Pop Ups from showing when running your Watir tests” at http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups) and another way would be to use RAutomation starting from Watir version 1.9.0. It is something like this:

      # just close the dialog
      js_dialog = browser.javascript_dialog
      Watir::Wait.until {js_dialog.present?}
      js_dialog.close

      # or press some button on it
      browser.javascript_dialog.button(“OK”).click

      Could you elaborate the problem about “browser loading”. What’s the exact problem you’re having there? I am currently involved in a project where JavaScript and Ajax is heavily used and i don’t have any problems testing that application with Watir.

      • Thanks for the response, Jarmo!

        Just a little info on what I’m running
        Ruby Version: 1.8.7
        Watir Version: 1.9.0

        For the javascript pop-up problem. I have a simple pop-up with an “OK” button. I’ve tried this and it doesn’t work:

        $browser.javascript_dialog.button(“OK”).click

        then I tried your method and this didn’t work:

        js_dialog = browser.javascript_dialog
        Watir::Wait.until {js_dialog.present?}
        js_dialog.close

        I’m at a loss. I don’t get an error, the script just stops running.

          • I checked again and I am using the latest version of watir 2.0. Here is a snippet of the code before and after the pop up.

            puts “Step 14: Approving Entitlement”
            $browser.select_list(:id, “rSide:entitlementStatus”).set “Ent Approved”

            sleep 4
            $browser.javascript_dialog.button(‘OK’).click
            $browser.driver.switch_to.alert.accept

            sleep 3
            $browser.button(:id, “authButton”).click

            You can also see I am using “sleep” a ton. Which I hate doing but have found no other way that works.

            Thanks

            • As i understand, then doing the #set will trigger the popup? If you put one “puts” statement right after #set, will you see that? Try #set_no_wait instead and lose all the “sleep” statements, they’re not necessary.

              And again, to really get rid of those popups, override the function as written in the wiki link i provided :)

              Jarmo

              • Thank you so much, Jarmo! You were correct that the #set did trigger the popup. Trying #set_no_wait solved the problem and clicked ok on the javascript popup.

                I’m not sure about those specific “sleeps” but most of my “sleeps” are necessary, at least until I find a better way. If I leave out the “sleeps” then watir starts looking for the element before the page is loaded and gives an error because it does not find it.

                Thanks again!

                • For that you can use different helping methods, i’d write your code like this instead:
                  puts “Step 14: Approving Entitlement”
                  $browser.select_list(:id, “rSide:entitlementStatus”).set “Ent Approved”

                  # no sleep necessary in here since this is will try to find that window and button during next 60 seconds
                  $browser.javascript_dialog.button(‘OK’).click
                  $browser.driver.switch_to.alert.accept

                  # use #when_present in here, which waits for the correct amount
                  $browser.button(:id, “authButton”).when_present.click

                  # or if the button is present all the time, but you need to wait some other condition to be true, then use #wait_until instead:
                  # browser.wait_until {something_which_will_be_true_eventually}
                  # button.click…

      • Thanks again, Jarmo. I agree that the wordpress reply system needs some work! Pretty bad. I will definitely check out stackoverflow and the watir google group and start posting some questions there.

  3. By sheer coincidence I am trying to install watir for the first time today, and I think the coincidence of the new release happening today is confusing me. Is the information on the Installation and Platforms pages outdated now that 2.0 is out? Is watir-webdriver still considered “experimental” and is it still necessary to use Ruby 1.8 rather than 1.9? What do I need to do to get support for Firefox 5 and 6?

    I don’t want to follow outdated installation instructions and get myself into an inconsistent state, so I’d like to do a clean install of whatever the newest stuff is.

  4. Željko, great post. I’ve been away from Watir for a while (currently leading the testing for Android-based Google TV) but it’s great fun to read about what’s been happening.

    Congrats to you and to everyone who’s worked on making 2.0 happen!

  5. This is so awesome!! thank you for still working on the best test tool in the world!

    Will Watir 2.0 work with 1.9.x ruby yet? Or are1.8.6 and 1.8.7 still the only versions supported?

    • I myself haven’t ran my work-related projects against Ruby 1.9.2 (don’t use 1.9.1, it is discouraged in the whole Ruby community as having nasty bugs in it), but it should work nevertheless, since i know that there are people, who are using 1.9.2 with Watir.

  6. Although everyone here has been incredibly helpful, I realize this post is not the correct place to be posting my questions concerning Watir coding. Can someone please tell me the best place (i.e. Most likely to get a response) to post my watir questions?

  7. Pingback: A Smattering of Selenium #58 « Official Selenium Blog

  8. Pingback: QuickLinks for August 2011 | (Agile) Testing

  9. I user Ruby 1.9.2 ,Watir 2.0.1 in Windows 7 x64 with IE9.
    It works well at the beginning,but it suddenly does’t work. when i run ‘ie = Watir::IE.new’,it says ‘timeout:error: execution expired’.
    Can you help me?

  10. Pingback: #46 Jarmo Pertman on Watir 2.0 | Watir Podcast

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s