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)



92
93
94
95
# File 'lib/taza/page.rb', line 92

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
44
45
46
47
# File 'lib/taza/page.rb', line 34

def self.element(name,&block)
  if name.nil?
    raise ElementError, "Element name can not be nil"
  end

  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 element(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


71
72
73
74
75
76
# File 'lib/taza/page.rb', line 71

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)


78
79
80
81
82
83
# File 'lib/taza/page.rb', line 78

def self.page_module(name,&block)
  puts "Warning: page_module is deprecated; use a partial instead."
  @module = name
  yield(block)
  @module = nil
end

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



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

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:



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

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:



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/taza/page.rb', line 97

def add_element_methods(page_module = nil) # :nodoc:
  self.class.elements.each do |element_name,element_block|
    if 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:



120
121
122
123
124
125
126
127
128
# File 'lib/taza/page.rb', line 120

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