📜 ⬆️ ⬇️

Page Object - the path to perfect autotests



Good day to all!
This topic is about how we test the web interface of our product Plus1 WapStart . We use Page Object, since This pattern has much in common with real tasks and allows you to write autotests easy to read and understand.

What is Page Object


Page Object is a pattern for implementing smart auto checks. Gem page-object is the implementation of this pattern, which helps in creating flexible pages with objects for testing browser applications. The bottom line is to create levels of abstraction to separate tests from test items, and provide a simple interface for the elements on the page. Gem works with watir-webdriver and selenium-webdriver.

Installing Ruby and gems


Install the RVM :
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer) 

To run the examples you need the latest version of Ruby and gems:
 $ rvm reload $ rvm install ruby-1.9.3-p125 $ rvm gemset create page-object && rvm use ruby-1.9.3-p125@page-object $ gem install rake page-object 

Hello, Page Object!


Create a RegistrationPage for the registration page - for this we enable the PageObject module, specify the page address and describe the elements:
')
 class RegistrationPage include PageObject page_url "http://www.passport.wapstart.ru/registration/" text_field(:email, :name => 'mail') text_field(:password, :id => 'hidden-password') select_list(:source, :name => 'informationSource') checkbox(:agreement, :name => 'agreement') button(:register, :name => "register") end 

The page describes six methods, each of which additionally generates several more, more on this later ... Add default data:

 DEFAULT_DATA = { 'email' => 'habrahabr@gmail.com', 'password' => 'qwerty', 'source' => ' ,   WapStart' } 


Create a method to fill only the required fields:

 def default(data = {}) populate_page_with DEFAULT_DATA.merge(data) check_agreement register end 


All the basic page objects are defined, we use them in AutoTest. Select the selenium-webdriver driver, passing it to the PageObject constructor:

 browser = Selenium::WebDriver.for :ff registration_page = RegistrationPage.new(browser, true) registration_page.default 

The argument true opens page_url, if we get to this page by clicking on the link, then it can be not transmitted. Run an example:

 $ ruby tests/RegisterDefault.rb 

Thus, in auto tests we avoid using find_element and locators, the code is easily readable and reusable.

Dynamic generation of page access methods


Accessors - class methods added to the page by connecting the PageObject module. Methods generate another set of methods that provide access to the elements of a web page:


An example of a fully described registration page:
 require 'page-object' require 'selenium/webdriver' class RegistrationPage include PageObject page_url "http://www.passport.wapstart.ru/registration/" DEFAULT_DATA = { 'email' => 'habrahabr@gmail.com', 'password' => 'qwerty', 'source' => ' ,   WapStart' } text_field(:email, :name => 'mail') text_field(:password, :id => 'hidden-password') checkbox(:showPassword, :id => 'showPassword') select_list(:source, :name => 'informationSource') text_field(:fio, :name => 'fio') text_field(:phone, :name => 'phone') select_list(:purpose, :name => 'registrationPurpose') checkbox(:haveCode, :id => 'havePartnerCode') text_field(:code, :id => 'plus1PartnerCode') checkbox(:agreement, :name => 'agreement') button(:register, :name => "register") # Exception span(:errorCode, :xpath => "//form[@id='registrationForm']/table/tbody/tr[9]/td[2]/span/span[2]") def default(data = {}) populate_page_with DEFAULT_DATA.merge(data) check_agreement register end end 

An example of an autotest to verify the validity of a partner code:
 describe "RegisterUser" do let(:browser) { @browser ||= Selenium::WebDriver.for :ff } #Initialize new instance of Browser(driver) it "InvalidCode" do page = RegistrationPage.new(browser, true) page.email = 'habrahabr@gmail.com' page.password = 'qwerty' page.source = ' ,   WapStart' page.check_haveCode page.code = 12345678 page.check_agreement page.register page.errorCode?.should be_true end after { browser.close } end 

Examples are available on GitHub :
 $ git clone git@github.com:ivaravko/pageobject-example.git $ cd pageobject-example/ pageobject-example$ rake 

Opening a page from another page


In a large project there are many pages and they are interconnected; in order to perform the transition, we add the next step to the default RegistrationPage class method - LoginPage.new (browser, true). This will allow you to go to the login and password entry page without changing the autotest.
 def default(data = {}) populate_page_with DEFAULT_DATA.merge(data) check_agreement register LoginPage.new(browser, true) end 

Posted by ivaravko

Links

Blog author gem
GitHub page
Documentation

Source: https://habr.com/ru/post/138674/


All Articles