Skip to content

Get me started right now!

akzhan edited this page Oct 11, 2012 · 12 revisions

So you want to use the page-object gem. Here is what you need to do.

Create your page

The first thing you must do is create your pages. These are simple ruby classes that include the PageObject Module. Do not create your own initialize method as one already exists and should not be overwritten. If you do need to complete some page initialization then you could implement a initialize_page callback method which will be called after the page is created and all dynamic methods are added.

class RegistrationPage
  include PageObject
end

By including the PageObject Module you have added a lot of capabilities to your page. Let's take a look at how we might use some of that right now.

Describe your page

After you create your class you need to describe the web page this class represents. The RegistrationPage example might look like this:

class RegistrationPage
  include PageObject

  text_field(:name, :id => 'name')
  text_field(:email, :id => 'email')
  button(:register, :value => 'Register')
end

By calling these methods, the PageObject module will add several additional methods for you. To learn about what methods are available and the generated methods please see the Accessors page.

Use your page

Now that we have a basic page object defined it is time to put it to use. You can use either watir-webdriver or selenium-webdriver as the driver gem. Just pass them into the constructor.

browser = Watir::Browser.new :firefox
registration_page = RegistrationPage.new(browser)

or

browser = Selenium::WebDriver.for :firefox
registration_page = RegistrationPage.new(browser)

Once created, you can interact with the page using the generated methods.

registration_page.name = 'Test User'
registration_page.email = 'test@example.com'
registration_page.register

That is all there is to getting started with this gem. The good news is that there is a lot of additional functionality that was not covered in this brief introduction. Please go back to the page-object top level page to learn more.