Class: Test::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/test/page.rb,
lib/test/page/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element = nil) ⇒ Page

Returns a new instance of Page

Parameters:

  • Element (Object)

    element for the Test::Page. element set via block will be used if not specified.


74
75
76
# File 'lib/test/page.rb', line 74

def initialize(element=nil)
  @element = element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args)

Proxies every method call not found on Test::Page to element instance. Subsequent executions of the same method will be invoked on the Test::Page object directly.


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/test/page.rb', line 88

def method_missing(name, *args)
  begin
    element
  rescue SystemStackError
    raise_invalid_element_definition
  end

  if element.respond_to?(name)
    self.class.send :define_method, name do |*args|
      element.send(name, *args) {yield}
    end
    self.send(name, *args) {yield}
  else
    super
  end
end

Class Attribute Details

.browser

Set and get the browser object for Test::Page.

This makes it possible to reuse one global browser object between different Test::Page objects. It will be also shared between sub-classes of Test::Page class.

Examples:

Set Watir browser object as the global browser

Test::Page.browser = Watir::Browser.new

19
20
21
# File 'lib/test/page.rb', line 19

def browser
  @browser
end

Instance Attribute Details

#browser

Set and get the browser object for specific Test::Page instance.

This is useful if some specific Test::Page instance needs a different browser than is set via browser method. Popup browser windows might be one example.

If browser is not set via #browser then browser set via browser will be used.


50
51
52
# File 'lib/test/page.rb', line 50

def browser
  @browser || parent_page_browser
end

Class Method Details

.element(&block)

Set element for the Test::Page via block. It will be evaluated lazily after Test::Page has been instantiated.

Element is like the container of the Test::Page - everything outside of that element is not considered as part of the Test::Page. Use as specific element as possible.

Examples:

Use Watir::Div as an element.

class MyPage < Test::Page
  element { browser.div(:id => "search") }
end

35
36
37
# File 'lib/test/page.rb', line 35

def element(&block)
  @element_block = block
end

Instance Method Details

#elementObject

Get the element instance.

When #setup is defined, it will be executed once per Test::Page instance.

Returns:

  • (Object)

    if element is specified for #initialize.

  • (Object)

    otherwise element block is evaluated once per Test::Page instance and its value will be returned.

Raises:

  • (NoBrowserSetException)

    if element has been set via block and browser has not been set.


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/test/page.rb', line 61

def element
  @setup_done ||= begin
                    setup if respond_to?(:setup)
                    true
                  end
  @element ||= begin
                 raise_no_browser_set_exception unless browser
                 element_proc = self.class.element_block
                 element_proc && instance_eval(&element_proc)
               end
end

#redirect_to(page, element = nil)

Create new Test::Page object conveniently on page actions.

Parameters:

  • Page (Page)

    page class to make an instance of

  • Element (Object)

    optional element instance to use as a container.


82
83
84
# File 'lib/test/page.rb', line 82

def redirect_to(page, element=nil)
  page.new element
end