I’ve very recently released a watir-page-helper gem that provides some useful helper methods when creating page models when using Watir-WebDriver (I chose not to support Watir as it doesn’t work on Firefox 4, therefore not on non-Windows machines).
This is loosely based upon the great work that Jeff Morgan did in his series of UI testing blog posts, but takes the concept a lot further IMO.
I wanted it to be solid before I released it as a gem, so I wrote unit tests for every method, and wrote yard tagged documentation for each method also. You can see the documentation automatically generated on rdoc.info.
gem install watir-page-helper
require 'watir-webdriver' require 'watir-page-helper' class MyPage include WatirPageHelper direct_url "http://www.google.com" expected_element :text_field, :name => "q" expected_title "Google" text_field :search_box, :name => "q" button :search, :name => "btnG" def initialize browser, visit = false @browser = browser goto if visit expected_element if respond_to? :expected_element has_expected_title? if respond_to? :has_expected_title? end end browser = Watir::Browser.new :chrome page = MyPage.new browser, true page.search_box = "Watirmelon" #This method is created by WatirPageHelper page.search #This method is created by WatirPageHelper also browser.close
The above example is very basic, but shows the concept well. You’re much better off putting the initialize method into a base page class so you don’t need to call the same methods for every page.
What you get
- direct_url: allows you to navigate to a page upon initialization, if visit is set to true
- expected_title: allows you to automatically assert the expected title of the page when it is initialized
- expected_element: allows you to initialize the page by looking for a certain element. This is useful for pages that load dynamic content.
Element methods for a majority of the Watir-WebDriver supported elements which generate useful helper methods.
For example: text_field, select_list, radio_button, form, div, span, h1..h6 etc.
I’ve been using a watir page helper on a few different projects now and I’m sick of copy-and-pasting the code each time, so I thought it’d be nice to package it as a gem.
I also think it’s important to have unit tests for this kind of stuff, so it was a good opportunity to write unit tests for every method in these classes. The documentation was an added bonus too.
I hope you find this useful, please let me know if anything doesn’t make any sense.