Class: PageObjectWrapper::PageObject

Inherits:
DslElementWithLocator show all
Defined in:
lib/page_object_wrapper/PageObject.rb

Constant Summary collapse

REQUIRED_ELEMENT_WAIT_PERIOD =
10
FEED_ALL =
Regexp.new(/^feed_all$/)
FEED =
Regexp.new(/^feed_([\w_]+)$/)
FIRE_ACTION =
Regexp.new(/^fire_([\w_]+)$/)
SELECT_FROM =
Regexp.new(/^select_from_([\w_]+)$/)
SELECT_ROW_FROM =
Regexp.new(/^select_row_from_([\w_]+)$/)
PAGINATION_EACH =
Regexp.new(/^([\w_]+)_each$/)
PAGINATION_OPEN =
Regexp.new(/^([\w_]+)_open$/)
VALIDATE =
Regexp.new(/^validate_([\w_\?]+)$/)
PRESS =
Regexp.new(/^press_([\w_\?]+)$/)
@@pages =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DslElement

#label_alias, #label_alias_value

Constructor Details

#initialize(label) ⇒ PageObject

Returns a new instance of PageObject.



31
32
33
34
35
36
37
38
39
40
# File 'lib/page_object_wrapper/PageObject.rb', line 31

def initialize(label)
  super label
  @esets = []
  @elements = []
  @actions = []
  @aliases = []
  @validators = []
  @tables = []
  @paginations = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

lazy evaluated calls of real watir elements are handled by :method_missing



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/page_object_wrapper/PageObject.rb', line 51

def method_missing(method_name, *args, &block)
  case 
    when KNOWN_ELEMENTS.include?(method_name.to_s.gsub(/^uniq_/,''))
      # page_object.uniq_xxx(hash)
      meth = method_name.to_s.gsub(/^uniq_/,'')
      e = Element.new(method_name.to_sym, meth)
      e.instance_eval { locator(args[0]); required(true) }
      @elements << e
    when has_eset?(method_name)
      # page_object.some_elements_set
      eset = eset_for(method_name)
      PageObjectWrapper.current_result = PageObject.return_array_of_watir_elements(eset)
    when has_element?(method_name)
      # page_object.some_element
      element = element_for(method_name)
      PageObjectWrapper.current_result = PageObject.return_watir_element element
    when FEED_ALL.match(method_name)
      # page_object.feed_all(:fresh_food)
      PageObjectWrapper.current_result = feed_elements(@elements, *args)
    when (FEED.match(method_name) and has_eset?($1))
      # page_object.feed_some_elements_set(:fresh_food)
      eset = eset_for($1)
      PageObjectWrapper.current_result = feed_elements(eset.elements, *args)
    when (FEED.match(method_name) and has_element?($1))
      # page_object.feed_some_element(:fresh_food)
      e = element_for($1)
      if [true, false].include? args[0] or args[0].is_a? String 
        PageObjectWrapper.current_result = feed_field(e, args[0])
      else
        PageObjectWrapper.current_result = feed_elements([e], *args)
      end
    when (FIRE_ACTION.match(method_name) and has_action?($1))
      # page_object.fire_some_action
      a = action_for($1)
      PageObjectWrapper.current_result = fire_action(a, *args)
    when (FIRE_ACTION.match(method_name) and has_alias?($1))
      # page_object.fire_some_action
      a = alias_for($1)
      PageObjectWrapper.current_result = fire_action(a, *args)
    when (VALIDATE.match(method_name) and has_validator?($1))
      # page_object.validate_something
      v = validator_for($1)
      PageObjectWrapper.current_result = run_validator(v, *args)
    when (SELECT_FROM.match(method_name) and has_table?($1))
      # page_object.select_from_some_table(:header_column, {:column => 'value'})
      table = table_for($1)
      PageObjectWrapper.current_result = select_from(table, *args)
    when (SELECT_ROW_FROM.match(method_name) and has_table?($1))
      # page_object.select_row_from_some_table(:number => 1, :column1 => value1, :column2 => value3, ...)
      table = table_for($1)
      PageObjectWrapper.current_result = select_row_from(table, args[0])
    when (PAGINATION_EACH.match(method_name) and has_pagination?($1))
      # page_object.each_pagination
      pagination = pagination_for($1)
      PageObjectWrapper.current_result = run_each_subpage(pagination, *args, &block)
    when (PAGINATION_OPEN.match(method_name) and has_pagination?($1))
      # page_object.open_padination(1)
      pagination = pagination_for($1)
      PageObjectWrapper.current_result = open_subpage(pagination, *args)
    when (PRESS.match(method_name) and has_element?($1))
      # page_object.press_element
      element = element_for($1)
      PageObjectWrapper.current_result = press(element)
    else
      super
  end
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def actions
  @actions
end

#aliasesObject (readonly)

Returns the value of attribute aliases.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def aliases
  @aliases
end

#elementsObject (readonly)

Returns the value of attribute elements.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def elements
  @elements
end

#esetsObject (readonly)

Returns the value of attribute esets.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def esets
  @esets
end

#paginationsObject (readonly)

Returns the value of attribute paginations.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def paginations
  @paginations
end

#tablesObject (readonly)

Returns the value of attribute tables.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def tables
  @tables
end

#uniq_element_hashObject (readonly)

Returns the value of attribute uniq_element_hash.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def uniq_element_hash
  @uniq_element_hash
end

#uniq_element_typeObject (readonly)

Returns the value of attribute uniq_element_type.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def uniq_element_type
  @uniq_element_type
end

#validatorsObject (readonly)

Returns the value of attribute validators.



17
18
19
# File 'lib/page_object_wrapper/PageObject.rb', line 17

def validators
  @validators
end

Class Method Details

.map_current_page(label) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/page_object_wrapper/PageObject.rb', line 187

def self.map_current_page label
  raise PageObjectWrapper::BrowserNotFound if PageObjectWrapper.browser.nil?
  page_object = PageObject.find_page_object(label)
  page_object.elements.select{ |e| e.required_value == true }.each{ |required_element|
    begin
      watir_element = return_watir_element required_element
      watir_element.wait_until_present REQUIRED_ELEMENT_WAIT_PERIOD
    rescue Watir::Wait::TimeoutError => e
      raise PageObjectWrapper::UnmappedPageObject, "#{label} <=> #{PageObjectWrapper.browser.url} (#{e.message})" if not watir_element.present?
    end
  }
  PageObjectWrapper.current_page = page_object
end

.open_page(label, optional_hash = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/page_object_wrapper/PageObject.rb', line 170

def self.open_page label, optional_hash=nil
  raise PageObjectWrapper::BrowserNotFound if PageObjectWrapper.browser.nil?
  page_object = PageObject.find_page_object(label)
  url = ''
  url += PageObjectWrapper.domain if page_object.locator_value[0]=='/'
  url += page_object.locator_value
  if not (optional_hash.nil? or optional_hash.empty?)
    optional_hash.each{|k,v|
      raise ArgumentError, "#{k.inspect} not Symbol" if not k.is_a? Symbol
      raise ArgumentError, "#{v.inspect} not meaningful String" if not v.is_a? String or v.empty?
      raise PageObjectWrapper::DynamicUrl, "#{k.inspect} not known parameter" if not url.match(':'+k.to_s)
      url.gsub!(/:#{k.to_s}/, v)
    }
  end
  PageObjectWrapper.browser.goto url
end

.pagesObject



201
202
203
# File 'lib/page_object_wrapper/PageObject.rb', line 201

def self.pages
  @@pages
end

Instance Method Details

#action(label, next_page = nil, &block) ⇒ Object



218
219
220
221
222
# File 'lib/page_object_wrapper/PageObject.rb', line 218

def action(label, next_page=nil, &block)
  a = Action.new(label, next_page, &block)
  @actions << a
  a
end

#action_alias(label, next_page = nil, &block) ⇒ Object



224
225
226
227
228
229
# File 'lib/page_object_wrapper/PageObject.rb', line 224

def action_alias(label, next_page=nil, &block)
  a = Alias.new(label, next_page)
  a.instance_eval(&block)
  @aliases << a
  a
end

#elements_set(label, &block) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/page_object_wrapper/PageObject.rb', line 205

def elements_set(label, &block)
  eset = ElementsSet.new(label)
  eset.instance_eval(&block)
  @esets << eset
  eset.elements.each{|e|
    PageObject.send :define_method, (e.label_value.to_s+'_menu').to_sym do |food_type|
      e.menu_value[food_type].to_s
    end
  }
  @elements += eset.elements
  eset
end

#pagination(label, &block) ⇒ Object



246
247
248
249
250
251
# File 'lib/page_object_wrapper/PageObject.rb', line 246

def pagination(label, &block)
  p = Pagination.new(label)
  p.instance_eval(&block)
  @paginations << p
  p
end

#respond_to?(method_sym, include_private = false) ⇒ Boolean

corresponding respond_to?

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/page_object_wrapper/PageObject.rb', line 120

def respond_to?(method_sym, include_private = false)
  method_name = method_sym.to_s
  case 
    when KNOWN_ELEMENTS.include?(method_name.gsub(/^uniq_/,''))
      # page_object.uniq_xxx(hash)
      true
    when has_eset?(method_name)
      # page_object.some_elements_set
      true
    when has_element?(method_name)
      # page_object.some_element
      true
    when FEED_ALL.match(method_name)
      # page_object.feed_all(:fresh_food)
      true
    when (FEED.match(method_name) and has_eset?($1))
      # page_object.feed_some_elements_set(:fresh_food)
      true
    when (FEED.match(method_name) and has_element?($1))
      # page_object.feed_some_element(:fresh_food)
      true
    when (FIRE_ACTION.match(method_name) and has_action?($1))
      # page_object.fire_some_action
      true
    when (FIRE_ACTION.match(method_name) and has_alias?($1))
      # page_object.fire_some_action
      true
    when (VALIDATE.match(method_name) and has_action?($1))
      # page_object.validate_xxx
      true
    when (SELECT_FROM.match(method_name) and has_table?($1))
      # page_object.select_from_some_table(:header_column, {:column => 'value'})
      true
    when (SELECT_ROW_FROM.match(method_name) and has_table?($1))
      # page_object.select_from_some_table(:header_column, {:column => 'value'})
      true
    when (PAGINATION_EACH.match(method_name) and has_pagination?($1))
      # page_object.each_pagination
      true
    when (PAGINATION_OPEN.match(method_name) and has_pagination?($1))
      # page_object.open_padination(1)
      true
    when (PRESS.match(method_name) and has_element?($1))
      # page_object.press_element
      true
    else
      super
  end
end

#table(label, &block) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/page_object_wrapper/PageObject.rb', line 237

def table(label, &block)
  t = Table.new(label)
  t.instance_eval(&block)
  @tables << t
  @elements << t
  t
end

#validateObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/page_object_wrapper/PageObject.rb', line 253

def validate
  output = []
  # commented out; already defined pages will e redifined with new definitions
  raise PageObjectWrapper::Load, "\tpage_object #{label_value.inspect} already defined\n" if PageObject.labeled(@@pages).count(label_value) > 1
  output << "\tlabel #{label_value.inspect} not a Symbol\n" if not label_value.is_a?(Symbol)
  output << "\tlabel aliases #{label_alias_value.inspect} not an Array of Symbols\n" if (not label_alias_value.empty?) and label_alias_value.collect(&:class).uniq != [Symbol]
  output << "\tlocator #{locator_value.inspect} not a meaningful String\n" if not locator_value.is_a?(String) or locator_value.empty?
  @esets.each{|eset|
    eset_output = []
    eset_output << "\telements_set #{eset.label_value.inspect} already defined\n" if PageObject.labeled(@esets).count(eset.label_value) > 1
    eset_output << "\tlabel #{eset.label_value.inspect} not a Symbol\n" if not eset.label_value.is_a?(Symbol)
    eset_output << "\tlabel aliases #{eset.label_alias_value.inspect} not an Array of Symbols\n" if (not eset.label_alias_value.empty?) and eset.label_alias_value.collect(&:class).uniq != [Symbol]
    eset_output.unshift "elements_set(#{eset.label_value.inspect}):\n" if not eset_output.empty?
    output += eset_output
  }
  @elements.each{|e|
    element_output = []
    element_output << "\telement #{e.label_value.inspect} already defined\n" if PageObject.labeled(@elements).count(e.label_value) > 1
    element_output << "\tlabel #{e.label_value.inspect} not a Symbol\n" if not e.label_value.is_a?(Symbol)
    element_output << "\tlabel aliases #{e.label_alias_value.inspect} not an Array of Symbols\n" if (not e.label_alias_value.empty?) and e.label_alias_value.collect(&:class).uniq != [Symbol]
    element_output << "\tlocator #{e.locator_value.inspect} not a meaningful Hash or String\n" if (not e.locator_value.is_a?(Hash) and not e.locator_value.is_a?(String)) \
                                                                                                  or e.locator_value.empty?
    element_output << "\tmenu #{e.menu_value.inspect} not properly defined (must be { :food_type => 'a string' | true | false })\n" if (not e.menu_value.empty?) and \
                                                                                                                                      ((e.menu_value.keys.collect(&:class).uniq != [Symbol]) \
                                                                                                                                       or not (e.menu_value.values.collect(&:class).uniq - [String, TrueClass, FalseClass]).empty?)
    element_output << "\trequired flag #{e.required_value.inspect} not a true | false\n" if not [true, false].include? e.required_value
    element_output.unshift "element(#{e.label_value.inspect}):\n" if not element_output.empty?
    output += element_output       
  }
  @actions.each{|a|
    action_output = []
    action_output << "\taction #{a.label_value.inspect} already defined\n" if PageObject.labeled(@actions).count(a.label_value) > 1
    action_output << "\tlabel #{a.label_value.inspect} not a Symbol\n" if not a.label_value.is_a?(Symbol)
    action_output << "\tlabel aliases #{a.label_alias_value.inspect} not an Array of Symbols\n" if (not a.label_alias_value.empty?) and a.label_alias_value.collect(&:class).uniq != [Symbol]
    if not a.next_page_value.nil?
      action_output << "\tnext_page #{a.next_page_value.inspect} not a Symbol\n" if not a.next_page_value.is_a? Symbol
      action_output << "\tnext_page #{a.next_page_value.inspect} unknown page_object\n" if not PageObject.labeled(@@pages).include?(a.next_page_value)
    end
    action_output << "\tfire event is not a Proc\n" if not a.fire_block_value.is_a?(Proc)
    action_output.unshift "action(#{a.label_value.inspect}):\n" if not action_output.empty?
    output += action_output
  }
  @aliases.each{|a|
    alias_output = []
    alias_output << "\talias #{a.label_value.inspect} already defined\n" if PageObject.labeled(@aliases).count(a.label_value) > 1
    alias_output << "\tlabel #{a.label_value.inspect} not a Symbol\n" if not a.label_value.is_a?(Symbol)
    alias_output << "\tlabel aliases #{a.label_alias_value.inspect} not an Array of Symbols\n" if (not a.label_alias_value.empty?) and a.label_alias_value.collect(&:class).uniq != [Symbol]
    if not a.next_page_value.nil?
      alias_output << "\tnext_page #{a.next_page_value.inspect} not a Symbol\n" if not a.next_page_value.is_a? Symbol
      alias_output << "\tnext_page #{a.next_page_value.inspect} unknown page_object\n" if not PageObject.labeled(@@pages).include?(a.next_page_value)
    end
    alias_output << "\taction #{a.action_value.inspect} not known Action\n" if not PageObject.labeled(@actions).include? a.action_value
    alias_output.unshift "alias(#{a.label_value.inspect}):\n" if not alias_output.empty?
    output += alias_output
  }
  @validators.each{|v|
    validator_output = []
    validator_output << "\tvalidator #{v.label_value.inspect} already defined\n" if PageObject.labeled(@validators).count(v.label_value) > 1
    validator_output << "\tlabel #{v.label_value.inspect} not a Symbol\n" if not v.label_value.is_a?(Symbol)
    validator_output << "\tlabel aliases #{v.label_alias_value.inspect} not an Array of Symbols\n" if (not v.label_alias_value.empty?) and v.label_alias_value.collect(&:class).uniq != [Symbol]
    validator_output << "\tvalidation block is not a Proc\n" if not v.validate_block_value.is_a?(Proc)
    validator_output.unshift "validator(#{v.label_value.inspect}):\n" if not validator_output.empty?
    output += validator_output
  }
  @tables.each{|t|
    table_output = []
    table_output << "\ttable #{t.label_value.inspect} already defined\n" if PageObject.labeled(@tables).count(t.label_value) > 1
    table_output << "\tlabel #{t.label_value.inspect} not a Symbol\n" if not t.label_value.is_a?(Symbol)
    table_output << "\tlabel aliases #{t.label_alias_value.inspect} not an Array of Symbols\n" if (not t.label_alias_value.empty?) and t.label_alias_value.collect(&:class).uniq != [Symbol]
    table_output << "\theader #{t.header_value.inspect} not a meaningful Array\n" if not t.header_value.is_a?(Array) or t.header_value.empty?
    table_output.unshift "table(#{t.label_value.inspect}):\n" if not table_output.empty?
    output += table_output
  }
  @paginations.each{|p|
    pagination_output = []
    pagination_output << "\tpagination #{p.label_value.inspect} already defined\n" if PageObject.labeled(@paginations).count(p.label_value) > 1
    pagination_output << "\tlabel #{p.label_value.inspect} not a Symbol\n" if not p.label_value.is_a?(Symbol)
    pagination_output << "\tlabel aliases #{p.label_alias_value.inspect} not an Array of Symbols\n" if (not p.label_alias_value.empty?) and p.label_alias_value.collect(&:class).uniq != [Symbol]
    pagination_output << "\tlocator #{p.locator_value.inspect} not a meaningful String\n" if not p.locator_value.is_a?(String) or p.locator_value.empty?
    pagination_output << "\t\"#{p.finds_value}\" not found in #{p.locator_value}\n" if not p.locator_value =~ /#{p.finds_value.to_s}/
    pagination_output.unshift "pagination(#{p.label_value.inspect}):\n" if not pagination_output.empty?
    output += pagination_output
  }
  output.unshift "page_object(#{label_value.inspect}):\n" if not output.empty?
  output
end

#validator(label, &block) ⇒ Object



231
232
233
234
235
# File 'lib/page_object_wrapper/PageObject.rb', line 231

def validator(label, &block)
  v = Validator.new(label, &block)
  @validators << v
  v
end