An update to my Watir Page Helper Gem

I’ve released version 0.2.0 of the watir-page-helper gem, a gem to help you create page models for watir-webdriver.

gem install watir-page-helper

There’s two changes included:

  • Nested elements are now properly supported (see below)
  • radio_button has been deprecated and renamed to radio (to align with the watir-webdriver)

Properly supporting nested elements

To be honest, my first attempt at doing this was half-baked, but there’s now solid support for this. Take this example.

Your app’s source html

<div id="myNiceDiv">
  <div>This div is unnamed and inside myNiceDiv.</div>
  <span> This span is unnamed and inside myNiceDiv.</span>

Say you want to define the div and the span that are inside ‘myNiceDiv’. Previously, this was hard to do, as they don’t have IDs and therefore not accessible off the @browser object. Enter nested elements.

You’d define this like such

class PageNestedDiv
  div :my_nice_div, :id => 'myNiceDiv'
  div(:my_unnamed_div) { |page| page.my_nice_div_div.div }
  span(:my_unnamed_span) { |page| page.my_nice_div_div.span }

You can see that we use a block to define the nested div and span, and specify they are children of the my_nice_div. The extra div on my_nice_div_div is because page.my_nice_div returns the text of my_nice_div.

You can still specify identifiers on the nested elements, something like this will work:

span(:my_unnamed_span) { |page| page.my_nice_div_div.span :index => 0 }

but as you can see, the index identifier is superfluous as no identifier assumes the first matching element in watir-webdriver.

I hope you find these changes useful and let me know if there’s anything else you’d like to see.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

13 thoughts on “An update to my Watir Page Helper Gem”

  1. Inspiring! I’d like to tie this into a command line and HTML reporting system for the test framework I built for my company; this would involve altering the source code and using it to test my company’s software, would this be okay? I don’t intend to publish it, but if I can build a version that’s more generically useful I’ll send it by you and you can see if you can make use of it.


  2. Any chance for Watir 2.0+ support? It converged with Watir-WebDriver quite a bit, although there are still some differences in the APIs.

    I need Watir because I can’t drive IE with WebDriver. WebDriver reqiures Protected Mode setting to be equal for all IE zones, and I need to run tests on a machine with no admin. privileges where Protected Mode setting can’t be changed.

    As a result, I drive Firefox with Watir-WebDriver and IE with Watir.


  3. Alister,

    I love this tool, but I have one problem. As a bit of a neophyte programmer, I’m having problems figuring out the syntax I need to use in order to get my text fields that are nested inside iframes to work.

    Here’s how I locate the field with watir-webdriver syntax:

    @browser.frame(:id, “Main85e87609x1ffdx4dc2x968fx284f1a93ad51”).text_field(:, “message”)

    What would the Water-page-helper-compatible line look like, in that case?

    I appreciate your help!


  4. Hmmm…

    My code is finally at a point where I’m able to start effectively using this tool, and I’ve run into my first problem…

    Here’s my code:

    link(:site_info_display_options) { |page| page.frame(:id=>/^Main.+/, :index=>0).link(:title, “Site Information Display Options”) }

    When I try to click that link, the error stack says that “frame” is an undefined method and, further, that the problem is localized in the ‘call’ block of the create_element_getter method.

    I hope that’s enough info for you to diagnose what I’m doing wrong.


    1. Sorry about that. I hadn’t added frame support until now.
      I’ve just release 0.3.0 of the gem that will support this now.
      So run ‘gem update watir-page-helper’ or ‘bundle update’ (if you use bunder)

      See: my blog post for some example usage.


  5. I don’t know if this is the right place for this but do you have examples of how to use the gem with tables? I see your logic for table and row objects in the watir-page-helper.rb, but your example says something like this:

    row(:test_table_row_1) { | test_table | }

    When I run that, I’m told that “tr” is an undefined method. So where do you define tr? To give an example, I have this defined in my page object:

    table :atomicList, :id => “atomic”
    row(:atomicRows) {|atomicTable|}

    Conceptually this matches your example from the code comments. When I do this, I get the error I mentioned. Based on your examples in this post, it seems like “tr” and “td” would have to be methods that have their own define_method calls, similar to “div” and “span”. But those clearly aren’t in the library of functionality so I have to assume you do this some other way.

    Any help would be appreciated. Thanks in advance.


    1. You’re very close. The block always yields the page, so, if you have a look at the example table spec:

      class PageNestedTable "mySecondTable"
      row(:test_table_row_1) { |page| }
      cell(:test_table_row_1_cell_1) { |page| }

      so, you’ll need something like

      table :atomicList, :id => “atomic”
      row(:atomicRows) {|page|}

      I hope this helps.


Comments are closed.