page-object 2.0.0 → 2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -2
  3. data/ChangeLog +10 -0
  4. data/README.md +12 -0
  5. data/Rakefile +2 -12
  6. data/cucumber.yml +2 -4
  7. data/lib/page-object.rb +12 -20
  8. data/lib/page-object/accessors.rb +50 -363
  9. data/lib/page-object/elements/bold.rb +0 -1
  10. data/lib/page-object/elements/button.rb +0 -4
  11. data/lib/page-object/elements/check_box.rb +19 -7
  12. data/lib/page-object/elements/div.rb +0 -4
  13. data/lib/page-object/elements/element.rb +159 -90
  14. data/lib/page-object/elements/file_field.rb +5 -7
  15. data/lib/page-object/elements/form.rb +0 -8
  16. data/lib/page-object/elements/hidden_field.rb +1 -4
  17. data/lib/page-object/elements/image.rb +13 -7
  18. data/lib/page-object/elements/label.rb +0 -4
  19. data/lib/page-object/elements/link.rb +0 -13
  20. data/lib/page-object/elements/list_item.rb +0 -3
  21. data/lib/page-object/elements/ordered_list.rb +30 -5
  22. data/lib/page-object/elements/radio_button.rb +12 -7
  23. data/lib/page-object/elements/select_list.rb +40 -8
  24. data/lib/page-object/elements/span.rb +0 -3
  25. data/lib/page-object/elements/table.rb +26 -5
  26. data/lib/page-object/elements/table_cell.rb +0 -4
  27. data/lib/page-object/elements/table_row.rb +30 -5
  28. data/lib/page-object/elements/text_area.rb +7 -7
  29. data/lib/page-object/elements/text_field.rb +5 -11
  30. data/lib/page-object/elements/unordered_list.rb +30 -5
  31. data/lib/page-object/indexed_properties.rb +1 -0
  32. data/lib/page-object/platforms.rb +0 -1
  33. data/lib/page-object/platforms/watir.rb +26 -4
  34. data/lib/page-object/platforms/watir/page_object.rb +4 -4
  35. data/lib/page-object/version.rb +1 -1
  36. data/lib/page-object/widgets.rb +0 -1
  37. data/page-object.gemspec +1 -15
  38. metadata +5 -47
  39. data/lib/page-object/platforms/selenium_webdriver.rb +0 -30
  40. data/lib/page-object/platforms/selenium_webdriver/button.rb +0 -15
  41. data/lib/page-object/platforms/selenium_webdriver/check_box.rb +0 -29
  42. data/lib/page-object/platforms/selenium_webdriver/element.rb +0 -335
  43. data/lib/page-object/platforms/selenium_webdriver/file_field.rb +0 -16
  44. data/lib/page-object/platforms/selenium_webdriver/form.rb +0 -16
  45. data/lib/page-object/platforms/selenium_webdriver/image.rb +0 -28
  46. data/lib/page-object/platforms/selenium_webdriver/link.rb +0 -23
  47. data/lib/page-object/platforms/selenium_webdriver/ordered_list.rb +0 -41
  48. data/lib/page-object/platforms/selenium_webdriver/page_object.rb +0 -1297
  49. data/lib/page-object/platforms/selenium_webdriver/radio_button.rb +0 -22
  50. data/lib/page-object/platforms/selenium_webdriver/select_list.rb +0 -93
  51. data/lib/page-object/platforms/selenium_webdriver/surrogate_selenium_element.rb +0 -42
  52. data/lib/page-object/platforms/selenium_webdriver/table.rb +0 -42
  53. data/lib/page-object/platforms/selenium_webdriver/table_row.rb +0 -43
  54. data/lib/page-object/platforms/selenium_webdriver/text_area.rb +0 -17
  55. data/lib/page-object/platforms/selenium_webdriver/text_field.rb +0 -17
  56. data/lib/page-object/platforms/selenium_webdriver/unordered_list.rb +0 -37
  57. data/lib/page-object/platforms/watir/check_box.rb +0 -29
  58. data/lib/page-object/platforms/watir/element.rb +0 -295
  59. data/lib/page-object/platforms/watir/file_field.rb +0 -16
  60. data/lib/page-object/platforms/watir/form.rb +0 -16
  61. data/lib/page-object/platforms/watir/image.rb +0 -22
  62. data/lib/page-object/platforms/watir/link.rb +0 -15
  63. data/lib/page-object/platforms/watir/ordered_list.rb +0 -40
  64. data/lib/page-object/platforms/watir/radio_button.rb +0 -22
  65. data/lib/page-object/platforms/watir/select_list.rb +0 -74
  66. data/lib/page-object/platforms/watir/table.rb +0 -38
  67. data/lib/page-object/platforms/watir/table_row.rb +0 -37
  68. data/lib/page-object/platforms/watir/text_area.rb +0 -23
  69. data/lib/page-object/platforms/watir/text_field.rb +0 -16
  70. data/lib/page-object/platforms/watir/unordered_list.rb +0 -42
@@ -1,16 +0,0 @@
1
- module PageObject
2
- module Platforms
3
- module SeleniumWebDriver
4
-
5
- module FileField
6
-
7
- #
8
- # Set the value of the FileField
9
- #
10
- def value=(new_value)
11
- element.send_keys(new_value)
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,16 +0,0 @@
1
- module PageObject
2
- module Platforms
3
- module SeleniumWebDriver
4
- module Form
5
-
6
- #
7
- # Submit the form.
8
- #
9
- def submit
10
- element.submit
11
- end
12
-
13
- end
14
- end
15
- end
16
- end
@@ -1,28 +0,0 @@
1
- module PageObject
2
- module Platforms
3
- module SeleniumWebDriver
4
- module Image
5
-
6
- #
7
- # Return the width of the image.
8
- #
9
- def width
10
- dimension.width
11
- end
12
-
13
- #
14
- # Return the height of the image
15
- #
16
- def height
17
- dimension.height
18
- end
19
-
20
- private
21
-
22
- def dimension
23
- element.size
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,23 +0,0 @@
1
- module PageObject
2
- module Platforms
3
- module SeleniumWebDriver
4
- module Link
5
- #
6
- # Override PageObject::PLatforms::SeleniumElement#value because
7
- # #value is not available on links in Selenium.
8
- #
9
- def value
10
- raise "value not available on link element with Selenium"
11
- end
12
-
13
- #
14
- # return the href for the link
15
- #
16
- def href
17
- attribute('href')
18
- end
19
- end
20
- end
21
- end
22
-
23
- end
@@ -1,41 +0,0 @@
1
- module PageObject
2
- module Platforms
3
- module SeleniumWebDriver
4
- module OrderedList
5
-
6
- #
7
- # Return the PageObject::Elements::ListItem for the index provided. Index
8
- # is zero based.
9
- #
10
- # @return [PageObject::Elements::ListItem]
11
- #
12
- def [](idx)
13
- Object::PageObject::Elements::ListItem.new(children[idx], :platform => :selenium_webdriver)
14
- end
15
-
16
- #
17
- # Return the number of items contained in the ordered list
18
- #
19
- def items
20
- children.size
21
- end
22
-
23
- #
24
- # Return the ListItem objects that are children of the OrderedList
25
- #
26
- def list_items
27
- children.collect do |obj|
28
- Object::PageObject::Elements::ListItem.new(obj, :platform => :selenium_webdriver)
29
- end
30
- end
31
-
32
- private
33
-
34
- def children
35
- element.find_elements(:xpath, child_xpath)
36
- end
37
-
38
- end
39
- end
40
- end
41
- end
@@ -1,1297 +0,0 @@
1
- require 'page-object/elements'
2
- require 'page-object/core_ext/string'
3
- require 'page-object/platforms/selenium_webdriver/surrogate_selenium_element'
4
-
5
- module PageObject
6
- module Platforms
7
- module SeleniumWebDriver
8
-
9
- #
10
- # Selenium implementation of the page object platform driver. You should not use the
11
- # class directly. Instead you should include the PageObject module in your page object
12
- # and use the methods dynamically added from the PageObject::Accessors module.
13
- #
14
- class PageObject
15
-
16
- PLATFORM_NAME = :selenium_webdriver
17
-
18
- def self.define_widget_accessors(widget_tag, widget_class, base_element_tag)
19
- define_widget_singular_accessor(base_element_tag, widget_class, widget_tag)
20
- define_widget_multiple_accessor(base_element_tag, widget_class, widget_tag)
21
- end
22
-
23
- def initialize(browser)
24
- @browser = browser
25
- end
26
-
27
- #
28
- # platform method to navigate to a provided url
29
- # See PageObject#navigate_to
30
- #
31
- def navigate_to(url)
32
- @browser.navigate.to url
33
- end
34
-
35
- #
36
- # platform method to get the current url
37
- # See PageObject#current_url
38
- #
39
- def current_url
40
- @browser.current_url
41
- end
42
-
43
- #
44
- # platform method to retrieve the text from the current page
45
- # See PageObject#text
46
- #
47
- def text
48
- @browser.find_element(:tag_name, 'body').text
49
- end
50
-
51
- #
52
- # platform method to retrieve the html for the current page
53
- # See PageObject#html
54
- #
55
- def html
56
- @browser.page_source
57
- end
58
-
59
- #
60
- # platform method to retrieve the title for the current page
61
- # See PageObject#title
62
- #
63
- def title
64
- @browser.title
65
- end
66
-
67
- #
68
- # platform method to wait for a block to return true
69
- # See PageObject#wait_until
70
- #
71
- def wait_until(timeout, message = nil, &block)
72
- wait = ::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => message})
73
- wait.until &block
74
- end
75
-
76
- #
77
- # platform method to handle an alert popup
78
- # See PageObject#alert
79
- #
80
- def alert(frame=nil, &block)
81
- yield
82
- begin
83
- alert = @browser.switch_to.alert
84
- value = alert.text
85
- alert.accept
86
- rescue Selenium::WebDriver::Error::NoAlertPresentError
87
- end
88
- value
89
- end
90
-
91
- #
92
- # platform method to handle a confirm popup
93
- # See PageObject#confirm
94
- #
95
- def confirm(response, frame=nil, &block)
96
- yield
97
- begin
98
- alert = @browser.switch_to.alert
99
- value = alert.text
100
- response ? alert.accept : alert.dismiss
101
- rescue Selenium::WebDriver::Error::NoAlertPresentError
102
- end
103
- value
104
- end
105
-
106
- #
107
- # platform method to handle a prompt popup
108
- # See PageObject#prompt
109
- #
110
- def prompt(answer, frame=nil, &block)
111
- @browser.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{answer}; }"
112
- yield
113
- result = @browser.execute_script "return window.__lastWatirPrompt"
114
- result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
115
- result
116
- end
117
-
118
- #
119
- # platform method to execute javascript on the browser
120
- # See PageObject#execute_script
121
- #
122
- def execute_script(script, *args)
123
- @browser.execute_script(script, *args)
124
- end
125
-
126
- #
127
- # platform method to handle attaching to a running window
128
- # See PageObject#attach_to_window
129
- #
130
- def attach_to_window(identifier, &block)
131
- value = identifier.values.first
132
- key = identifier.keys.first
133
- handles = @browser.window_handles
134
- handles.each do |handle|
135
- @browser.switch_to.window handle
136
- if (key == :title and value == @browser.title) or
137
- (key == :url and @browser.current_url.include?(value))
138
- return @browser.switch_to.window handle, &block
139
- end
140
- end
141
- end
142
-
143
- #
144
- # find the element that has focus
145
- #
146
- def element_with_focus
147
- element = @browser.execute_script("return document.activeElement")
148
- type = element.attribute(:type).to_s.downcase if element.tag_name.to_sym == :input
149
- cls = ::PageObject::Elements.element_class_for(element.tag_name, type)
150
- cls.new(element, :platform => self.class::PLATFORM_NAME)
151
- end
152
-
153
- #
154
- # platform method to switch to a frame and execute a block
155
- # See PageObject#in_frame
156
- #
157
- def in_frame(identifier, frame=nil, &block)
158
- switch_to_frame([frame: identifier])
159
- block.call(nil)
160
- @browser.switch_to.default_content
161
- end
162
-
163
- #
164
- # platform method to switch to an iframe and execute a block
165
- # See PageObject#in_frame
166
- #
167
- def in_iframe(identifier, frame=nil, &block)
168
- switch_to_frame([iframe: identifier])
169
- block.call(nil)
170
- @browser.switch_to.default_content
171
- end
172
-
173
- #
174
- # platform method to refresh the page
175
- # See PageObject#refresh
176
- #
177
- def refresh
178
- @browser.navigate.refresh
179
- end
180
-
181
- #
182
- # platform method to go back to the previous page
183
- # See PageObject#back
184
- #
185
- def back
186
- @browser.navigate.back
187
- end
188
-
189
- #
190
- # platform method to go forward to the next page
191
- # See PageObject#forward
192
- #
193
- def forward
194
- @browser.navigate.forward
195
- end
196
-
197
- #
198
- # platform method to clear the cookies from the browser
199
- # See PageObject#clear_cookies
200
- #
201
- def clear_cookies
202
- @browser.manage.delete_all_cookies
203
- end
204
-
205
- #
206
- # platform method to save the current screenshot to a file
207
- # See PageObject#save_screenshot
208
- #
209
- def save_screenshot(file_name)
210
- @browser.save_screenshot(file_name)
211
- end
212
-
213
- #
214
- # platform method to get the value stored in a text field
215
- # See PageObject::Accessors#text_field
216
- #
217
- def text_field_value_for(identifier)
218
- process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|
219
- @browser.find_element(how, what).attribute('value')
220
- end
221
- end
222
-
223
-
224
- #
225
- # platform method to set the value for a text field
226
- # See PageObject::Accessors#text_field
227
- #
228
- def text_field_value_set(identifier, value)
229
- process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|
230
- @browser.find_element(how, what).clear
231
- @browser.find_element(how, what).send_keys(value)
232
- end
233
- end
234
-
235
- #
236
- # platform method to retrieve a text field element
237
- # See PageObject::Accessors#text_field
238
- #
239
- def text_field_for(identifier)
240
- find_selenium_element(identifier, Elements::TextField, 'input', :type => 'text')
241
- end
242
-
243
- #
244
- # platform method to retrieve all text field elements
245
- #
246
- def text_fields_for(identifier)
247
- find_selenium_elements(identifier, Elements::TextField, 'input', :type => 'text')
248
- end
249
-
250
- #
251
- # platform method to get the value stored in a hidden field
252
- # See PageObject::Accessors#hidden_field
253
- #
254
- def hidden_field_value_for(identifier)
255
- process_selenium_call(identifier, Elements::HiddenField, 'input', :type => 'hidden') do |how, what|
256
- @browser.find_element(how, what).attribute('value')
257
- end
258
- end
259
-
260
- #
261
- # platform method to retrieve a hidden field element
262
- # See PageObject::Accessors#hidden_field
263
- #
264
- def hidden_field_for(identifier)
265
- find_selenium_element(identifier, Elements::HiddenField, 'input', :type => 'hidden')
266
- end
267
-
268
- #
269
- # platform method to retrieve all hidden field elements
270
- #
271
- def hidden_fields_for(identifier)
272
- find_selenium_elements(identifier, Elements::HiddenField, 'input', :type => 'hidden')
273
- end
274
-
275
- #
276
- # platform method to set text in a textarea
277
- # See PageObject::Accessors#text_area
278
- #
279
- def text_area_value_set(identifier, value)
280
- process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|
281
- text_area = @browser.find_element(how, what)
282
- text_area.clear
283
- text_area.send_keys(value)
284
- end
285
- end
286
-
287
- #
288
- # platform method to get the text from a textarea
289
- # See PageObject::Accessors#text_area
290
- #
291
- def text_area_value_for(identifier)
292
- process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|
293
- @browser.find_element(how, what).attribute('value')
294
- end
295
- end
296
-
297
- #
298
- # platform method to get the text area element
299
- # See PageObject::Accessors#text_area
300
- #
301
- def text_area_for(identifier)
302
- find_selenium_element(identifier, Elements::TextArea, 'textarea')
303
- end
304
-
305
- #
306
- # platform method to retrieve all text area elements
307
- #
308
- def text_areas_for(identifier)
309
- find_selenium_elements(identifier, Elements::TextArea, 'textarea')
310
- end
311
-
312
- #
313
- # platform method to get the currently selected value from a select list
314
- # See PageObject::Accessors#select_list
315
- #
316
- def select_list_value_for(identifier)
317
- process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|
318
- selected = nil
319
- @browser.find_element(how, what).find_elements(:tag_name => 'option').each do |o|
320
- if selected.nil?
321
- selected = o.text if o.selected?
322
- end
323
- end
324
- selected
325
- end
326
- end
327
-
328
- #
329
- # platform method to select a value from a select list
330
- # See PageObject::Accessors#select_list
331
- #
332
- def select_list_value_set(identifier, value)
333
- process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|
334
- select_list = @browser.find_element(how, what)
335
- select_list.find_elements(:tag_name => 'option').find do |option|
336
- option.text == value
337
- end.click
338
- end
339
- end
340
-
341
- #
342
- # platform method to return the select list element
343
- # See PageObject::Accessors#select_list
344
- #
345
- def select_list_for(identifier)
346
- find_selenium_element(identifier, Elements::SelectList, 'select')
347
- end
348
-
349
- #
350
- # platform method to retrieve all select list elements
351
- #
352
- def select_lists_for(identifier)
353
- find_selenium_elements(identifier, Elements::SelectList, 'select')
354
- end
355
-
356
- #
357
- # platform method to click a link
358
- # See PageObject::Accessors#link
359
- #
360
- def click_link_for(identifier)
361
- process_selenium_call(identifier, Elements::Link, 'a') do |how, what|
362
- @browser.find_element(how, what).click
363
- end
364
- end
365
-
366
- #
367
- # platform method to return a PageObject::Elements::Link object
368
- # see PageObject::Accessors#link
369
- #
370
- def link_for(identifier)
371
- find_selenium_element(identifier, Elements::Link, 'a')
372
- end
373
-
374
- #
375
- # platform method to retrieve all link elements
376
- #
377
- def links_for(identifier)
378
- find_selenium_elements(identifier, Elements::Link, 'a')
379
- end
380
-
381
- #
382
- # platform method to check a checkbox
383
- # See PageObject::Accessors#checkbox
384
- #
385
- def check_checkbox(identifier)
386
- process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
387
- @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?
388
- end
389
- end
390
-
391
- #
392
- # platform method to uncheck a checkbox
393
- # See PageObject::Accessors#checkbox
394
- #
395
- def uncheck_checkbox(identifier)
396
- process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
397
- @browser.find_element(how, what).click if @browser.find_element(how, what).selected?
398
- end
399
- end
400
-
401
- #
402
- # platform method to determine if a checkbox is checked
403
- # See PageObject::Accessors#checkbox
404
- #
405
- def checkbox_checked?(identifier)
406
- process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
407
- @browser.find_element(how, what).selected?
408
- end
409
- end
410
-
411
- #
412
- # platform method to return a PageObject::Elements::CheckBox element
413
- # See PageObject::Accessors#checkbox
414
- #
415
- def checkbox_for(identifier)
416
- find_selenium_element(identifier, Elements::CheckBox, 'input', :type => 'checkbox')
417
- end
418
-
419
- #
420
- # platform method to retrieve all checkbox elements
421
- #
422
- def checkboxs_for(identifier)
423
- find_selenium_elements(identifier, Elements::CheckBox, 'input', :type => 'checkbox')
424
- end
425
-
426
- #
427
- # platform method to select a radio button
428
- # See PageObject::Accessors#radio_button
429
- #
430
- def select_radio(identifier)
431
- process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|
432
- @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?
433
- end
434
- end
435
-
436
- #
437
- # platform method to determine if a radio button is selected
438
- # See PageObject::Accessors#radio_button
439
- #
440
- def radio_selected?(identifier)
441
- process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|
442
- @browser.find_element(how, what).selected?
443
- end
444
- end
445
-
446
- #
447
- # platform method to return a PageObject::Eements::RadioButton element
448
- # See PageObject::Accessors#radio_button
449
- #
450
- def radio_button_for(identifier)
451
- find_selenium_element(identifier, Elements::RadioButton, 'input', :type => 'radio')
452
- end
453
-
454
- #
455
- # platform method to retrieve all radio button elements
456
- #
457
- def radio_buttons_for(identifier)
458
- find_selenium_elements(identifier, Elements::RadioButton, 'input', :type => 'radio')
459
- end
460
-
461
- #
462
- # platform method to return the text for a div
463
- # See PageObject::Accessors#div
464
- #
465
- def div_text_for(identifier)
466
- process_selenium_call(identifier, Elements::Div, 'div') do |how, what|
467
- @browser.find_element(how, what).text
468
- end
469
- end
470
-
471
- #
472
- # platform method to return a PageObject::Elements::Div element
473
- # See PageObject::Accessors#div
474
- #
475
- def div_for(identifier)
476
- find_selenium_element(identifier, Elements::Div, 'div')
477
- end
478
-
479
- #
480
- # platform method to retrieve all div elements
481
- #
482
- def divs_for(identifier)
483
- find_selenium_elements(identifier, Elements::Div, 'div')
484
- end
485
-
486
- #
487
- # platform method to return the text for a span
488
- # See PageObject::Accessors#span
489
- #
490
- def span_text_for(identifier)
491
- process_selenium_call(identifier, Elements::Span, 'span') do |how, what|
492
- @browser.find_element(how, what).text
493
- end
494
- end
495
-
496
- #
497
- # platform method to return a PageObject::Elements::Span element
498
- # See PageObject::Accessors#span
499
- #
500
- def span_for(identifier)
501
- find_selenium_element(identifier, Elements::Span, 'span')
502
- end
503
-
504
- #
505
- # platform method to retrieve all span elements
506
- #
507
- def spans_for(identifier)
508
- find_selenium_elements(identifier, Elements::Span, 'span')
509
- end
510
-
511
- #
512
- # platform method to click a button
513
- # See PageObject::Accessors#button
514
- #
515
- def click_button_for(identifier)
516
- process_selenium_call(identifier, Elements::Button, 'input', :type => 'submit') do |how, what|
517
- @browser.find_element(how, what).click
518
- end
519
- end
520
-
521
- #
522
- # platform method to retrieve a button element
523
- # See PageObject::Accessors#button
524
- #
525
- def button_for(identifier)
526
- find_selenium_element(identifier, Elements::Button, 'input', :type => 'submit')
527
- end
528
-
529
- #
530
- # platform method to retrieve an array of button elements
531
- #
532
- def buttons_for(identifier)
533
- find_selenium_elements(identifier, Elements::Button, 'input', :type => 'submit')
534
- end
535
-
536
- #
537
- # platform method to return the text for a table
538
- # See PageObject::Accessors#table
539
- #
540
- def table_text_for(identifier)
541
- process_selenium_call(identifier, Elements::Table, 'table') do |how, what|
542
- @browser.find_element(how, what).text
543
- end
544
- end
545
-
546
- #
547
- # platform method to retrieve a table element
548
- # See PageObject::Accessors#table
549
- #
550
- def table_for(identifier)
551
- find_selenium_element(identifier, Elements::Table, 'table')
552
- end
553
-
554
- #
555
- # platform method to retrieve all table elements
556
- #
557
- def tables_for(identifier)
558
- find_selenium_elements(identifier, Elements::Table, 'table')
559
- end
560
-
561
- #
562
- # platform method to retrieve the text from a table cell
563
- # See PageObject::Accessors#cell
564
- #
565
- def cell_text_for(identifier)
566
- process_selenium_call(identifier, Elements::TableCell, 'td') do |how, what|
567
- @browser.find_element(how, what).text
568
- end
569
- end
570
-
571
- #
572
- # platform method to retrieve a table cell element
573
- # See PageObject::Accessors#cell
574
- #
575
- def cell_for(identifier)
576
- find_selenium_element(identifier, Elements::TableCell, 'td')
577
- end
578
-
579
- #
580
- # platform method to retrieve all table cell elements
581
- #
582
- def cells_for(identifier)
583
- find_selenium_elements(identifier, Elements::TableCell, 'td')
584
- end
585
-
586
- #
587
- # platform method to retrieve the text from a table row
588
- # See PageObject::Accessors#row
589
- #
590
- def row_text_for(identifier)
591
- process_selenium_call(identifier, Elements::TableRow, 'tr') do |how, what|
592
- @browser.find_element(how, what).text
593
- end
594
- end
595
-
596
- #
597
- # platform method to retrieve a table row element
598
- # See PageObject::Accessors#row
599
- #
600
- def row_for(identifier)
601
- find_selenium_element(identifier, Elements::TableRow, 'tr')
602
- end
603
-
604
- #
605
- # platform method to retrieve all table row elements
606
- #
607
- def rows_for(identifier)
608
- find_selenium_elements(identifier, Elements::TableRow, 'tr')
609
- end
610
-
611
- #
612
- # platform method to retrieve load status of an image element
613
- # See PageObject::Accessors#image
614
- #
615
- def image_loaded_for(identifier)
616
- process_selenium_call(identifier, Elements::Image, 'img') do |how, what|
617
- element = @browser.find_element(how, what)
618
- @browser.execute_script(
619
- 'return typeof arguments[0].naturalWidth != "undefined" && arguments[0].naturalWidth > 0',
620
- element
621
- )
622
- end
623
- end
624
-
625
- #
626
- # platform method to retrieve an image element
627
- # See PageObject::Accessors#image
628
- #
629
- def image_for(identifier)
630
- find_selenium_element(identifier, Elements::Image, 'img')
631
- end
632
-
633
- #
634
- # platform method to retrieve all image elements
635
- #
636
- def images_for(identifier)
637
- find_selenium_elements(identifier, Elements::Image, 'img')
638
- end
639
-
640
- #
641
- # platform method to retrieve a form element
642
- # See PageObject::Accessors#form
643
- #
644
- def form_for(identifier)
645
- find_selenium_element(identifier, Elements::Form, 'form')
646
- end
647
-
648
- #
649
- # platform method to retrieve all forms
650
- #
651
- def forms_for(identifier)
652
- find_selenium_elements(identifier, Elements::Form, 'form')
653
- end
654
-
655
- #
656
- # platform method to retrieve the text from a list item
657
- # See PageObject::Accessors#list_item
658
- #
659
- def list_item_text_for(identifier)
660
- process_selenium_call(identifier, Elements::ListItem, 'li') do |how, what|
661
- @browser.find_element(how, what).text
662
- end
663
- end
664
-
665
- #
666
- # platform method to retrieve a list item element
667
- # See PageObject::Accessors#list_item
668
- #
669
- def list_item_for(identifier)
670
- find_selenium_element(identifier, Elements::ListItem, 'li')
671
- end
672
-
673
- #
674
- # platform method to retrieve all list items
675
- #
676
- def list_items_for(identifier)
677
- find_selenium_elements(identifier, Elements::ListItem, 'li')
678
- end
679
-
680
- #
681
- # platform method to retrieve the text from an unordered list
682
- # See PageObject::Accessors#unordered_list
683
- #
684
- def unordered_list_text_for(identifier)
685
- process_selenium_call(identifier, Elements::UnorderedList, 'ul') do |how, what|
686
- @browser.find_element(how, what).text
687
- end
688
- end
689
-
690
- #
691
- # platform method to retrieve an unordered list element
692
- # See PageObject::Accessors#unordered_list
693
- #
694
- def unordered_list_for(identifier)
695
- find_selenium_element(identifier, Elements::UnorderedList, 'ul')
696
- end
697
-
698
- #
699
- # platform method to retrieve all unordered lists
700
- #
701
- def unordered_lists_for(identifier)
702
- find_selenium_elements(identifier, Elements::UnorderedList, 'ul')
703
- end
704
-
705
- #
706
- # platform method to retrieve the text from an ordered list
707
- # See PageObject::Accessors#ordered_list
708
- #
709
- def ordered_list_text_for(identifier)
710
- process_selenium_call(identifier, Elements::OrderedList, 'ol') do |how, what|
711
- @browser.find_element(how, what).text
712
- end
713
- end
714
-
715
- #
716
- # platform method to retrieve an ordered list element
717
- # See PageObject::Accessors#ordered_list
718
- #
719
- def ordered_list_for(identifier)
720
- find_selenium_element(identifier, Elements::OrderedList, 'ol')
721
- end
722
-
723
- #
724
- # platform method to retrieve all ordered lists
725
- #
726
- def ordered_lists_for(identifier)
727
- find_selenium_elements(identifier, Elements::OrderedList, 'ol')
728
- end
729
-
730
- #
731
- # platform method to retrieve the text from a h1
732
- # See PageObject::Accessors#h1
733
- #
734
- def h1_text_for(identifier)
735
- process_selenium_call(identifier, Elements::Heading, 'h1') do |how, what|
736
- @browser.find_element(how, what).text
737
- end
738
- end
739
-
740
- #
741
- # platform method to retrieve the h1 element
742
- # See PageObject::Accessors#h1
743
- #
744
- def h1_for(identifier)
745
- find_selenium_element(identifier, Elements::Heading, 'h1')
746
- end
747
-
748
- #
749
- # platform method to retrieve all h1 elements
750
- #
751
- def h1s_for(identifier)
752
- find_selenium_elements(identifier, Elements::Heading, 'h1')
753
- end
754
-
755
- #
756
- # platform method to retrieve the text from a h2
757
- # See PageObject::Accessors#h2
758
- #
759
- def h2_text_for(identifier)
760
- process_selenium_call(identifier, Elements::Heading, 'h2') do |how, what|
761
- @browser.find_element(how, what).text
762
- end
763
- end
764
-
765
- #
766
- # platform method to retrieve the h2 element
767
- # See PageObject::Accessors#h2
768
- #
769
- def h2_for(identifier)
770
- find_selenium_element(identifier, Elements::Heading, 'h2')
771
- end
772
-
773
- #
774
- # platform method to retrieve all h2 elements
775
- #
776
- def h2s_for(identifier)
777
- find_selenium_elements(identifier, Elements::Heading, 'h2')
778
- end
779
-
780
- #
781
- # platform method to retrieve the text from a h3
782
- # See PageObject::Accessors#h3
783
- #
784
- def h3_text_for(identifier)
785
- process_selenium_call(identifier, Elements::Heading, 'h3') do |how, what|
786
- @browser.find_element(how, what).text
787
- end
788
- end
789
-
790
- #
791
- # platform method to retrieve the h3 element
792
- # See PageObject::Accessors#h3
793
- #
794
- def h3_for(identifier)
795
- find_selenium_element(identifier, Elements::Heading, 'h3')
796
- end
797
-
798
- #
799
- # platform method to retrieve all h3 elements
800
- #
801
- def h3s_for(identifier)
802
- find_selenium_elements(identifier, Elements::Heading, 'h3')
803
- end
804
-
805
- #
806
- # platform method to retrieve the text from a h4
807
- # See PageObject::Accessors#h4
808
- #
809
- def h4_text_for(identifier)
810
- process_selenium_call(identifier, Elements::Heading, 'h4') do |how, what|
811
- @browser.find_element(how, what).text
812
- end
813
- end
814
-
815
- #
816
- # platform method to retrieve the h4 element
817
- # See PageObject::Accessors#h4
818
- #
819
- def h4_for(identifier)
820
- find_selenium_element(identifier, Elements::Heading, 'h4')
821
- end
822
-
823
- #
824
- # platform method to retrieve all h4 elements
825
- #
826
- def h4s_for(identifier)
827
- find_selenium_elements(identifier, Elements::Heading, 'h4')
828
- end
829
-
830
- #
831
- # platform method to retrieve the text from a h5
832
- # See PageObject::Accessors#h5
833
- #
834
- def h5_text_for(identifier)
835
- process_selenium_call(identifier, Elements::Heading, 'h5') do |how, what|
836
- @browser.find_element(how, what).text
837
- end
838
- end
839
-
840
- #
841
- # platform method to retrieve the h5 element
842
- # See PageObject::Accessors#h5
843
- #
844
- def h5_for(identifier)
845
- find_selenium_element(identifier, Elements::Heading, 'h5')
846
- end
847
-
848
- #
849
- # platform method to retrieve all h5 elements
850
- #
851
- def h5s_for(identifier)
852
- find_selenium_elements(identifier, Elements::Heading, 'h5')
853
- end
854
-
855
- #
856
- # platform method to retrieve the text from a h6
857
- # See PageObject::Accessors#h6
858
- #
859
- def h6_text_for(identifier)
860
- process_selenium_call(identifier, Elements::Heading, 'h6') do |how, what|
861
- @browser.find_element(how, what).text
862
- end
863
- end
864
-
865
- #
866
- # platform method to retrieve the h6 element
867
- # See PageObject::Accessors#h6
868
- #
869
- def h6_for(identifier)
870
- find_selenium_element(identifier, Elements::Heading, 'h6')
871
- end
872
-
873
- #
874
- # platform method to retrieve all h6 elements
875
- #
876
- def h6s_for(identifier)
877
- find_selenium_elements(identifier, Elements::Heading, 'h6')
878
- end
879
-
880
- #
881
- # platform method to retrieve the text for a paragraph
882
- # See PageObject::Accessors#paragraph
883
- #
884
- def paragraph_text_for(identifier)
885
- process_selenium_call(identifier, Elements::Paragraph, 'p') do |how, what|
886
- @browser.find_element(how, what).text
887
- end
888
- end
889
-
890
- #
891
- # platform method to retrieve the paragraph element
892
- # See PageObject::Accessors#paragraph
893
- #
894
- def paragraph_for(identifier)
895
- find_selenium_element(identifier, Elements::Paragraph, 'p')
896
- end
897
-
898
- #
899
- # platform method to retrieve all paragraph elements
900
- #
901
- def paragraphs_for(identifier)
902
- find_selenium_elements(identifier, Elements::Paragraph, 'p')
903
- end
904
-
905
- #
906
- # platform method to return the text for a label
907
- # See PageObject::Accessors#label
908
- #
909
- def label_text_for(identifier)
910
- process_selenium_call(identifier, Elements::Label, 'label') do |how, what|
911
- @browser.find_element(how, what).text
912
- end
913
- end
914
-
915
-
916
- #
917
- # platform method to return a PageObject::Elements::Label element
918
- # See PageObject::Accessors#label
919
- #
920
- def label_for(identifier)
921
- find_selenium_element(identifier, Elements::Label, 'label')
922
- end
923
-
924
-
925
- #
926
- # platform method to retrieve all label elements
927
- #
928
- def labels_for(identifier)
929
- find_selenium_elements(identifier, Elements::Label, 'label')
930
- end
931
-
932
- #
933
- # platform method to set the file on a file_field element
934
- # See PageObject::Accessors#file_field
935
- #
936
- def file_field_value_set(identifier, value)
937
- process_selenium_call(identifier, Elements::FileField, 'input', :type => 'file') do |how, what|
938
- @browser.find_element(how, what).send_keys(value)
939
- end
940
- end
941
-
942
- #
943
- # platform method to retrieve a file_field element
944
- # See PageObject::Accessors#file_field
945
- #
946
- def file_field_for(identifier)
947
- find_selenium_element(identifier, Elements::FileField, 'input', :type => 'file')
948
- end
949
-
950
- #
951
- # platform method to return an array of file field elements
952
- #
953
- def file_fields_for(identifier)
954
- find_selenium_elements(identifier, Elements::FileField, 'input', :type => 'file')
955
- end
956
-
957
- #
958
- # platform method to click on an area
959
- #
960
- def click_area_for(identifier)
961
- process_selenium_call(identifier, Elements::Area, 'area') do |how, what|
962
- @browser.find_element(how, what).click
963
- end
964
- end
965
-
966
- #
967
- # platform method to retrieve an area element
968
- #
969
- def area_for(identifier)
970
- find_selenium_element(identifier, Elements::Area, 'area')
971
- end
972
-
973
- #
974
- # platform method to return an array of area elements
975
- #
976
- def areas_for(identifier)
977
- find_selenium_elements(identifier, Elements::Area, 'area')
978
- end
979
-
980
- #
981
- # platform method to retrieve a canvas element
982
- #
983
- def canvas_for(identifier)
984
- find_selenium_element(identifier, Elements::Canvas, 'canvas')
985
- end
986
-
987
- #
988
- # platform method to return an array of canvas elements
989
- #
990
- def canvass_for(identifier)
991
- find_selenium_elements(identifier, Elements::Canvas, 'canvas')
992
- end
993
-
994
- #
995
- # platform method to retrieve an audio element
996
- #
997
- def audio_for(identifier)
998
- find_selenium_element(identifier, Elements::Audio, 'audio')
999
- end
1000
-
1001
- #
1002
- # platform method to return an array of audio elements
1003
- #
1004
- def audios_for(identifier)
1005
- find_selenium_elements(identifier, Elements::Audio, 'audio')
1006
- end
1007
-
1008
- #
1009
- # platform method to retrieve a video element
1010
- #
1011
- def video_for(identifier)
1012
- find_selenium_element(identifier, Elements::Video, 'video')
1013
- end
1014
-
1015
- #
1016
- # platform method to return an array of video elements
1017
- #
1018
- def videos_for(identifier)
1019
- find_selenium_elements(identifier, Elements::Video, 'video')
1020
- end
1021
-
1022
- #
1023
- # platform method to retrieve a generic element
1024
- # See PageObject::Accessors#element
1025
- #
1026
- def element_for(tag, identifier)
1027
- find_selenium_element(identifier, Elements::Element, tag.to_s)
1028
- end
1029
-
1030
- #
1031
- # platform method to retrieve a collection of generic elements
1032
- # See PageObject::Accessors#elements
1033
- #
1034
- def elements_for(tag, identifier)
1035
- find_selenium_elements(identifier, Elements::Element, tag.to_s)
1036
- end
1037
-
1038
- #
1039
- # platform method to return a PageObject rooted at an element
1040
- # See PageObject::Accessors#page_section
1041
- #
1042
- def page_for(identifier, page_class)
1043
- find_selenium_page(identifier, page_class)
1044
- end
1045
-
1046
- #
1047
- # platform method to return a collection of PageObjects rooted at elements
1048
- # See PageObject::Accessors#page_sections
1049
- #
1050
- def pages_for(identifier, page_class)
1051
- SectionCollection[*find_selenium_pages(identifier, page_class)]
1052
- end
1053
-
1054
- #
1055
- # platform method to return a svg element
1056
- #
1057
- def svg_for(identifier)
1058
- find_selenium_element(identifier, Elements::Element, 'svg')
1059
- end
1060
-
1061
- #
1062
- # platform method to return an array of svg elements
1063
- #
1064
- def svgs_for(identifier)
1065
- find_selenium_elements(identifier, Elements::Element, 'svg')
1066
- end
1067
-
1068
-
1069
- #
1070
- # platform method to retrieve the text from a b
1071
- # See PageObject::Accessors#b
1072
- #
1073
- def b_text_for(identifier)
1074
- process_selenium_call(identifier, Elements::Bold, 'b') do |how, what|
1075
- @browser.find_element(how, what).text
1076
- end
1077
- end
1078
-
1079
- #
1080
- # platform method to retrieve the b element
1081
- # See PageObject::Accessors#b
1082
- #
1083
- def b_for(identifier)
1084
- find_selenium_element(identifier, Elements::Bold, 'b')
1085
- end
1086
-
1087
- #
1088
- # platform method to retrieve all b elements
1089
- #
1090
- def bs_for(identifier)
1091
- find_selenium_elements(identifier, Elements::Bold, 'b')
1092
- end
1093
-
1094
- #
1095
- # platform method to retrieve the text from a i
1096
- # See PageObject::Accessors#i
1097
- #
1098
- def i_text_for(identifier)
1099
- process_selenium_call(identifier, Elements::Italic, 'i') do |how, what|
1100
- @browser.find_element(how, what).text
1101
- end
1102
- end
1103
-
1104
- #
1105
- # platform method to retrieve the i element
1106
- # See PageObject::Accessors#i
1107
- #
1108
- def i_for(identifier)
1109
- find_selenium_element(identifier, Elements::Italic, 'i')
1110
- end
1111
-
1112
- #
1113
- # platform method to retrieve all i elements
1114
- #
1115
- def is_for(identifier)
1116
- find_selenium_elements(identifier, Elements::Italic, 'i')
1117
- end
1118
-
1119
- private
1120
-
1121
- def process_selenium_call(identifier, type, tag, other=nil)
1122
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1123
- switch_to_frame(frame_identifiers)
1124
- value = yield how, what
1125
- @browser.switch_to.default_content unless frame_identifiers.nil?
1126
- value
1127
- end
1128
-
1129
- def find_selenium_element(identifier, type, tag, other=nil)
1130
- regexp = delete_regexp(identifier)
1131
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1132
- switch_to_frame(frame_identifiers)
1133
- begin
1134
- unless regexp
1135
- element = @browser.find_element(how, what)
1136
- else
1137
- elements = @browser.find_elements(how, what)
1138
- element = elements.find {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1139
- end
1140
- rescue Selenium::WebDriver::Error::NoSuchElementError
1141
- @browser.switch_to.default_content unless frame_identifiers.nil?
1142
- return build_null_object(identifier, type, tag, other)
1143
- end
1144
- @browser.switch_to.default_content unless frame_identifiers.nil?
1145
- type.new(element, :platform => self.class::PLATFORM_NAME)
1146
- end
1147
-
1148
- def find_selenium_elements(identifier, type, tag, other=nil)
1149
- regexp = delete_regexp(identifier)
1150
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1151
- switch_to_frame(frame_identifiers)
1152
- unless regexp
1153
- elements = @browser.find_elements(how, what)
1154
- else
1155
- eles = @browser.find_elements(how, what)
1156
- elements = eles.find_all {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1157
- end
1158
- @browser.switch_to.default_content unless frame_identifiers.nil?
1159
- elements.map { |element| type.new(element, :platform => self.class::PLATFORM_NAME) }
1160
- end
1161
-
1162
- def find_selenium_pages(identifier, page_class)
1163
- regexp = delete_regexp(identifier)
1164
- how, what, frame_identifiers = parse_identifiers(identifier, Elements::Element, 'element')
1165
- switch_to_frame(frame_identifiers)
1166
- unless regexp
1167
- elements = @browser.find_elements(how, what)
1168
- else
1169
- eles = @browser.find_elements(how, what)
1170
- elements = eles.find_all {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1171
- end
1172
- @browser.switch_to.default_content unless frame_identifiers.nil?
1173
- elements.map { |element| page_class.new(element) }
1174
- end
1175
-
1176
- def find_selenium_page(identifier, page_class)
1177
- type, tag = Elements::Element, 'element'
1178
- regexp = delete_regexp(identifier)
1179
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag)
1180
- switch_to_frame(frame_identifiers)
1181
- begin
1182
- unless regexp
1183
- element = @browser.find_element(how, what)
1184
- else
1185
- elements = @browser.find_elements(how, what)
1186
- element = elements.find {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1187
- end
1188
- rescue Selenium::WebDriver::Error::NoSuchElementError
1189
- @browser.switch_to.default_content unless frame_identifiers.nil?
1190
- return build_null_object(identifier, type, tag, nil)
1191
- end
1192
- @browser.switch_to.default_content unless frame_identifiers.nil?
1193
- page_class.new(element)
1194
- end
1195
-
1196
- def build_null_object(identifier, type, tag, other)
1197
- null_element = SurrogateSeleniumElement.new
1198
- null_element.identifier = identifier
1199
- null_element.type = type
1200
- null_element.tag = tag
1201
- null_element.other = other
1202
- null_element.platform = self
1203
- Elements::Element.new(null_element, :platform => self.class::PLATFORM_NAME)
1204
- end
1205
-
1206
- def delete_regexp(identifier)
1207
- regexp = identifier.find {|ident| ident[1].is_a?(Regexp)}
1208
- identifier.delete(regexp[0]) if regexp
1209
- regexp
1210
- end
1211
-
1212
- def parse_identifiers(identifier, element, tag_name=nil, additional=nil)
1213
- frame_identifiers = identifier.delete(:frame)
1214
- identifier = add_tagname_if_needed identifier, tag_name, additional if tag_name
1215
- how, what = element.selenium_identifier_for identifier
1216
- return how, what, frame_identifiers
1217
- end
1218
-
1219
- def add_tagname_if_needed identifier, tag, additional=nil
1220
- return identifier if identifier.length < 2 and supported_identifier(identifier, tag, additional)
1221
- identifier[:tag_name] = tag
1222
- if additional
1223
- additional.each do |key, value|
1224
- identifier[key] = value
1225
- end
1226
- end
1227
- identifier
1228
- end
1229
-
1230
- def supported_identifier(identifier, tag, additional)
1231
- return false if identifier.size == 0
1232
- return false if identifier[:index]
1233
- return false if identifier[:action] and tag == 'form'
1234
- return false if identifier[:alt] and tag == 'img'
1235
- return false if identifier[:alt] and tag == 'input' and
1236
- ['submit', 'image', 'button', 'reset'].include? additional[:type]
1237
- return false if identifier[:href] and tag == 'a'
1238
- return false if identifier[:src] and tag == 'input' and
1239
- ['submit', 'image', 'button', 'reset'].include? additional[:type]
1240
- return false if identifier[:src] and tag == 'img'
1241
- return false if identifier[:label]
1242
- return false if identifier[:text] and tag == 'input' and additional[:type] == 'hidden'
1243
- return false if identifier[:text] and tag == 'input' and additional[:type] == 'text'
1244
- return false if identifier[:text] and ['div', 'span', 'td', 'label', 'li'].include? tag
1245
- return false if identifier[:title] and tag == 'input' and additional[:type] == 'text'
1246
- return false if identifier[:title] and tag == 'input' and additional[:type] == 'file'
1247
- return false if identifier[:title] and tag == 'a'
1248
- return false if identifier[:title] and tag == 'span'
1249
- return false if identifier[:title] and tag == 'div'
1250
- return false if identifier[:value] and tag == 'input' and
1251
- ['radio', 'submit', 'image', 'button', 'reset', 'checkbox', 'hidden'].include? additional[:type]
1252
- true
1253
- end
1254
-
1255
- def matches_selector?(element, how, what)
1256
- what === fetch_value(element, how)
1257
- end
1258
-
1259
- def fetch_value(element, how)
1260
- case how
1261
- when :text
1262
- element.text
1263
- when :tag_name
1264
- element.tag_name.downcase
1265
- when :href
1266
- (href = element.attribute(:href)) && href.strip
1267
- else
1268
- element.attribute(how.to_s.gsub("_", "-").to_sym)
1269
- end
1270
- end
1271
-
1272
- def switch_to_frame(frame_identifiers)
1273
- unless frame_identifiers.nil?
1274
- frame_identifiers.each do |frame|
1275
- frame_id = frame.values.first
1276
- value = frame_id.values.first
1277
- @browser.switch_to.frame(value)
1278
- end
1279
- end
1280
- end
1281
-
1282
- def self.define_widget_multiple_accessor(base_element_tag, widget_class, widget_tag)
1283
- send(:define_method, "#{widget_tag}s_for") do |identifier|
1284
- find_selenium_elements(identifier, widget_class, base_element_tag)
1285
- end
1286
- end
1287
-
1288
- def self.define_widget_singular_accessor(base_element_tag, widget_class, widget_tag)
1289
- send(:define_method, "#{widget_tag}_for") do |identifier|
1290
- find_selenium_element(identifier, widget_class, base_element_tag)
1291
- end
1292
- end
1293
-
1294
- end
1295
- end
1296
- end
1297
- end