Module: PageObject
- Defined in:
- lib/site-object/page.rb
Overview
Page objects are containers for all of the functionality of a page that you want to expose for testing purposes. When you create a page object you define a URL to access it, elements for all of the page elements that you want to work with as well as higher level methods that use those elements to perform page operations.
Here’s a very simple account edit page example that has two fields and one button and assumes that you’ve defined a site object called ‘ExampleSite.’
class AccountDetailsEditPage < ExampleSite::Page
set_url "/accounts/{account_code}/edit" # Parameterized URL.
element(:first_name) {|b| b.text_field(:id, 'fname') } # text_field is a Watir method.
element(:last_name) {|b| b.text_field(:id, 'fname') } # text_field is a Watir method.
element(:save) {|b| b.(:id, 'fname') } # text_field is a Watir method.
def update(fname, lname) # Very simple method that uses the page elements defined above.
first_name.set fname
last_name.set lname
save.click
end
end
The URL defined in the example above is “parameterized” (account_code is a placeholder.) You don’t need to specify parameters for a URL, but if you do you need to call the page with a hash argument. To use the page after initializing an instance of the site object:
site.account_details_edit_page(account_code: 12345)
Pages only take arguments if the URL is parameterized.
Note that in the example above that there’s no explicit navigation call. This is because the site will look at its current URL and automatically navigate to the page if it’s not already on it.
Here’s a simple page object for the rubygems.org search page. Note that no page URL is defined using the PageObject#set_url method. This is because the page URL for the landing page is the same as the base URL for the site. When a page URL isn’t explicitly defined the base URL is used in its place:
class LandingPage < RubyGems::Page
element(:search_field) { |b| b.browser.text_field(:id, 'home_query') }
element(:search_submit) { |b| b.browser.input(:id, 'search_submit') }
def search(criteria)
search_field.set('rails')
search_submit.click
expect_page(SearchResultsPage)
end
end
Page objects aren’t initialized outside of the context of a site object. When a site object is initialized it creates accessor methods for each page object that inherits from the site’s page class. In the example above, the LandingPage class inherits from the RubyGems site object’s page class so you’d be able to use it once you’ve initialized a RubyGems site:
site.landing_page.search("rails") # Returns an instance of the landing page after performing a search.
Because the site object has accessor methods for all of its pages and page navigation is automatic it’s not always necessary to specify a page object directly. But you can get one if need one:
page = site.some_page
=><SomePage>
Defined Under Namespace
Modules: PageClassMethods, PageInstanceMethods