Skip to content

Instantly share code, notes, and snippets.

@bfaloona
Created July 30, 2009 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfaloona/158796 to your computer and use it in GitHub Desktop.
Save bfaloona/158796 to your computer and use it in GitHub Desktop.
watircraft page, step definition, and method to call should assertion on a page object using gherkin text
#
# detail_reports_page.rb
#
class DetailReportsPage < ::Taza::Page
element(:add_report) {browser.link(:href, 'javascript:createReport();')}
field(:level_dropdown) {browser.select_list(:name, 'Level')}
end
#
# steps.rb
# example steps:
# Then the detail reports page add report button exists
# Then the detail reports page level dropdown does not exist
# Then the summary page export link does not exist
#
Then /^the (.+? page) (.+?) (does not )?exists?$/ do |page, obj_desc, negation|
case obj_desc
when / button$/
obj_desc.gsub!(/ button$/, '')
page_obj_should(page, obj_desc, nil, negation, :exist)
when / link$/
obj_desc.gsub!(/ link$/, '')
page_obj_should(page, obj_desc, nil, negation, :exist)
else
page_obj_should(page, obj_desc, nil, negation, :exist)
end
end
#
# method.rb
#
# evaluate page object with should or should_not assertion
# @param [String, #to_s] page - gherkin description of the watircraft page object
# @param [String, #to_s] obj_desc - gherkin description of the object on the page
# @param [String] attribute - attribute or method chain appended to the page object.
# - use nil for no attribute
# @param [Boolean] negation - true evaluates page object with should_not instead of should
# @param [String, #to_s] matcher - matcher and, if applicable, its parameters
def page_obj_should(page, obj_desc, attribute, negation, matcher)
if negation
should_case = '.should_not '
else
should_case = '.should '
end
page_object_string = obj_desc.computerize
# when the page object evaluates to a String object,
# or when an UnknownObjectException is raised,
# then reference the built-in '_field' object instead.
begin
if eval( page.computerize + '.' + page_object_string ).class == String
page_object_string = page_object_string << '_field'
end
rescue Watir::Exception::UnknownObjectException => e
page_object_string = page_object_string << '_field'
end
eval [page.computerize, page_object_string, attribute].compact.join('.') << should_case << matcher.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment