Skip to content

Page Object Pattern

rueyaa332266 edited this page Oct 15, 2019 · 3 revisions

Page object pattern

Page object pattern is a well-known pattern when using selenium. It reduces the amount of duplicated code and makes test code more flexible.

You can also find more explanation in Selenium wiki .

Page object pattern in Bucky

Bucky also supports page object pattern by creating part file in YAML and page_object file in Ruby. Bucky will create the object of each part by running page_object Ruby code. So the user operations which can be use in scenario test code, can specific the element by page and part keys. Moreover, you can create a custom user operate by adding the code in the page_object file.

Creating part and page_object

You can create part and page by Bucky command.

Note: Part file and page_object is a set with same name.

Note: You can find this example file here

Example:

# Create github top page
bucky make page github_top --service bucky_hands_on --device pc

Part file

The part file is composed of element name and it's location.

Note: Method to find elements can be: id, class, xpath, css, name, tag_name, link_text, partial_link_text

Description:

element_name:
    - The method to find the element by
    - The locator to use

Example:

search_resault:
    - xpath
    - //div[contains(@class,'jump-to-suggestions')]

Page_object file

It is the Ruby file that crate page object in the same name of part file. You don't have to add any code in here although you want to make some custom user operation.

Note: You can use the name of part as an Selenium::WebDriver::Element object.

Example:

# frozen_string_literal: true

require 'bucky/test_equipment/pageobject/base_pageobject'
module Services
  module BuckyHandsOn
    module Pc
      module PageObject
        class GithubTop < Bucky::TestEquipment::PageObject::BasePageObject
          protected

           def click_after_three_second(_)
            sleep(3)
            search_resault.click
          end
        end
      end
    end
  end
end

Usage:

The custom user operation can use in the scenario test code.

exec:
  operate: click_after_three_second # custom method in page_object file
  page: page_name # page_object file name