Class: WatirPump::Page

Inherits:
Component show all
Extended by:
Forwardable
Defined in:
lib/watir_pump/page.rb

Overview

Representation of a single page of the application under test.

Implements Singleton pattern.

Constant Summary collapse

INSTANCE_DELEGATED_METHODS =

List of class methods forwarded to the singleton instance

%i[
  browser
  open open_yield open_dsl
  use use_yield use_dsl
  act act_yield act_dsl
  loaded?
  matches_current_url?
  url_template
].freeze

Constants included from Constants

Constants::CLICKABLES, Constants::METHODS_FORWARDED_TO_ROOT, Constants::READABLES, Constants::WRITABLES

Instance Attribute Summary

Attributes inherited from Component

#browser, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Component

component, components, custom_reader, custom_writer, decorate, element, elements, #fill_form, #fill_form!, #form_data, form_field_readers, form_field_writers, #initialize, #inspect, inspect_properties, query, region, #root

Methods included from Components::Flag

#flag_accessor, #flag_reader, #flag_writer

Methods included from Components::DropdownList

#select_accessor, #select_reader, #select_writer

Methods included from Components::CheckboxGroup

#checkbox_accessor, #checkbox_reader, #checkbox_writer

Methods included from Components::RadioGroup

#radio_accessor, #radio_reader, #radio_writer

Constructor Details

This class inherits a constructor from WatirPump::Component

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class WatirPump::Component

Class Method Details

.instancePage

Returns singleton instance of current Page

Returns:



47
48
49
# File 'lib/watir_pump/page.rb', line 47

def instance
  @instance ||= new(WatirPump.config.browser)
end

.uri(uri = nil) ⇒ Object

Class macro declaring Page’s URI template. Example: ‘/jobs/{job_id}’

Parameters:

  • uri (String) (defaults to: nil)

    URI of current page. Compliant with Addressable::Template

See Also:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/watir_pump/page.rb', line 32

def uri(uri = nil)
  return @uri if @uri
  if uri.nil?
    ancestors[1..-1].each do |a|
      return @uri = a.uri if a.respond_to?(:uri) && a.uri
    end
  else
    @uri = uri
  end
  @uri
end

Instance Method Details

#loaded?Boolean

Predicate denoting if page is ready to be interacted with Overload in child class to customize the readiness criteria

Returns:

  • (Boolean)


166
167
168
# File 'lib/watir_pump/page.rb', line 166

def loaded?
  matches_current_url?
end

#matches_current_url?Boolean

Predicate denoting if current browser URL matches pages ‘uri` template

Returns:

  • (Boolean)


173
174
175
# File 'lib/watir_pump/page.rb', line 173

def matches_current_url?
  Addressable::Template.new(url_template).match browser.url
end

#open(params = {}, &blk) ⇒ Object

Opens the page in the browser and executes passed block in the scope of the page instance. Depending on the value of WatirPump.config.call_page_blocks_with_yield method #open_yield or #open_dsl is called internally

Parameters:

  • params (Hash) (defaults to: {})

    Parameters for the URL template



65
66
67
68
69
70
71
# File 'lib/watir_pump/page.rb', line 65

def open(params = {}, &blk)
  if WatirPump.config.call_page_blocks_with_yield
    open_yield(params, &blk)
  else
    open_dsl(params, &blk)
  end
end

#open_dsl(params = {}, &blk) ⇒ Object

Opens the page in the browser and executes passed block in the scope of the page instance (instance_exec).

Examples:

UserPage.open_dsl(id: 123) do
  puts root.h1.text
  puts browser.title
end

Parameters:

  • params (Hash) (defaults to: {})

    Parameters for the URL template



101
102
103
104
105
106
# File 'lib/watir_pump/page.rb', line 101

def open_dsl(params = {}, &blk)
  url = Addressable::Template.new(url_template).expand(params).to_s
  browser.goto url
  use_dsl(&blk) if block_given?
  self
end

#open_yield(params = {}, &blk) ⇒ Object

Opens the page in the browser and executes passed block in the scope of the page instance. Current page and browser references are passed to the yielded block.

Examples:

UserPage.open_yield(id: 123) do |page, browser|
  puts page.root.h1.text
  puts browser.title
end

Parameters:

  • params (Hash) (defaults to: {})

    Parameters for the URL template



84
85
86
87
88
89
# File 'lib/watir_pump/page.rb', line 84

def open_yield(params = {}, &blk)
  url = Addressable::Template.new(url_template).expand(params).to_s
  browser.goto url
  use_yield(&blk) if block_given?
  self
end

#url_templateString

Returns complete (base plus URI) URL template for current Page

Returns:

  • (String)


55
56
57
# File 'lib/watir_pump/page.rb', line 55

def url_template
  WatirPump.config.base_url + self.class.uri
end

#use(&blk) ⇒ Object Also known as: act

Executes passed block in the scope of the page instance. Depending on the value of WatirPump.config.call_page_blocks_with_yield method #use_yield or #use_dsl is called internally



112
113
114
115
116
117
118
# File 'lib/watir_pump/page.rb', line 112

def use(&blk)
  if WatirPump.config.call_page_blocks_with_yield
    use_yield(&blk)
  else
    use_dsl(&blk)
  end
end

#use_dsl(&blk) ⇒ Object Also known as: act_dsl

Executes passed block in the scope of the page instance. (instance_exec)

Examples:

UserPage.use_dsl do
  puts root.h1.text
  puts browser.title
end


145
146
147
148
149
# File 'lib/watir_pump/page.rb', line 145

def use_dsl(&blk)
  wait_for_loaded
  instance_exec(&blk)
  self
end

#use_yield {|_self, browser| ... } ⇒ Object Also known as: act_yield

Executes passed block in the scope of the page instance. Current page and browser references are passed to the yielded block.

Examples:

UserPage.use_yield do |page, browser|
  puts page.root.h1.text
  puts browser.title
end

Yields:

Yield Parameters:



130
131
132
133
134
# File 'lib/watir_pump/page.rb', line 130

def use_yield
  wait_for_loaded
  yield self, browser
  self
end

#wait_for_loadedPage

Waits until current Page is loaded

Returns:



155
156
157
158
159
160
# File 'lib/watir_pump/page.rb', line 155

def wait_for_loaded
  Watir::Wait.until(message: "Timeout waiting for #{self} to load") do
    loaded?
  end
  self
end