When using RSpec expectations and matchers to assert something binary, it’s easy to write a step definition that looks something like this:
on EtsyTreasuryPage do |page| page.item_info_div.exists?.should be_true end
There are other ways to make this step more readable. When ever you are checking a method with a question mark following its name, you can use the .should methods on its parent, and pass the name of the method you are expecting. For example, this is how you would write the above code.
on EtsyTreasuryPage do |page| page.item_info_div.should be_exists end
This will execute exactly the same as the first example, although it’s not quite as nice to read. To make it easier to read, we can monkey patch the Watir-WebDriver class to have a method alias.
Putting this code in a file you load:
module Watir class Element alias_method :shown?, :exists? end end
means that you are monkey patching the Element class, and can now use .shown? instead of, or in addittion to, .exists?.
This means your step definition is now highly readible:
on EtsyTreasuryPage do |page| page.item_info_div.should be_shown end
Update: 10 August 2011
Make sure you have the rspec gem installed (either gem install, or via bundler) to ensure you have access to these methods.