Module: CapybaraPageObject::PageObject

Includes:
Capybara::DSL
Defined in:
lib/capybara_page_object/page_object.rb

Overview

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.

Examples:

Login page example

class LoginPage
  include PageObject

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

 = LoginPage.new
.username = 'ryan'
.password = 'starwars'
.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



45
46
47
48
# File 'lib/capybara_page_object/page_object.rb', line 45

def self.included(base)
  base.extend(CapybaraPageObject::ClassMethods)
  base.extend(CapybaraPageObject::ElementClassMethods)
end

Instance Method Details

#go_backObject



66
67
68
# File 'lib/capybara_page_object/page_object.rb', line 66

def go_back
  page.evaluate_script('window.history.back()')
end

#go_forwardObject



70
71
72
# File 'lib/capybara_page_object/page_object.rb', line 70

def go_forward
  page.evaluate_script('window.history.forward()')
end

#initialize(visit = false) ⇒ Object

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.



36
37
38
39
40
41
42
43
# File 'lib/capybara_page_object/page_object.rb', line 36

def initialize(visit=false)
  initialize_accessors if respond_to?(:initialize_accessors)
  if visit
    raise "Page must specify `page_url` to use visit functionality" unless respond_to?(:goto)
    goto
  end
  initialize_page if respond_to?(:initialize_page)
end

#loaded?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/capybara_page_object/page_object.rb', line 58

def loaded?
  unless respond_to?(:has_expected_element?)
    raise "Must set expected_element to use the `loaded?` method"
  end

  has_expected_element?
end

#reload_pageObject



74
75
76
77
# File 'lib/capybara_page_object/page_object.rb', line 74

def reload_page
  url = [ current_path, page.driver.request.env['QUERY_STRING'] ].reject{|s| s.nil? || s == '' }.join('?')
  page.visit url
end

#within(element, &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/capybara_page_object/page_object.rb', line 50

def within(element, &block)
  if element.kind_of?(CapybaraPageObject::Element)
    super(element.element, &block)
  else
    super(element, &block)
  end
end