Class: Taza::Page

Inherits:
Object show all
Defined in:
lib/taza/page.rb

Overview

An abstraction of a web page, place the elements you care about accessing in here as well as specify the filters that apply when trying to access the element.

Example:

require 'taza'
class HomePage < Taza::Page
  element(:foo) {browser.element_by_xpath('some xpath')}
  filter :title_given, :foo

  def title_given
    browser.title.nil?
  end
end

homepage.foo will return the element specified in the block if the filter returned true

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Page) initialize(page_module = nil)

A new instance of Page



87
88
89
90
# File 'lib/taza/page.rb', line 87

def initialize(page_module = nil)
  add_element_methods(page_module)
  @active_filters = []
end

Instance Attribute Details

- (Object) browser

Returns the value of attribute browser



17
18
19
# File 'lib/taza/page.rb', line 17

def browser
  @browser
end

Class Method Details

+ (Object) element(name, &block)

A element on a page

Watir Example:

class HomePage < Taza::Page
  element(:foo) {browser.element_by_xpath('some xpath')}
end

homepage.foo.click



34
35
36
37
38
39
40
41
42
43
# File 'lib/taza/page.rb', line 34

def self.element(name,&block)
  if !@module.nil?
    self.elements[@module] = Hash.new if self.elements[@module].nil?
    self.elements[@module] = self.elements[@module].merge({ name => block })
  elsif !self.elements[name].nil?
    raise ElementError,"Duplicate definations for Element - #{name} on Page - #{self.to_s}"
  else
    self.elements[name] = block
  end
end

+ (Object) elements

:nodoc:



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

def elements # :nodoc:
  @elements ||= {}
end

+ (Object) filter(method_name, *elements)

A filter for elemenet(s) on a page Example:

class HomePage < Taza::Page
  element(:foo) {browser.element_by_xpath('some xpath')}
  filter :title_given, :foo
  #a filter will apply to all elements if none are specified
  filter :some_filter
  #a filter will also apply to all elements if the symbol :all is given
  filter :another_filter, :all

  def some_filter
    true
  end

  def some_filter
    true
  end

  def title_given
    browser.title.nil?
  end
end


67
68
69
70
71
72
# File 'lib/taza/page.rb', line 67

def self.filter(method_name, *elements)
  elements = [:all] if elements.empty?
  elements.each do |element|
    self.filters[element] = self.filters[element] << method_name
  end
end

+ (Object) filters

:nodoc:



22
23
24
# File 'lib/taza/page.rb', line 22

def filters # :nodoc:
  @filters ||= Hash.new { [] }
end

+ (Object) page_module(name) {|block| ... }

Yields:

  • (block)


74
75
76
77
78
# File 'lib/taza/page.rb', line 74

def self.page_module(name,&block)
  @module = name
  yield(block)
  @module = nil
end

+ (Object) page_module_filter(method_name, page_module_name, *elements)



80
81
82
83
84
85
# File 'lib/taza/page.rb', line 80

def self.page_module_filter(method_name, page_module_name, *elements)
  elements = [page_module_name] if elements.empty?
  elements.each do |element|
    self.filters[element] = self.filters[element] << method_name
  end
end

Instance Method Details

- (Object) add_element_method(params)

:nodoc:



106
107
108
109
110
111
112
113
# File 'lib/taza/page.rb', line 106

def add_element_method(params) # :nodoc:
  metaclass.class_eval do
    define_method(params[:element_name]) do |*args|
      check_filters(params)
      self.instance_exec(*args,&params[:element_block])
    end
  end
end

- (Object) add_element_methods(page_module = nil)

:nodoc:



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/taza/page.rb', line 92

def add_element_methods(page_module = nil) # :nodoc:
  self.class.elements.each do |element_name,element_block|
    if (element_block.is_a?(Hash) && !page_module.nil? && page_module==element_name)
      element_block.each do |key,value|
        filters = self.class.filters[element_name] + self.class.filters[:all] + self.class.filters[key]
        add_element_method(:filters => filters, :element_name => key, :element_block => value)
      end
    else
      filters = self.class.filters[element_name] + self.class.filters[:all]
      add_element_method(:filters => filters, :element_name => element_name, :element_block => element_block)
    end
  end
end

- (Object) check_filters(params)

:nodoc:



115
116
117
118
119
120
121
122
123
# File 'lib/taza/page.rb', line 115

def check_filters(params) # :nodoc:
  params[:filters].each do |filter_method|
    unless @active_filters.include?(filter_method)
      @active_filters << filter_method
      raise FilterError, "#{filter_method} returned false for #{params[:element_name]}" unless send(filter_method)
      @active_filters.delete(filter_method)
    end
  end
end