module PageObject

Module that when included adds functionality to a page object. This module will add numerous class and instance methods that you use to define and interact with web pages.

If we have a login page with a username and password textfield and a login button we might define our page like the one below. We can then interact with the object using the generated methods.

@example Login page example

class LoginPage
  include PageObject

  text_field(:username, :id => 'user')
  text_field(:password, :id => 'pass')
  button(:login, :value => 'Login')
end

...

browser = Watir::Browser.new :firefox
login_page = LoginPage.new(browser)
login_page.username = 'cheezy'
login_page.password = 'secret'
login_page.login

@see PageObject::Accessors to see what class level methods are added to this module at runtime.

Constants

VERSION

@private

Attributes

browser[R]

@return [Watir::Browser or Selenium::WebDriver::Driver] the platform browser passed to the constructor

platform[R]

@return [PageObject::WatirPageObject or PageObject::SeleniumPageObject] the platform page object

Public Class Methods

add_framework(key, framework) click to toggle source

Add a new javascript framework to page-object. The module passed in must adhere to the same prototype as the JQuery and Prototype modules.

@param [Symbol] the name used to reference the framework in subsequent calls @param [Module] a module that has the necessary methods to perform the required actions.

# File lib/page-object-lds.rb, line 134
def self.add_framework(key, framework)
  PageObject::JavascriptFrameworkFacade.add_framework(key, framework)
end
default_element_wait() click to toggle source

Returns the default timeout for element level waits

# File lib/page-object-lds.rb, line 111
def self.default_element_wait
  @element_wait ||= 5
end
default_element_wait=(timeout) click to toggle source

Sets the default timeout for element level waits

# File lib/page-object-lds.rb, line 104
def self.default_element_wait=(timeout)
  @element_wait = timeout
end
default_page_wait() click to toggle source

Returns the default timeout for page lavel waits

# File lib/page-object-lds.rb, line 97
def self.default_page_wait
  @page_wait ||= 30
end
default_page_wait=(timeout) click to toggle source

Set the default timeout for page level waits

# File lib/page-object-lds.rb, line 90
def self.default_page_wait=(timeout)
  @page_wait = timeout
end
included(cls) click to toggle source

@private

# File lib/page-object-lds.rb, line 83
def self.included(cls)
  cls.extend PageObject::Accessors
end
javascript_framework=(framework) click to toggle source

Set the javascript framework to use when determining number of ajax requests. Valid frameworks are :jquery, :prototype, :yui, and :angularjs

# File lib/page-object-lds.rb, line 120
def self.javascript_framework=(framework)
  PageObject::JavascriptFrameworkFacade.framework = framework
end
new(browser, visit=false) click to toggle source

Construct a new page object. Prior to browser initialization it will call a method named initialize_accessors if it exists. Upon initialization of the page it will call a method named initialize_page if it exists.

@param [Watir::Browser or Selenium::WebDriver::Driver] the platform browser to use @param [bool] open the page if page_url is set

# File lib/page-object-lds.rb, line 66
def initialize(browser, visit=false)
  initialize_accessors if respond_to?(:initialize_accessors)
  initialize_browser(browser)
  goto if visit && respond_to?(:goto)
  initialize_page if respond_to?(:initialize_page)
end
register_widget(widget_tag, widget_class, base_element_tag) click to toggle source
# File lib/page-object-lds.rb, line 408
def self.register_widget(widget_tag, widget_class, base_element_tag)
  Widgets.register_widget(widget_tag, widget_class, base_element_tag)
end

Public Instance Methods

alert(frame=nil, &block) click to toggle source

Override the normal alert popup so it does not occur.

@example

message = @page.alert do
  @page.button_that_causes_alert
end

@param frame optional parameter used when alert is nested within a frame @param block a block that has the call that will cause the alert to display @return [String] the message that was contained in the alert

# File lib/page-object-lds.rb, line 223
def alert(frame=nil, &block)
  platform.alert(frame, &block)
end
attach_to_window(identifier, &block) click to toggle source

Attach to a running window. You can locate the window using either the window’s title or url. If it fails to connect to a window it will pause for 1 second and try again.

@example

page.attach_to_window(:title => "other window's title")

@param [Hash] either :title or :url of the other window. The url does not need to be the entire url - it can just be the page name like index.html @param block if present the block is executed and then execution is returned to the calling window

# File lib/page-object-lds.rb, line 356
def attach_to_window(identifier, &block)
  begin
    platform.attach_to_window(identifier, &block)
  rescue
    sleep 1
    platform.attach_to_window(identifier, &block)
  end
end
back() click to toggle source

Go back to the previous page

# File lib/page-object-lds.rb, line 382
def back
  platform.back
end
clear_cookies() click to toggle source

Clear the cookies from the browser

# File lib/page-object-lds.rb, line 396
def clear_cookies
  platform.clear_cookies
end
confirm(response, frame=nil, &block) click to toggle source

Override the normal confirm popup so it does not occur.

@example

message = @popup.confirm(true) do
  @page.button_that_causes_confirm
end

@param [bool] what response you want to return back from the confirm popup @param frame optional parameter used when the confirm is nested within a frame @param block a block that has the call that will cause the confirm to display @return [String] the message that was prompted in the confirm

# File lib/page-object-lds.rb, line 240
def confirm(response, frame=nil, &block)
  platform.confirm(response, frame, &block)
end
current_url() click to toggle source

get the current page url

# File lib/page-object-lds.rb, line 141
def current_url
  platform.current_url
end
element_with_focus() click to toggle source

Find the element that has focus on the page

# File lib/page-object-lds.rb, line 368
def element_with_focus
  platform.element_with_focus
end
execute_script(script, *args) click to toggle source

Execute javascript on the browser

@example Get inner HTML of element

span = @page.span_element
@page.execute_script "return arguments[0].innerHTML", span
#=> "Span innerHTML"
# File lib/page-object-lds.rb, line 270
def execute_script(script, *args)
  args.map! { |e| e.kind_of?(PageObject::Elements::Element) ? e.element : e }
  platform.execute_script(script, *args)    
end
forward() click to toggle source

Go forward to the next page

# File lib/page-object-lds.rb, line 389
def forward
  platform.forward
end
html() click to toggle source

Returns the html of the current page

# File lib/page-object-lds.rb, line 165
def html
  platform.html
end
in_frame(identifier, frame=nil, &block) click to toggle source

Identify an element as existing within a frame. A frame parameter is passed to the block and must be passed to the other calls to PageObject. You can nest calls to in_frame by passing the frame to the next level.

@example

in_frame(:id => 'frame_id') do |frame|
  text_field_element(:id => 'fname', :frame => frame)
end

@param [Hash] identifier how we find the frame. The valid keys are:

* :id => Watir and Selenium
* :index => Watir and Selenium
* :name => Watir and Selenium
* :class => Watir only

@param frame passed from a previous call to in_frame. Used to nest calls @param block that contains the calls to elements that exist inside the frame.

# File lib/page-object-lds.rb, line 293
def in_frame(identifier, frame=nil, &block)
  platform.in_frame(identifier, frame, &block)
end
in_iframe(identifier, frame=nil, &block) click to toggle source

Identify an element as existing within an iframe. A frame parameter is passed to the block and must be passed to the other calls to PageObject. You can nest calls to in_iframe by passing the frame to the next level.

@example

in_iframe(:id => 'iframe_id') do |iframe|
  text_field_element(:id => 'ifname', :frame => iframe)
end

@param [Hash] identifier how we find the iframe. The valid keys are:

* :id => Watir and Selenium
* :index => Watir and Selenium
* :name => Watir and Selenium
* :class => Watir only

@param frame passed from a previous call to in_iframe. Used to nest calls @param block that contains the calls to elements that exist inside the iframe.

# File lib/page-object-lds.rb, line 314
def in_iframe(identifier, frame=nil, &block)
  platform.in_iframe(identifier, frame, &block)
end
initialize_browser(browser) click to toggle source
# File lib/page-object-lds.rb, line 77
def initialize_browser(browser)
  @browser = browser
  include_platform_driver(browser)
end
modal_dialog() { || ... } click to toggle source

Override the normal showModalDialog call is it opens a window instead of a dialog. You will need to attach to the new window in order to continue.

@example

@page.modal_dialog do
  @page.action_that_spawns_the_modal
end

@param block a block that contains the call that will cause the modal dialog.

navigate_to(url) click to toggle source

navigate to the provided url

@param [String] the full url to navigate to

prompt(answer, frame=nil, &block) click to toggle source

Override the normal prompt popup so it does not occur.

@example

message = @popup.prompt("Some Value") do
  @page.button_that_causes_prompt
end

@param [string] the value returned to the caller of the prompt @param frame optional parameter used with the prompt is nested within a frame @param block a block that has the call that will cause the prompt to display @return [Hash] A has containing two keys - :message contains the prompt message and :default_value contains the default value for the prompt if provided

# File lib/page-object-lds.rb, line 258
def prompt(answer, frame=nil, &block)
  platform.prompt(answer, frame, &block)
end
refresh() click to toggle source

Refresh to current page

# File lib/page-object-lds.rb, line 375
def refresh
  platform.refresh
end
save_screenshot(file_name) click to toggle source

Save the current screenshot to the provided url. File is saved as a png file.

# File lib/page-object-lds.rb, line 404
def save_screenshot(file_name)
  platform.save_screenshot file_name
end
testModif() click to toggle source
# File lib/page-object-lds.rb, line 73
def testModif
  puts 'test'
end
text() click to toggle source

Returns the text of the current page

# File lib/page-object-lds.rb, line 158
def text
  platform.text
end
title() click to toggle source

Returns the title of the current page

# File lib/page-object-lds.rb, line 172
def title
  platform.title
end
wait_for_ajax(timeout = 30, message = nil) click to toggle source

Wait until there are no pending ajax requests. This requires you to set the javascript framework in advance.

@param [Numeric] the amount of time to wait for the block to return true. @param [String] the message to include with the error if we exceed the timeout duration.

# File lib/page-object-lds.rb, line 201
def wait_for_ajax(timeout = 30, message = nil)
  end_time = ::Time.now + timeout
  until ::Time.now > end_time
    return if browser.execute_script(::PageObject::JavascriptFrameworkFacade.pending_requests) == 0
    sleep 0.5
  end
  message = "Timed out waiting for ajax requests to complete" unless message
  raise message
end
wait_until(timeout = PageObject.default_page_wait, message = nil, &block) click to toggle source

Wait until the block returns true or times out

@example

@page.wait_until(5, 'Success not found on page') do
  @page.text.include? 'Success'
end

@param [Numeric] the amount of time to wait for the block to return true. @param [String] the message to include with the error if we exceed the timeout duration. @param block the block to execute. It should return true when successful.

# File lib/page-object-lds.rb, line 188
def wait_until(timeout = PageObject.default_page_wait, message = nil, &block)
  platform.wait_until(timeout, message, &block)
end

Private Instance Methods

call_block(&block) click to toggle source
# File lib/page-object-lds.rb, line 418
def call_block(&block)
  block.arity == 1 ? block.call(self) : self.instance_eval(&block)
end
include_platform_driver(browser) click to toggle source
# File lib/page-object-lds.rb, line 414
def include_platform_driver(browser)
  @platform = load_platform(browser, PageObject::Platforms.get)
end