I’ve been working on some improvements to my WatirMelonCucumber page object framework, partly inspired by the SpecDriver C# framework I put together, and also inspired by a work colleague who’s especially good at ruby and meta-programming.
Base Page Class to inherit from
The first improvement is adding a base page class that all pages defined inherit stuff from. This means I don’t have to write an initialize for every page, as the base page class initializes each page automagically. This base page class can also navigate to the selected pages direct if needed, check for the appearance of a certain element if needed, and check the title is correctly displayed, again, if needed. Lastly, the base page class catches any methods that are missing and throws these to the browser, which means you can call browser methods such as title, and text at the page level.
class BasePageClass include WatirWebDriverHelper 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 def method_missing sym, *args, &block @browser.send sym, *args, &block end end
Watir Page Helper Mixin
I originally saw the Watir Helper idea in Jeff Morgan’s cucumber framework, which I have used here. I have made quite a few changes, not only to support more elements, but also to allow passing in blocks to the helper methods. This helper is included in the base page class, and therefore is accessible in any page defined that inherits from the base page class.
Update: 5th May: See my watir-page-helper gem to include this automatically.
Resulting page model example
The combination of these two improvements mentioned, now mean the actual page classes are nice and neat. For example:
class GoogleHomePage < SearchPageClass direct_url "http://www.google.com" expected_title "Google" button :google_search, :name => "btnG" def search_for term self.search = term google_search GoogleResultsPage.new(@browser, false) end end
Ruby & Browser Support
I have changed the framework to use Chrome, since it’s so fast, but that’s easily configurable in env.rb, and watir-webdriver headless instead of Celerity. This means JRuby is not needed, and it can run under Ruby 1.9.2.
By introducing the base page class, and the watir page helper methods, it means the resulting page models are short, simple and easy to understand. There is a lot less repetition, for example, not including the watir helper in every page, as it’s included in the base, and also not needing an initialize for each page that essentially does the same thing.
Let me know what you think.