meeane-page-object 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (253) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -7
  3. data/.rspec +2 -2
  4. data/.ruby-gemset +1 -1
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +10 -10
  7. data/ChangeLog +790 -790
  8. data/Gemfile +13 -13
  9. data/Guardfile +20 -20
  10. data/LICENSE +20 -20
  11. data/README.md +1 -1
  12. data/Rakefile +35 -35
  13. data/cucumber.yml +10 -10
  14. data/features/area.feature +35 -35
  15. data/features/async.feature +30 -30
  16. data/features/audio.feature +68 -68
  17. data/features/button.feature +88 -87
  18. data/features/canvas.feature +37 -37
  19. data/features/check_box.feature +55 -55
  20. data/features/div.feature +45 -45
  21. data/features/element.feature +322 -322
  22. data/features/file_field.feature +40 -40
  23. data/features/form.feature +43 -43
  24. data/features/frames.feature +75 -75
  25. data/features/generic_elements.feature +29 -29
  26. data/features/gxt_table_extension.feature +24 -24
  27. data/features/headings.feature +97 -97
  28. data/features/hidden_field.feature +45 -45
  29. data/features/html/async.html +36 -36
  30. data/features/html/double_click.html +12 -12
  31. data/features/html/failure.html +7 -7
  32. data/features/html/frame_1.html +17 -17
  33. data/features/html/frame_2.html +15 -15
  34. data/features/html/frame_3.html +14 -14
  35. data/features/html/frames.html +12 -12
  36. data/features/html/hover.html +11 -11
  37. data/features/html/iframes.html +12 -12
  38. data/features/html/indexed_property.html +51 -51
  39. data/features/html/modal.html +17 -17
  40. data/features/html/modal_1.html +37 -37
  41. data/features/html/modal_2.html +26 -26
  42. data/features/html/multi_elements.html +144 -144
  43. data/features/html/nested_elements.html +77 -77
  44. data/features/html/nested_frame_2.html +11 -11
  45. data/features/html/nested_frame_3.html +14 -14
  46. data/features/html/nested_frames.html +10 -10
  47. data/features/html/static_elements.html +233 -226
  48. data/features/html/success.html +7 -7
  49. data/features/html/sun.html +7 -7
  50. data/features/image.feature +50 -50
  51. data/features/indexed_property.feature +97 -97
  52. data/features/javascript.feature +27 -27
  53. data/features/label.feature +46 -46
  54. data/features/link.feature +52 -52
  55. data/features/list_item.feature +36 -36
  56. data/features/modal_dialog.feature +15 -15
  57. data/features/multi_elements.feature +486 -486
  58. data/features/nested_elements.feature +117 -117
  59. data/features/ordered_list.feature +56 -56
  60. data/features/page_level_actions.feature +90 -90
  61. data/features/paragraph.feature +35 -35
  62. data/features/radio_button.feature +58 -58
  63. data/features/radio_button_collinson.feature +12 -58
  64. data/features/radio_button_group.feature +29 -29
  65. data/features/sample-app/public/jquery-1.3.2.js +4376 -4376
  66. data/features/sample-app/public/jquery.html +30 -30
  67. data/features/sample-app/public/prototype-1.6.0.3.js +4319 -4319
  68. data/features/sample-app/public/prototype.html +35 -35
  69. data/features/sample-app/sample_app.rb +35 -35
  70. data/features/select_list.feature +86 -86
  71. data/features/span.feature +37 -37
  72. data/features/step_definitions/accessor_steps.rb +60 -60
  73. data/features/step_definitions/area_steps.rb +19 -19
  74. data/features/step_definitions/async_steps.rb +83 -83
  75. data/features/step_definitions/audio_steps.rb +27 -27
  76. data/features/step_definitions/button_steps.rb +43 -43
  77. data/features/step_definitions/canvas_steps.rb +15 -15
  78. data/features/step_definitions/check_box_steps.rb +35 -35
  79. data/features/step_definitions/div_steps.rb +19 -19
  80. data/features/step_definitions/element_steps.rb +266 -266
  81. data/features/step_definitions/file_field_steps.rb +19 -19
  82. data/features/step_definitions/form_steps.rb +18 -18
  83. data/features/step_definitions/frames_steps.rb +159 -159
  84. data/features/step_definitions/generic_element_steps.rb +31 -31
  85. data/features/step_definitions/gxt_table_steps.rb +58 -58
  86. data/features/step_definitions/headings_steps.rb +12 -12
  87. data/features/step_definitions/hidden_field_steps.rb +26 -26
  88. data/features/step_definitions/image_steps.rb +27 -27
  89. data/features/step_definitions/indexed_property_steps.rb +128 -128
  90. data/features/step_definitions/javascript_steps.rb +52 -52
  91. data/features/step_definitions/label_steps.rb +19 -19
  92. data/features/step_definitions/link_steps.rb +40 -40
  93. data/features/step_definitions/list_item_steps.rb +19 -19
  94. data/features/step_definitions/modal_dialog_steps.rb +62 -62
  95. data/features/step_definitions/multi_elements_steps.rb +528 -528
  96. data/features/step_definitions/nested_elements_steps.rb +212 -212
  97. data/features/step_definitions/ordered_list_steps.rb +23 -23
  98. data/features/step_definitions/page_level_actions_steps.rb +135 -135
  99. data/features/step_definitions/page_traversal_steps.rb +4 -4
  100. data/features/step_definitions/paragraph_steps.rb +28 -28
  101. data/features/step_definitions/radio_button_collinson_steps.rb +2 -46
  102. data/features/step_definitions/radio_button_group_steps.rb +35 -35
  103. data/features/step_definitions/radio_button_steps.rb +26 -46
  104. data/features/step_definitions/select_list_steps.rb +65 -65
  105. data/features/step_definitions/span_steps.rb +19 -19
  106. data/features/step_definitions/table_cell_steps.rb +14 -14
  107. data/features/step_definitions/table_steps.rb +66 -66
  108. data/features/step_definitions/text_area_steps.rb +34 -34
  109. data/features/step_definitions/text_field_steps.rb +35 -35
  110. data/features/step_definitions/unordered_list_steps.rb +23 -23
  111. data/features/step_definitions/video_steps.rb +45 -45
  112. data/features/support/ajax_text_environment.rb +26 -26
  113. data/features/support/env.rb +8 -8
  114. data/features/support/hooks.rb +8 -8
  115. data/features/support/page.rb +370 -364
  116. data/features/support/persistent_browser.rb +70 -70
  117. data/features/support/targets/firefox14_osx.rb +6 -6
  118. data/features/support/targets/firefox14_windows7.rb +6 -6
  119. data/features/support/url_helper.rb +57 -57
  120. data/features/table.feature +114 -114
  121. data/features/table_cell.feature +45 -45
  122. data/features/text_area.feature +51 -51
  123. data/features/text_field.feature +70 -70
  124. data/features/unordered_list.feature +56 -56
  125. data/features/video.feature +73 -73
  126. data/lib/page-object.rb +408 -408
  127. data/lib/page-object/accessors.rb +1313 -1313
  128. data/lib/page-object/core_ext/string.rb +4 -4
  129. data/lib/page-object/element_locators.rb +21 -21
  130. data/lib/page-object/elements.rb +59 -59
  131. data/lib/page-object/elements/area.rb +31 -31
  132. data/lib/page-object/elements/audio.rb +9 -9
  133. data/lib/page-object/elements/button.rb +35 -35
  134. data/lib/page-object/elements/canvas.rb +23 -23
  135. data/lib/page-object/elements/check_box.rb +37 -37
  136. data/lib/page-object/elements/div.rb +19 -19
  137. data/lib/page-object/elements/element.rb +226 -226
  138. data/lib/page-object/elements/file_field.rb +38 -38
  139. data/lib/page-object/elements/form.rb +36 -36
  140. data/lib/page-object/elements/heading.rb +15 -15
  141. data/lib/page-object/elements/hidden_field.rb +22 -22
  142. data/lib/page-object/elements/image.rb +36 -36
  143. data/lib/page-object/elements/label.rb +36 -36
  144. data/lib/page-object/elements/link.rb +46 -46
  145. data/lib/page-object/elements/list_item.rb +19 -19
  146. data/lib/page-object/elements/media.rb +45 -45
  147. data/lib/page-object/elements/option.rb +10 -10
  148. data/lib/page-object/elements/ordered_list.rb +50 -50
  149. data/lib/page-object/elements/paragraph.rb +9 -9
  150. data/lib/page-object/elements/radio_button.rb +37 -37
  151. data/lib/page-object/elements/select_list.rb +42 -42
  152. data/lib/page-object/elements/span.rb +19 -19
  153. data/lib/page-object/elements/table.rb +68 -68
  154. data/lib/page-object/elements/table_cell.rb +28 -28
  155. data/lib/page-object/elements/table_row.rb +50 -50
  156. data/lib/page-object/elements/text_area.rb +38 -38
  157. data/lib/page-object/elements/text_field.rb +42 -42
  158. data/lib/page-object/elements/unordered_list.rb +51 -51
  159. data/lib/page-object/elements/video.rb +18 -18
  160. data/lib/page-object/indexed_properties.rb +36 -36
  161. data/lib/page-object/javascript/jquery.rb +14 -14
  162. data/lib/page-object/javascript/prototype.rb +14 -14
  163. data/lib/page-object/javascript/yui.rb +18 -18
  164. data/lib/page-object/javascript_framework_facade.rb +78 -78
  165. data/lib/page-object/loads_platform.rb +26 -26
  166. data/lib/page-object/locator_generator.rb +129 -129
  167. data/lib/page-object/nested_elements.rb +17 -17
  168. data/lib/page-object/page_factory.rb +108 -108
  169. data/lib/page-object/page_populator.rb +92 -92
  170. data/lib/page-object/platforms.rb +18 -18
  171. data/lib/page-object/platforms/selenium_webdriver.rb +17 -17
  172. data/lib/page-object/platforms/selenium_webdriver/button.rb +14 -14
  173. data/lib/page-object/platforms/selenium_webdriver/check_box.rb +29 -29
  174. data/lib/page-object/platforms/selenium_webdriver/element.rb +291 -291
  175. data/lib/page-object/platforms/selenium_webdriver/file_field.rb +16 -16
  176. data/lib/page-object/platforms/selenium_webdriver/form.rb +16 -16
  177. data/lib/page-object/platforms/selenium_webdriver/image.rb +28 -28
  178. data/lib/page-object/platforms/selenium_webdriver/label.rb +17 -18
  179. data/lib/page-object/platforms/selenium_webdriver/link.rb +23 -23
  180. data/lib/page-object/platforms/selenium_webdriver/ordered_list.rb +39 -39
  181. data/lib/page-object/platforms/selenium_webdriver/page_object.rb +1136 -1136
  182. data/lib/page-object/platforms/selenium_webdriver/radio_button.rb +22 -22
  183. data/lib/page-object/platforms/selenium_webdriver/select_list.rb +93 -93
  184. data/lib/page-object/platforms/selenium_webdriver/surrogate_selenium_element.rb +42 -42
  185. data/lib/page-object/platforms/selenium_webdriver/table.rb +42 -42
  186. data/lib/page-object/platforms/selenium_webdriver/table_row.rb +43 -43
  187. data/lib/page-object/platforms/selenium_webdriver/text_area.rb +17 -17
  188. data/lib/page-object/platforms/selenium_webdriver/text_field.rb +17 -17
  189. data/lib/page-object/platforms/selenium_webdriver/unordered_list.rb +39 -39
  190. data/lib/page-object/platforms/watir_webdriver.rb +18 -18
  191. data/lib/page-object/platforms/watir_webdriver/check_box.rb +29 -29
  192. data/lib/page-object/platforms/watir_webdriver/element.rb +249 -249
  193. data/lib/page-object/platforms/watir_webdriver/file_field.rb +16 -16
  194. data/lib/page-object/platforms/watir_webdriver/form.rb +16 -16
  195. data/lib/page-object/platforms/watir_webdriver/image.rb +22 -22
  196. data/lib/page-object/platforms/watir_webdriver/label.rb +17 -18
  197. data/lib/page-object/platforms/watir_webdriver/link.rb +15 -15
  198. data/lib/page-object/platforms/watir_webdriver/ordered_list.rb +35 -35
  199. data/lib/page-object/platforms/watir_webdriver/page_object.rb +1029 -1029
  200. data/lib/page-object/platforms/watir_webdriver/radio_button.rb +22 -22
  201. data/lib/page-object/platforms/watir_webdriver/select_list.rb +74 -74
  202. data/lib/page-object/platforms/watir_webdriver/table.rb +38 -38
  203. data/lib/page-object/platforms/watir_webdriver/table_row.rb +37 -37
  204. data/lib/page-object/platforms/watir_webdriver/text_area.rb +23 -23
  205. data/lib/page-object/platforms/watir_webdriver/text_field.rb +16 -16
  206. data/lib/page-object/platforms/watir_webdriver/unordered_list.rb +36 -36
  207. data/lib/page-object/version.rb +4 -4
  208. data/lib/page-object/widgets.rb +133 -133
  209. data/meeane-page-object.gemspec +32 -32
  210. data/spec/page-object/element_locators_spec.rb +1065 -1065
  211. data/spec/page-object/elements/area_spec.rb +45 -45
  212. data/spec/page-object/elements/button_spec.rb +50 -50
  213. data/spec/page-object/elements/canvas_spec.rb +40 -40
  214. data/spec/page-object/elements/check_box_spec.rb +49 -49
  215. data/spec/page-object/elements/div_spec.rb +28 -28
  216. data/spec/page-object/elements/element_spec.rb +114 -114
  217. data/spec/page-object/elements/file_field_spec.rb +39 -39
  218. data/spec/page-object/elements/form_spec.rb +28 -28
  219. data/spec/page-object/elements/heading_spec.rb +48 -48
  220. data/spec/page-object/elements/hidden_field_spec.rb +28 -28
  221. data/spec/page-object/elements/image_spec.rb +66 -66
  222. data/spec/page-object/elements/label_spec.rb +29 -29
  223. data/spec/page-object/elements/link_spec.rb +49 -49
  224. data/spec/page-object/elements/list_item_spec.rb +28 -28
  225. data/spec/page-object/elements/nested_element_spec.rb +254 -254
  226. data/spec/page-object/elements/option_spec.rb +11 -11
  227. data/spec/page-object/elements/ordered_list_spec.rb +94 -94
  228. data/spec/page-object/elements/paragraph_spec.rb +28 -28
  229. data/spec/page-object/elements/select_list_spec.rb +142 -142
  230. data/spec/page-object/elements/selenium/radio_button_spec.rb +44 -44
  231. data/spec/page-object/elements/selenium/text_field_spec.rb +49 -49
  232. data/spec/page-object/elements/selenium_element_spec.rb +172 -172
  233. data/spec/page-object/elements/span_spec.rb +26 -26
  234. data/spec/page-object/elements/table_cell_spec.rb +21 -21
  235. data/spec/page-object/elements/table_row_spec.rb +70 -70
  236. data/spec/page-object/elements/table_spec.rb +98 -98
  237. data/spec/page-object/elements/text_area_spec.rb +39 -39
  238. data/spec/page-object/elements/unordered_list_spec.rb +95 -95
  239. data/spec/page-object/elements/watir_element_spec.rb +142 -142
  240. data/spec/page-object/javascript_framework_facade_spec.rb +61 -61
  241. data/spec/page-object/loads_platform_spec.rb +53 -53
  242. data/spec/page-object/page-object_spec.rb +405 -405
  243. data/spec/page-object/page_factory_spec.rb +256 -256
  244. data/spec/page-object/page_populator_spec.rb +122 -122
  245. data/spec/page-object/platforms/selenium_webdriver/selenium_page_object_spec.rb +68 -68
  246. data/spec/page-object/platforms/selenium_webdriver_spec.rb +28 -28
  247. data/spec/page-object/platforms/watir_webdriver/watir_page_object_spec.rb +29 -29
  248. data/spec/page-object/platforms/watir_webdriver_spec.rb +9 -9
  249. data/spec/page-object/selenium_accessors_spec.rb +589 -589
  250. data/spec/page-object/watir_accessors_spec.rb +1107 -1107
  251. data/spec/page-object/widget_spec.rb +226 -226
  252. data/spec/spec_helper.rb +44 -44
  253. metadata +193 -43
@@ -1,16 +1,16 @@
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
+ 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 +1,16 @@
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
+ 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 +1,28 @@
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
+ 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,18 +1,17 @@
1
- module PageObject
2
- module Platforms
3
- module SeleniumWebDriver
4
- module Label
5
-
6
- #
7
- # choose the label
8
- #
9
- def choose
10
- sleep 0.5
11
- element.click
12
- wait_for_ajax
13
- end
14
-
15
- end
16
- end
17
- end
18
- end
1
+ module PageObject
2
+ module Platforms
3
+ module SeleniumWebDriver
4
+ module Label
5
+
6
+ #
7
+ # choose the label
8
+ #
9
+ def choose
10
+ element.click
11
+ wait_for_ajax
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,23 +1,23 @@
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
+ 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,39 +1,39 @@
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
- children[idx]
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
- private
24
-
25
- def children
26
- items = list_items.map do |item|
27
- ::PageObject::Elements::ListItem.new(item, :platform => :selenium_webdriver)
28
- end
29
- items.find_all { |item| item.parent.element == element }
30
- end
31
-
32
- def list_items
33
- element.find_elements(:xpath, child_xpath)
34
- end
35
-
36
- end
37
- end
38
- end
39
- end
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
+ children[idx]
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
+ private
24
+
25
+ def children
26
+ items = list_items.map do |item|
27
+ ::PageObject::Elements::ListItem.new(item, :platform => :selenium_webdriver)
28
+ end
29
+ items.find_all { |item| item.parent.element == element }
30
+ end
31
+
32
+ def list_items
33
+ element.find_elements(:xpath, child_xpath)
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,1136 +1,1136 @@
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
- def initialize(browser)
16
- @browser = browser
17
- end
18
-
19
- #
20
- # platform method to navigate to a provided url
21
- # See PageObject#navigate_to
22
- #
23
- def navigate_to(url)
24
- @browser.navigate.to url
25
- end
26
-
27
- #
28
- # platform method to get the current url
29
- # See PageObject#current_url
30
- #
31
- def current_url
32
- @browser.current_url
33
- end
34
-
35
- #
36
- # platform method to retrieve the text from the current page
37
- # See PageObject#text
38
- #
39
- def text
40
- @browser.find_element(:tag_name, 'body').text
41
- end
42
-
43
- #
44
- # platform method to retrieve the html for the current page
45
- # See PageObject#html
46
- #
47
- def html
48
- @browser.page_source
49
- end
50
-
51
- #
52
- # platform method to retrieve the title for the current page
53
- # See PageObject#title
54
- #
55
- def title
56
- @browser.title
57
- end
58
-
59
- #
60
- # platform method to wait for a block to return true
61
- # See PageObject#wait_until
62
- #
63
- def wait_until(timeout, message = nil, &block)
64
- wait = ::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => message})
65
- wait.until &block
66
- end
67
-
68
- #
69
- # platform method to handle an alert popup
70
- # See PageObject#alert
71
- #
72
- def alert(frame=nil, &block)
73
- yield
74
- begin
75
- alert = @browser.switch_to.alert
76
- value = alert.text
77
- alert.accept
78
- rescue Selenium::WebDriver::Error::NoAlertPresentError
79
- end
80
- value
81
- end
82
-
83
- #
84
- # platform method to handle a confirm popup
85
- # See PageObject#confirm
86
- #
87
- def confirm(response, frame=nil, &block)
88
- yield
89
- begin
90
- alert = @browser.switch_to.alert
91
- value = alert.text
92
- response ? alert.accept : alert.dismiss
93
- rescue Selenium::WebDriver::Error::NoAlertPresentError
94
- end
95
- value
96
- end
97
-
98
- #
99
- # platform method to handle a prompt popup
100
- # See PageObject#prompt
101
- #
102
- def prompt(answer, frame=nil, &block)
103
- @browser.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{answer}; }"
104
- yield
105
- result = @browser.execute_script "return window.__lastWatirPrompt"
106
- result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
107
- result
108
- end
109
-
110
- #
111
- # platform method to execute javascript on the browser
112
- # See PageObject#execute_script
113
- #
114
- def execute_script(script, *args)
115
- @browser.execute_script(script, *args)
116
- end
117
-
118
- #
119
- # platform method to handle attaching to a running window
120
- # See PageObject#attach_to_window
121
- #
122
- def attach_to_window(identifier, &block)
123
- value = identifier.values.first
124
- key = identifier.keys.first
125
- handles = @browser.window_handles
126
- handles.each do |handle|
127
- @browser.switch_to.window handle
128
- if (key == :title and value == @browser.title) or
129
- (key == :url and @browser.current_url.include?(value))
130
- return @browser.switch_to.window handle, &block
131
- end
132
- end
133
- end
134
-
135
- #
136
- # find the element that has focus
137
- #
138
- def element_with_focus
139
- element = @browser.execute_script("return document.activeElement")
140
- type = element.attribute(:type).to_s.downcase if element.tag_name.to_sym == :input
141
- cls = ::PageObject::Elements.element_class_for(element.tag_name, type)
142
- cls.new(element, :platform => :selenium_webdriver)
143
- end
144
-
145
- #
146
- # platform method to switch to a frame and execute a block
147
- # See PageObject#in_frame
148
- #
149
- def in_frame(identifier, frame=nil, &block)
150
- switch_to_frame([frame: identifier])
151
- block.call(nil)
152
- @browser.switch_to.default_content
153
- end
154
-
155
- #
156
- # platform method to switch to an iframe and execute a block
157
- # See PageObject#in_frame
158
- #
159
- def in_iframe(identifier, frame=nil, &block)
160
- switch_to_frame([iframe: identifier])
161
- block.call(nil)
162
- @browser.switch_to.default_content
163
- end
164
-
165
- #
166
- # platform method to refresh the page
167
- # See PageObject#refresh
168
- #
169
- def refresh
170
- @browser.navigate.refresh
171
- end
172
-
173
- #
174
- # platform method to go back to the previous page
175
- # See PageObject#back
176
- #
177
- def back
178
- @browser.navigate.back
179
- end
180
-
181
- #
182
- # platform method to go forward to the next page
183
- # See PageObject#forward
184
- #
185
- def forward
186
- @browser.navigate.forward
187
- end
188
-
189
- #
190
- # platform method to clear the cookies from the browser
191
- # See PageObject#clear_cookies
192
- #
193
- def clear_cookies
194
- @browser.manage.delete_all_cookies
195
- end
196
-
197
- #
198
- # platform method to save the current screenshot to a file
199
- # See PageObject#save_screenshot
200
- #
201
- def save_screenshot(file_name)
202
- @browser.save_screenshot(file_name)
203
- end
204
-
205
- #
206
- # platform method to get the value stored in a text field
207
- # See PageObject::Accessors#text_field
208
- #
209
- def text_field_value_for(identifier)
210
- process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|
211
- @browser.find_element(how, what).attribute('value')
212
- end
213
- end
214
-
215
-
216
- #
217
- # platform method to set the value for a text field
218
- # See PageObject::Accessors#text_field
219
- #
220
- def text_field_value_set(identifier, value)
221
- process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|
222
- @browser.find_element(how, what).clear
223
- @browser.find_element(how, what).send_keys(value)
224
- end
225
- end
226
-
227
- #
228
- # platform method to retrieve a text field element
229
- # See PageObject::Accessors#text_field
230
- #
231
- def text_field_for(identifier)
232
- find_selenium_element(identifier, Elements::TextField, 'input', :type => 'text')
233
- end
234
-
235
- #
236
- # platform method to retrieve all text field elements
237
- #
238
- def text_fields_for(identifier)
239
- find_selenium_elements(identifier, Elements::TextField, 'input', :type => 'text')
240
- end
241
-
242
- #
243
- # platform method to get the value stored in a hidden field
244
- # See PageObject::Accessors#hidden_field
245
- #
246
- def hidden_field_value_for(identifier)
247
- process_selenium_call(identifier, Elements::HiddenField, 'input', :type => 'hidden') do |how, what|
248
- @browser.find_element(how, what).attribute('value')
249
- end
250
- end
251
-
252
- #
253
- # platform method to retrieve a hidden field element
254
- # See PageObject::Accessors#hidden_field
255
- #
256
- def hidden_field_for(identifier)
257
- find_selenium_element(identifier, Elements::HiddenField, 'input', :type => 'hidden')
258
- end
259
-
260
- #
261
- # platform method to retrieve all hidden field elements
262
- #
263
- def hidden_fields_for(identifier)
264
- find_selenium_elements(identifier, Elements::HiddenField, 'input', :type => 'hidden')
265
- end
266
-
267
- #
268
- # platform method to set text in a textarea
269
- # See PageObject::Accessors#text_area
270
- #
271
- def text_area_value_set(identifier, value)
272
- process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|
273
- text_area = @browser.find_element(how, what)
274
- text_area.clear
275
- text_area.send_keys(value)
276
- end
277
- end
278
-
279
- #
280
- # platform method to get the text from a textarea
281
- # See PageObject::Accessors#text_area
282
- #
283
- def text_area_value_for(identifier)
284
- process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|
285
- @browser.find_element(how, what).attribute('value')
286
- end
287
- end
288
-
289
- #
290
- # platform method to get the text area element
291
- # See PageObject::Accessors#text_area
292
- #
293
- def text_area_for(identifier)
294
- find_selenium_element(identifier, Elements::TextArea, 'textarea')
295
- end
296
-
297
- #
298
- # platform method to retrieve all text area elements
299
- #
300
- def text_areas_for(identifier)
301
- find_selenium_elements(identifier, Elements::TextArea, 'textarea')
302
- end
303
-
304
- #
305
- # platform method to get the currently selected value from a select list
306
- # See PageObject::Accessors#select_list
307
- #
308
- def select_list_value_for(identifier)
309
- process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|
310
- selected = nil
311
- @browser.find_element(how, what).find_elements(:tag_name => 'option').each do |o|
312
- if selected.nil?
313
- selected = o.text if o.selected?
314
- end
315
- end
316
- selected
317
- end
318
- end
319
-
320
- #
321
- # platform method to select a value from a select list
322
- # See PageObject::Accessors#select_list
323
- #
324
- def select_list_value_set(identifier, value)
325
- process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|
326
- select_list = @browser.find_element(how, what)
327
- select_list.find_elements(:tag_name => 'option').find do |option|
328
- option.text == value
329
- end.click
330
- end
331
- end
332
-
333
- #
334
- # platform method to return the select list element
335
- # See PageObject::Accessors#select_list
336
- #
337
- def select_list_for(identifier)
338
- find_selenium_element(identifier, Elements::SelectList, 'select')
339
- end
340
-
341
- #
342
- # platform method to retrieve all select list elements
343
- #
344
- def select_lists_for(identifier)
345
- find_selenium_elements(identifier, Elements::SelectList, 'select')
346
- end
347
-
348
- #
349
- # platform method to click a link
350
- # See PageObject::Accessors#link
351
- #
352
- def click_link_for(identifier)
353
- process_selenium_call(identifier, Elements::Link, 'a') do |how, what|
354
- @browser.find_element(how, what).click
355
- end
356
- end
357
-
358
- #
359
- # platform method to return a PageObject::Elements::Link object
360
- # see PageObject::Accessors#link
361
- #
362
- def link_for(identifier)
363
- find_selenium_element(identifier, Elements::Link, 'a')
364
- end
365
-
366
- #
367
- # platform method to retrieve all link elements
368
- #
369
- def links_for(identifier)
370
- find_selenium_elements(identifier, Elements::Link, 'a')
371
- end
372
-
373
- #
374
- # platform method to check a checkbox
375
- # See PageObject::Accessors#checkbox
376
- #
377
- def check_checkbox(identifier)
378
- process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
379
- @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?
380
- end
381
- end
382
-
383
- #
384
- # platform method to uncheck a checkbox
385
- # See PageObject::Accessors#checkbox
386
- #
387
- def uncheck_checkbox(identifier)
388
- process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
389
- @browser.find_element(how, what).click if @browser.find_element(how, what).selected?
390
- end
391
- end
392
-
393
- #
394
- # platform method to determine if a checkbox is checked
395
- # See PageObject::Accessors#checkbox
396
- #
397
- def checkbox_checked?(identifier)
398
- process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
399
- @browser.find_element(how, what).selected?
400
- end
401
- end
402
-
403
- #
404
- # platform method to return a PageObject::Elements::CheckBox element
405
- # See PageObject::Accessors#checkbox
406
- #
407
- def checkbox_for(identifier)
408
- find_selenium_element(identifier, Elements::CheckBox, 'input', :type => 'checkbox')
409
- end
410
-
411
- #
412
- # platform method to retrieve all checkbox elements
413
- #
414
- def checkboxs_for(identifier)
415
- find_selenium_elements(identifier, Elements::CheckBox, 'input', :type => 'checkbox')
416
- end
417
-
418
- #
419
- # platform method to select a radio button
420
- # See PageObject::Accessors#radio_button
421
- #
422
- def select_radio(identifier)
423
- process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|
424
- @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?
425
- end
426
- end
427
-
428
- #
429
- # platform method to determine if a radio button is selected
430
- # See PageObject::Accessors#radio_button
431
- #
432
- def radio_selected?(identifier)
433
- process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|
434
- @browser.find_element(how, what).selected?
435
- end
436
- end
437
-
438
- #
439
- # platform method to return a PageObject::Eements::RadioButton element
440
- # See PageObject::Accessors#radio_button
441
- #
442
- def radio_button_for(identifier)
443
- find_selenium_element(identifier, Elements::RadioButton, 'input', :type => 'radio')
444
- end
445
-
446
- #
447
- # platform method to retrieve all radio button elements
448
- #
449
- def radio_buttons_for(identifier)
450
- find_selenium_elements(identifier, Elements::RadioButton, 'input', :type => 'radio')
451
- end
452
-
453
- #
454
- # platform method to return the text for a div
455
- # See PageObject::Accessors#div
456
- #
457
- def div_text_for(identifier)
458
- process_selenium_call(identifier, Elements::Div, 'div') do |how, what|
459
- @browser.find_element(how, what).text
460
- end
461
- end
462
-
463
- #
464
- # platform method to return a PageObject::Elements::Div element
465
- # See PageObject::Accessors#div
466
- #
467
- def div_for(identifier)
468
- find_selenium_element(identifier, Elements::Div, 'div')
469
- end
470
-
471
- #
472
- # platform method to retrieve all div elements
473
- #
474
- def divs_for(identifier)
475
- find_selenium_elements(identifier, Elements::Div, 'div')
476
- end
477
-
478
- #
479
- # platform method to return the text for a span
480
- # See PageObject::Accessors#span
481
- #
482
- def span_text_for(identifier)
483
- process_selenium_call(identifier, Elements::Span, 'span') do |how, what|
484
- @browser.find_element(how, what).text
485
- end
486
- end
487
-
488
- #
489
- # platform method to return a PageObject::Elements::Span element
490
- # See PageObject::Accessors#span
491
- #
492
- def span_for(identifier)
493
- find_selenium_element(identifier, Elements::Span, 'span')
494
- end
495
-
496
- #
497
- # platform method to retrieve all span elements
498
- #
499
- def spans_for(identifier)
500
- find_selenium_elements(identifier, Elements::Span, 'span')
501
- end
502
-
503
- #
504
- # platform method to click a button
505
- # See PageObject::Accessors#button
506
- #
507
- def click_button_for(identifier)
508
- process_selenium_call(identifier, Elements::Button, 'input', :type => 'submit') do |how, what|
509
- @browser.find_element(how, what).click
510
- end
511
- end
512
-
513
- #
514
- # platform method to retrieve a button element
515
- # See PageObject::Accessors#button
516
- #
517
- def button_for(identifier)
518
- find_selenium_element(identifier, Elements::Button, 'input', :type => 'submit')
519
- end
520
-
521
- #
522
- # platform method to retrieve an array of button elements
523
- #
524
- def buttons_for(identifier)
525
- find_selenium_elements(identifier, Elements::Button, 'input', :type => 'submit')
526
- end
527
-
528
- #
529
- # platform method to return the text for a table
530
- # See PageObject::Accessors#table
531
- #
532
- def table_text_for(identifier)
533
- process_selenium_call(identifier, Elements::Table, 'table') do |how, what|
534
- @browser.find_element(how, what).text
535
- end
536
- end
537
-
538
- #
539
- # platform method to retrieve a table element
540
- # See PageObject::Accessors#table
541
- #
542
- def table_for(identifier)
543
- find_selenium_element(identifier, Elements::Table, 'table')
544
- end
545
-
546
- #
547
- # platform method to retrieve all table elements
548
- #
549
- def tables_for(identifier)
550
- find_selenium_elements(identifier, Elements::Table, 'table')
551
- end
552
-
553
- #
554
- # platform method to retrieve the text from a table cell
555
- # See PageObject::Accessors#cell
556
- #
557
- def cell_text_for(identifier)
558
- process_selenium_call(identifier, Elements::TableCell, 'td') do |how, what|
559
- @browser.find_element(how, what).text
560
- end
561
- end
562
-
563
- #
564
- # platform method to retrieve a table cell element
565
- # See PageObject::Accessors#cell
566
- #
567
- def cell_for(identifier)
568
- find_selenium_element(identifier, Elements::TableCell, 'td')
569
- end
570
-
571
- #
572
- # platform method to retrieve all table cell elements
573
- #
574
- def cells_for(identifier)
575
- find_selenium_elements(identifier, Elements::TableCell, 'td')
576
- end
577
-
578
- #
579
- # platform method to retrieve an image element
580
- # See PageObject::Accessors#image
581
- #
582
- def image_for(identifier)
583
- find_selenium_element(identifier, Elements::Image, 'img')
584
- end
585
-
586
- #
587
- # platform method to retrieve all image elements
588
- #
589
- def images_for(identifier)
590
- find_selenium_elements(identifier, Elements::Image, 'img')
591
- end
592
-
593
- #
594
- # platform method to retrieve a form element
595
- # See PageObject::Accessors#form
596
- #
597
- def form_for(identifier)
598
- find_selenium_element(identifier, Elements::Form, 'form')
599
- end
600
-
601
- #
602
- # platform method to retrieve all forms
603
- #
604
- def forms_for(identifier)
605
- find_selenium_elements(identifier, Elements::Form, 'form')
606
- end
607
-
608
- #
609
- # platform method to retrieve the text from a list item
610
- # See PageObject::Accessors#list_item
611
- #
612
- def list_item_text_for(identifier)
613
- process_selenium_call(identifier, Elements::ListItem, 'li') do |how, what|
614
- @browser.find_element(how, what).text
615
- end
616
- end
617
-
618
- #
619
- # platform method to retrieve a list item element
620
- # See PageObject::Accessors#list_item
621
- #
622
- def list_item_for(identifier)
623
- find_selenium_element(identifier, Elements::ListItem, 'li')
624
- end
625
-
626
- #
627
- # platform method to retrieve all list items
628
- #
629
- def list_items_for(identifier)
630
- find_selenium_elements(identifier, Elements::ListItem, 'li')
631
- end
632
-
633
- #
634
- # platform method to retrieve the text from an unordered list
635
- # See PageObject::Accessors#unordered_list
636
- #
637
- def unordered_list_text_for(identifier)
638
- process_selenium_call(identifier, Elements::UnorderedList, 'ul') do |how, what|
639
- @browser.find_element(how, what).text
640
- end
641
- end
642
-
643
- #
644
- # platform method to retrieve an unordered list element
645
- # See PageObject::Accessors#unordered_list
646
- #
647
- def unordered_list_for(identifier)
648
- find_selenium_element(identifier, Elements::UnorderedList, 'ul')
649
- end
650
-
651
- #
652
- # platform method to retrieve all unordered lists
653
- #
654
- def unordered_lists_for(identifier)
655
- find_selenium_elements(identifier, Elements::UnorderedList, 'ul')
656
- end
657
-
658
- #
659
- # platform method to retrieve the text from an ordered list
660
- # See PageObject::Accessors#ordered_list
661
- #
662
- def ordered_list_text_for(identifier)
663
- process_selenium_call(identifier, Elements::OrderedList, 'ol') do |how, what|
664
- @browser.find_element(how, what).text
665
- end
666
- end
667
-
668
- #
669
- # platform method to retrieve an ordered list element
670
- # See PageObject::Accessors#ordered_list
671
- #
672
- def ordered_list_for(identifier)
673
- find_selenium_element(identifier, Elements::OrderedList, 'ol')
674
- end
675
-
676
- #
677
- # platform method to retrieve all ordered lists
678
- #
679
- def ordered_lists_for(identifier)
680
- find_selenium_elements(identifier, Elements::OrderedList, 'ol')
681
- end
682
-
683
- #
684
- # platform method to retrieve the text from a h1
685
- # See PageObject::Accessors#h1
686
- #
687
- def h1_text_for(identifier)
688
- process_selenium_call(identifier, Elements::Heading, 'h1') do |how, what|
689
- @browser.find_element(how, what).text
690
- end
691
- end
692
-
693
- #
694
- # platform method to retrieve the h1 element
695
- # See PageObject::Accessors#h1
696
- #
697
- def h1_for(identifier)
698
- find_selenium_element(identifier, Elements::Heading, 'h1')
699
- end
700
-
701
- #
702
- # platform method to retrieve all h1 elements
703
- #
704
- def h1s_for(identifier)
705
- find_selenium_elements(identifier, Elements::Heading, 'h1')
706
- end
707
-
708
- #
709
- # platform method to retrieve the text from a h2
710
- # See PageObject::Accessors#h2
711
- #
712
- def h2_text_for(identifier)
713
- process_selenium_call(identifier, Elements::Heading, 'h2') do |how, what|
714
- @browser.find_element(how, what).text
715
- end
716
- end
717
-
718
- #
719
- # platform method to retrieve the h2 element
720
- # See PageObject::Accessors#h2
721
- #
722
- def h2_for(identifier)
723
- find_selenium_element(identifier, Elements::Heading, 'h2')
724
- end
725
-
726
- #
727
- # platform method to retrieve all h2 elements
728
- #
729
- def h2s_for(identifier)
730
- find_selenium_elements(identifier, Elements::Heading, 'h2')
731
- end
732
-
733
- #
734
- # platform method to retrieve the text from a h3
735
- # See PageObject::Accessors#h3
736
- #
737
- def h3_text_for(identifier)
738
- process_selenium_call(identifier, Elements::Heading, 'h3') do |how, what|
739
- @browser.find_element(how, what).text
740
- end
741
- end
742
-
743
- #
744
- # platform method to retrieve the h3 element
745
- # See PageObject::Accessors#h3
746
- #
747
- def h3_for(identifier)
748
- find_selenium_element(identifier, Elements::Heading, 'h3')
749
- end
750
-
751
- #
752
- # platform method to retrieve all h3 elements
753
- #
754
- def h3s_for(identifier)
755
- find_selenium_elements(identifier, Elements::Heading, 'h3')
756
- end
757
-
758
- #
759
- # platform method to retrieve the text from a h4
760
- # See PageObject::Accessors#h4
761
- #
762
- def h4_text_for(identifier)
763
- process_selenium_call(identifier, Elements::Heading, 'h4') do |how, what|
764
- @browser.find_element(how, what).text
765
- end
766
- end
767
-
768
- #
769
- # platform method to retrieve the h4 element
770
- # See PageObject::Accessors#h4
771
- #
772
- def h4_for(identifier)
773
- find_selenium_element(identifier, Elements::Heading, 'h4')
774
- end
775
-
776
- #
777
- # platform method to retrieve all h4 elements
778
- #
779
- def h4s_for(identifier)
780
- find_selenium_elements(identifier, Elements::Heading, 'h4')
781
- end
782
-
783
- #
784
- # platform method to retrieve the text from a h5
785
- # See PageObject::Accessors#h5
786
- #
787
- def h5_text_for(identifier)
788
- process_selenium_call(identifier, Elements::Heading, 'h5') do |how, what|
789
- @browser.find_element(how, what).text
790
- end
791
- end
792
-
793
- #
794
- # platform method to retrieve the h5 element
795
- # See PageObject::Accessors#h5
796
- #
797
- def h5_for(identifier)
798
- find_selenium_element(identifier, Elements::Heading, 'h5')
799
- end
800
-
801
- #
802
- # platform method to retrieve all h5 elements
803
- #
804
- def h5s_for(identifier)
805
- find_selenium_elements(identifier, Elements::Heading, 'h5')
806
- end
807
-
808
- #
809
- # platform method to retrieve the text from a h6
810
- # See PageObject::Accessors#h6
811
- #
812
- def h6_text_for(identifier)
813
- process_selenium_call(identifier, Elements::Heading, 'h6') do |how, what|
814
- @browser.find_element(how, what).text
815
- end
816
- end
817
-
818
- #
819
- # platform method to retrieve the h6 element
820
- # See PageObject::Accessors#h6
821
- #
822
- def h6_for(identifier)
823
- find_selenium_element(identifier, Elements::Heading, 'h6')
824
- end
825
-
826
- #
827
- # platform method to retrieve all h6 elements
828
- #
829
- def h6s_for(identifier)
830
- find_selenium_elements(identifier, Elements::Heading, 'h6')
831
- end
832
-
833
- #
834
- # platform method to retrieve the text for a paragraph
835
- # See PageObject::Accessors#paragraph
836
- #
837
- def paragraph_text_for(identifier)
838
- process_selenium_call(identifier, Elements::Paragraph, 'p') do |how, what|
839
- @browser.find_element(how, what).text
840
- end
841
- end
842
-
843
- #
844
- # platform method to retrieve the paragraph element
845
- # See PageObject::Accessors#paragraph
846
- #
847
- def paragraph_for(identifier)
848
- find_selenium_element(identifier, Elements::Paragraph, 'p')
849
- end
850
-
851
- #
852
- # platform method to retrieve all paragraph elements
853
- #
854
- def paragraphs_for(identifier)
855
- find_selenium_elements(identifier, Elements::Paragraph, 'p')
856
- end
857
-
858
- #
859
- # platform method to return the text for a label
860
- # See PageObject::Accessors#label
861
- #
862
- def label_text_for(identifier)
863
- process_selenium_call(identifier, Elements::Label, 'label') do |how, what|
864
- @browser.find_element(how, what).text
865
- end
866
- end
867
-
868
-
869
- #
870
- # platform method to return a PageObject::Elements::Label element
871
- # See PageObject::Accessors#label
872
- #
873
- def label_for(identifier)
874
- find_selenium_element(identifier, Elements::Label, 'label')
875
- end
876
-
877
-
878
- #
879
- # platform method to retrieve all label elements
880
- #
881
- def labels_for(identifier)
882
- find_selenium_elements(identifier, Elements::Label, 'label')
883
- end
884
-
885
- #
886
- # platform method to set the file on a file_field element
887
- # See PageObject::Accessors#file_field
888
- #
889
- def file_field_value_set(identifier, value)
890
- process_selenium_call(identifier, Elements::FileField, 'input', :type => 'file') do |how, what|
891
- @browser.find_element(how, what).send_keys(value)
892
- end
893
- end
894
-
895
- #
896
- # platform method to retrieve a file_field element
897
- # See PageObject::Accessors#file_field
898
- #
899
- def file_field_for(identifier)
900
- find_selenium_element(identifier, Elements::FileField, 'input', :type => 'file')
901
- end
902
-
903
- #
904
- # platform method to return an array of file field elements
905
- #
906
- def file_fields_for(identifier)
907
- find_selenium_elements(identifier, Elements::FileField, 'input', :type => 'file')
908
- end
909
-
910
- #
911
- # platform method to click on an area
912
- #
913
- def click_area_for(identifier)
914
- process_selenium_call(identifier, Elements::Area, 'area') do |how, what|
915
- @browser.find_element(how, what).click
916
- end
917
- end
918
-
919
- #
920
- # platform method to retrieve an area element
921
- #
922
- def area_for(identifier)
923
- find_selenium_element(identifier, Elements::Area, 'area')
924
- end
925
-
926
- #
927
- # platform method to return an array of area elements
928
- #
929
- def areas_for(identifier)
930
- find_selenium_elements(identifier, Elements::Area, 'area')
931
- end
932
-
933
- #
934
- # platform method to retrieve a canvas element
935
- #
936
- def canvas_for(identifier)
937
- find_selenium_element(identifier, Elements::Canvas, 'canvas')
938
- end
939
-
940
- #
941
- # platform method to return an array of canvas elements
942
- #
943
- def canvass_for(identifier)
944
- find_selenium_elements(identifier, Elements::Canvas, 'canvas')
945
- end
946
-
947
- #
948
- # platform method to retrieve an audio element
949
- #
950
- def audio_for(identifier)
951
- find_selenium_element(identifier, Elements::Audio, 'audio')
952
- end
953
-
954
- #
955
- # platform method to return an array of audio elements
956
- #
957
- def audios_for(identifier)
958
- find_selenium_elements(identifier, Elements::Audio, 'audio')
959
- end
960
-
961
- #
962
- # platform method to retrieve a video element
963
- #
964
- def video_for(identifier)
965
- find_selenium_element(identifier, Elements::Video, 'video')
966
- end
967
-
968
- #
969
- # platform method to return an array of video elements
970
- #
971
- def videos_for(identifier)
972
- find_selenium_elements(identifier, Elements::Video, 'video')
973
- end
974
-
975
- #
976
- # platform method to retrieve a generic element
977
- # See PageObject::Accessors#element
978
- #
979
- def element_for(tag, identifier)
980
- find_selenium_element(identifier, Elements::Element, tag.to_s)
981
- end
982
-
983
- #
984
- # platform method to retrieve a collection of generic elements
985
- # See PageObject::Accessors#elements
986
- #
987
- def elements_for(tag, identifier)
988
- find_selenium_elements(identifier, Elements::Element, tag.to_s)
989
- end
990
-
991
- #
992
- # platform method to return a svg element
993
- #
994
- def svg_for(identifier)
995
- find_selenium_element(identifier, Elements::Element, 'svg')
996
- end
997
-
998
- #
999
- # platform method to return an array of svg elements
1000
- #
1001
- def svgs_for(identifier)
1002
- find_selenium_elements(identifier, Elements::Element, 'svg')
1003
- end
1004
-
1005
- private
1006
-
1007
- def process_selenium_call(identifier, type, tag, other=nil)
1008
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1009
- switch_to_frame(frame_identifiers)
1010
- value = yield how, what
1011
- @browser.switch_to.default_content unless frame_identifiers.nil?
1012
- value
1013
- end
1014
-
1015
- def find_selenium_element(identifier, type, tag, other=nil)
1016
- regexp = delete_regexp(identifier)
1017
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1018
- switch_to_frame(frame_identifiers)
1019
- begin
1020
- unless regexp
1021
- element = @browser.find_element(how, what)
1022
- else
1023
- elements = @browser.find_elements(how, what)
1024
- element = elements.find {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1025
- end
1026
- rescue Selenium::WebDriver::Error::NoSuchElementError
1027
- @browser.switch_to.default_content unless frame_identifiers.nil?
1028
- return build_null_object(identifier, type, tag, other)
1029
- end
1030
- @browser.switch_to.default_content unless frame_identifiers.nil?
1031
- type.new(element, :platform => :selenium_webdriver)
1032
- end
1033
-
1034
- def find_selenium_elements(identifier, type, tag, other=nil)
1035
- regexp = delete_regexp(identifier)
1036
- how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1037
- switch_to_frame(frame_identifiers)
1038
- unless regexp
1039
- elements = @browser.find_elements(how, what)
1040
- else
1041
- eles = @browser.find_elements(how, what)
1042
- elements = eles.find_all {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1043
- end
1044
- @browser.switch_to.default_content unless frame_identifiers.nil?
1045
- elements.map { |element| type.new(element, :platform => :selenium_webdriver) }
1046
- end
1047
-
1048
- def build_null_object(identifier, type, tag, other)
1049
- null_element = SurrogateSeleniumElement.new
1050
- null_element.identifier = identifier
1051
- null_element.type = type
1052
- null_element.tag = tag
1053
- null_element.other = other
1054
- null_element.platform = self
1055
- Elements::Element.new(null_element, :platform => :selenium_webdriver)
1056
- end
1057
-
1058
- def delete_regexp(identifier)
1059
- regexp = identifier.find {|ident| ident[1].is_a?(Regexp)}
1060
- identifier.delete(regexp[0]) if regexp
1061
- regexp
1062
- end
1063
-
1064
- def parse_identifiers(identifier, element, tag_name=nil, additional=nil)
1065
- frame_identifiers = identifier.delete(:frame)
1066
- identifier = add_tagname_if_needed identifier, tag_name, additional if tag_name
1067
- how, what = element.selenium_identifier_for identifier
1068
- return how, what, frame_identifiers
1069
- end
1070
-
1071
- def add_tagname_if_needed identifier, tag, additional=nil
1072
- return identifier if identifier.length < 2 and supported_identifier(identifier, tag, additional)
1073
- identifier[:tag_name] = tag
1074
- if additional
1075
- additional.each do |key, value|
1076
- identifier[key] = value
1077
- end
1078
- end
1079
- identifier
1080
- end
1081
-
1082
- def supported_identifier(identifier, tag, additional)
1083
- return false if identifier.size == 0
1084
- return false if identifier[:index]
1085
- return false if identifier[:action] and tag == 'form'
1086
- return false if identifier[:alt] and tag == 'img'
1087
- return false if identifier[:alt] and tag == 'input' and
1088
- ['submit', 'image', 'button', 'reset'].include? additional[:type]
1089
- return false if identifier[:href] and tag == 'a'
1090
- return false if identifier[:src] and tag == 'input' and
1091
- ['submit', 'image', 'button', 'reset'].include? additional[:type]
1092
- return false if identifier[:src] and tag == 'img'
1093
- return false if identifier[:label]
1094
- return false if identifier[:text] and tag == 'input' and additional[:type] == 'hidden'
1095
- return false if identifier[:text] and tag == 'input' and additional[:type] == 'text'
1096
- return false if identifier[:text] and ['div', 'span', 'td', 'label', 'li'].include? tag
1097
- return false if identifier[:title] and tag == 'input' and additional[:type] == 'text'
1098
- return false if identifier[:title] and tag == 'input' and additional[:type] == 'file'
1099
- return false if identifier[:title] and tag == 'a'
1100
- return false if identifier[:title] and tag == 'span'
1101
- return false if identifier[:title] and tag == 'div'
1102
- return false if identifier[:value] and tag == 'input' and
1103
- ['radio', 'submit', 'image', 'button', 'reset', 'checkbox', 'hidden'].include? additional[:type]
1104
- true
1105
- end
1106
-
1107
- def matches_selector?(element, how, what)
1108
- what === fetch_value(element, how)
1109
- end
1110
-
1111
- def fetch_value(element, how)
1112
- case how
1113
- when :text
1114
- element.text
1115
- when :tag_name
1116
- element.tag_name.downcase
1117
- when :href
1118
- (href = element.attribute(:href)) && href.strip
1119
- else
1120
- element.attribute(how.to_s.gsub("_", "-").to_sym)
1121
- end
1122
- end
1123
-
1124
- def switch_to_frame(frame_identifiers)
1125
- unless frame_identifiers.nil?
1126
- frame_identifiers.each do |frame|
1127
- frame_id = frame.values.first
1128
- value = frame_id.values.first
1129
- @browser.switch_to.frame(value)
1130
- end
1131
- end
1132
- end
1133
- end
1134
- end
1135
- end
1136
- end
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
+ def initialize(browser)
16
+ @browser = browser
17
+ end
18
+
19
+ #
20
+ # platform method to navigate to a provided url
21
+ # See PageObject#navigate_to
22
+ #
23
+ def navigate_to(url)
24
+ @browser.navigate.to url
25
+ end
26
+
27
+ #
28
+ # platform method to get the current url
29
+ # See PageObject#current_url
30
+ #
31
+ def current_url
32
+ @browser.current_url
33
+ end
34
+
35
+ #
36
+ # platform method to retrieve the text from the current page
37
+ # See PageObject#text
38
+ #
39
+ def text
40
+ @browser.find_element(:tag_name, 'body').text
41
+ end
42
+
43
+ #
44
+ # platform method to retrieve the html for the current page
45
+ # See PageObject#html
46
+ #
47
+ def html
48
+ @browser.page_source
49
+ end
50
+
51
+ #
52
+ # platform method to retrieve the title for the current page
53
+ # See PageObject#title
54
+ #
55
+ def title
56
+ @browser.title
57
+ end
58
+
59
+ #
60
+ # platform method to wait for a block to return true
61
+ # See PageObject#wait_until
62
+ #
63
+ def wait_until(timeout, message = nil, &block)
64
+ wait = ::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => message})
65
+ wait.until &block
66
+ end
67
+
68
+ #
69
+ # platform method to handle an alert popup
70
+ # See PageObject#alert
71
+ #
72
+ def alert(frame=nil, &block)
73
+ yield
74
+ begin
75
+ alert = @browser.switch_to.alert
76
+ value = alert.text
77
+ alert.accept
78
+ rescue Selenium::WebDriver::Error::NoAlertPresentError
79
+ end
80
+ value
81
+ end
82
+
83
+ #
84
+ # platform method to handle a confirm popup
85
+ # See PageObject#confirm
86
+ #
87
+ def confirm(response, frame=nil, &block)
88
+ yield
89
+ begin
90
+ alert = @browser.switch_to.alert
91
+ value = alert.text
92
+ response ? alert.accept : alert.dismiss
93
+ rescue Selenium::WebDriver::Error::NoAlertPresentError
94
+ end
95
+ value
96
+ end
97
+
98
+ #
99
+ # platform method to handle a prompt popup
100
+ # See PageObject#prompt
101
+ #
102
+ def prompt(answer, frame=nil, &block)
103
+ @browser.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{answer}; }"
104
+ yield
105
+ result = @browser.execute_script "return window.__lastWatirPrompt"
106
+ result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
107
+ result
108
+ end
109
+
110
+ #
111
+ # platform method to execute javascript on the browser
112
+ # See PageObject#execute_script
113
+ #
114
+ def execute_script(script, *args)
115
+ @browser.execute_script(script, *args)
116
+ end
117
+
118
+ #
119
+ # platform method to handle attaching to a running window
120
+ # See PageObject#attach_to_window
121
+ #
122
+ def attach_to_window(identifier, &block)
123
+ value = identifier.values.first
124
+ key = identifier.keys.first
125
+ handles = @browser.window_handles
126
+ handles.each do |handle|
127
+ @browser.switch_to.window handle
128
+ if (key == :title and value == @browser.title) or
129
+ (key == :url and @browser.current_url.include?(value))
130
+ return @browser.switch_to.window handle, &block
131
+ end
132
+ end
133
+ end
134
+
135
+ #
136
+ # find the element that has focus
137
+ #
138
+ def element_with_focus
139
+ element = @browser.execute_script("return document.activeElement")
140
+ type = element.attribute(:type).to_s.downcase if element.tag_name.to_sym == :input
141
+ cls = ::PageObject::Elements.element_class_for(element.tag_name, type)
142
+ cls.new(element, :platform => :selenium_webdriver)
143
+ end
144
+
145
+ #
146
+ # platform method to switch to a frame and execute a block
147
+ # See PageObject#in_frame
148
+ #
149
+ def in_frame(identifier, frame=nil, &block)
150
+ switch_to_frame([frame: identifier])
151
+ block.call(nil)
152
+ @browser.switch_to.default_content
153
+ end
154
+
155
+ #
156
+ # platform method to switch to an iframe and execute a block
157
+ # See PageObject#in_frame
158
+ #
159
+ def in_iframe(identifier, frame=nil, &block)
160
+ switch_to_frame([iframe: identifier])
161
+ block.call(nil)
162
+ @browser.switch_to.default_content
163
+ end
164
+
165
+ #
166
+ # platform method to refresh the page
167
+ # See PageObject#refresh
168
+ #
169
+ def refresh
170
+ @browser.navigate.refresh
171
+ end
172
+
173
+ #
174
+ # platform method to go back to the previous page
175
+ # See PageObject#back
176
+ #
177
+ def back
178
+ @browser.navigate.back
179
+ end
180
+
181
+ #
182
+ # platform method to go forward to the next page
183
+ # See PageObject#forward
184
+ #
185
+ def forward
186
+ @browser.navigate.forward
187
+ end
188
+
189
+ #
190
+ # platform method to clear the cookies from the browser
191
+ # See PageObject#clear_cookies
192
+ #
193
+ def clear_cookies
194
+ @browser.manage.delete_all_cookies
195
+ end
196
+
197
+ #
198
+ # platform method to save the current screenshot to a file
199
+ # See PageObject#save_screenshot
200
+ #
201
+ def save_screenshot(file_name)
202
+ @browser.save_screenshot(file_name)
203
+ end
204
+
205
+ #
206
+ # platform method to get the value stored in a text field
207
+ # See PageObject::Accessors#text_field
208
+ #
209
+ def text_field_value_for(identifier)
210
+ process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|
211
+ @browser.find_element(how, what).attribute('value')
212
+ end
213
+ end
214
+
215
+
216
+ #
217
+ # platform method to set the value for a text field
218
+ # See PageObject::Accessors#text_field
219
+ #
220
+ def text_field_value_set(identifier, value)
221
+ process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|
222
+ @browser.find_element(how, what).clear
223
+ @browser.find_element(how, what).send_keys(value)
224
+ end
225
+ end
226
+
227
+ #
228
+ # platform method to retrieve a text field element
229
+ # See PageObject::Accessors#text_field
230
+ #
231
+ def text_field_for(identifier)
232
+ find_selenium_element(identifier, Elements::TextField, 'input', :type => 'text')
233
+ end
234
+
235
+ #
236
+ # platform method to retrieve all text field elements
237
+ #
238
+ def text_fields_for(identifier)
239
+ find_selenium_elements(identifier, Elements::TextField, 'input', :type => 'text')
240
+ end
241
+
242
+ #
243
+ # platform method to get the value stored in a hidden field
244
+ # See PageObject::Accessors#hidden_field
245
+ #
246
+ def hidden_field_value_for(identifier)
247
+ process_selenium_call(identifier, Elements::HiddenField, 'input', :type => 'hidden') do |how, what|
248
+ @browser.find_element(how, what).attribute('value')
249
+ end
250
+ end
251
+
252
+ #
253
+ # platform method to retrieve a hidden field element
254
+ # See PageObject::Accessors#hidden_field
255
+ #
256
+ def hidden_field_for(identifier)
257
+ find_selenium_element(identifier, Elements::HiddenField, 'input', :type => 'hidden')
258
+ end
259
+
260
+ #
261
+ # platform method to retrieve all hidden field elements
262
+ #
263
+ def hidden_fields_for(identifier)
264
+ find_selenium_elements(identifier, Elements::HiddenField, 'input', :type => 'hidden')
265
+ end
266
+
267
+ #
268
+ # platform method to set text in a textarea
269
+ # See PageObject::Accessors#text_area
270
+ #
271
+ def text_area_value_set(identifier, value)
272
+ process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|
273
+ text_area = @browser.find_element(how, what)
274
+ text_area.clear
275
+ text_area.send_keys(value)
276
+ end
277
+ end
278
+
279
+ #
280
+ # platform method to get the text from a textarea
281
+ # See PageObject::Accessors#text_area
282
+ #
283
+ def text_area_value_for(identifier)
284
+ process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|
285
+ @browser.find_element(how, what).attribute('value')
286
+ end
287
+ end
288
+
289
+ #
290
+ # platform method to get the text area element
291
+ # See PageObject::Accessors#text_area
292
+ #
293
+ def text_area_for(identifier)
294
+ find_selenium_element(identifier, Elements::TextArea, 'textarea')
295
+ end
296
+
297
+ #
298
+ # platform method to retrieve all text area elements
299
+ #
300
+ def text_areas_for(identifier)
301
+ find_selenium_elements(identifier, Elements::TextArea, 'textarea')
302
+ end
303
+
304
+ #
305
+ # platform method to get the currently selected value from a select list
306
+ # See PageObject::Accessors#select_list
307
+ #
308
+ def select_list_value_for(identifier)
309
+ process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|
310
+ selected = nil
311
+ @browser.find_element(how, what).find_elements(:tag_name => 'option').each do |o|
312
+ if selected.nil?
313
+ selected = o.text if o.selected?
314
+ end
315
+ end
316
+ selected
317
+ end
318
+ end
319
+
320
+ #
321
+ # platform method to select a value from a select list
322
+ # See PageObject::Accessors#select_list
323
+ #
324
+ def select_list_value_set(identifier, value)
325
+ process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|
326
+ select_list = @browser.find_element(how, what)
327
+ select_list.find_elements(:tag_name => 'option').find do |option|
328
+ option.text == value
329
+ end.click
330
+ end
331
+ end
332
+
333
+ #
334
+ # platform method to return the select list element
335
+ # See PageObject::Accessors#select_list
336
+ #
337
+ def select_list_for(identifier)
338
+ find_selenium_element(identifier, Elements::SelectList, 'select')
339
+ end
340
+
341
+ #
342
+ # platform method to retrieve all select list elements
343
+ #
344
+ def select_lists_for(identifier)
345
+ find_selenium_elements(identifier, Elements::SelectList, 'select')
346
+ end
347
+
348
+ #
349
+ # platform method to click a link
350
+ # See PageObject::Accessors#link
351
+ #
352
+ def click_link_for(identifier)
353
+ process_selenium_call(identifier, Elements::Link, 'a') do |how, what|
354
+ @browser.find_element(how, what).click
355
+ end
356
+ end
357
+
358
+ #
359
+ # platform method to return a PageObject::Elements::Link object
360
+ # see PageObject::Accessors#link
361
+ #
362
+ def link_for(identifier)
363
+ find_selenium_element(identifier, Elements::Link, 'a')
364
+ end
365
+
366
+ #
367
+ # platform method to retrieve all link elements
368
+ #
369
+ def links_for(identifier)
370
+ find_selenium_elements(identifier, Elements::Link, 'a')
371
+ end
372
+
373
+ #
374
+ # platform method to check a checkbox
375
+ # See PageObject::Accessors#checkbox
376
+ #
377
+ def check_checkbox(identifier)
378
+ process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
379
+ @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?
380
+ end
381
+ end
382
+
383
+ #
384
+ # platform method to uncheck a checkbox
385
+ # See PageObject::Accessors#checkbox
386
+ #
387
+ def uncheck_checkbox(identifier)
388
+ process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
389
+ @browser.find_element(how, what).click if @browser.find_element(how, what).selected?
390
+ end
391
+ end
392
+
393
+ #
394
+ # platform method to determine if a checkbox is checked
395
+ # See PageObject::Accessors#checkbox
396
+ #
397
+ def checkbox_checked?(identifier)
398
+ process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|
399
+ @browser.find_element(how, what).selected?
400
+ end
401
+ end
402
+
403
+ #
404
+ # platform method to return a PageObject::Elements::CheckBox element
405
+ # See PageObject::Accessors#checkbox
406
+ #
407
+ def checkbox_for(identifier)
408
+ find_selenium_element(identifier, Elements::CheckBox, 'input', :type => 'checkbox')
409
+ end
410
+
411
+ #
412
+ # platform method to retrieve all checkbox elements
413
+ #
414
+ def checkboxs_for(identifier)
415
+ find_selenium_elements(identifier, Elements::CheckBox, 'input', :type => 'checkbox')
416
+ end
417
+
418
+ #
419
+ # platform method to select a radio button
420
+ # See PageObject::Accessors#radio_button
421
+ #
422
+ def select_radio(identifier)
423
+ process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|
424
+ @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?
425
+ end
426
+ end
427
+
428
+ #
429
+ # platform method to determine if a radio button is selected
430
+ # See PageObject::Accessors#radio_button
431
+ #
432
+ def radio_selected?(identifier)
433
+ process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|
434
+ @browser.find_element(how, what).selected?
435
+ end
436
+ end
437
+
438
+ #
439
+ # platform method to return a PageObject::Eements::RadioButton element
440
+ # See PageObject::Accessors#radio_button
441
+ #
442
+ def radio_button_for(identifier)
443
+ find_selenium_element(identifier, Elements::RadioButton, 'input', :type => 'radio')
444
+ end
445
+
446
+ #
447
+ # platform method to retrieve all radio button elements
448
+ #
449
+ def radio_buttons_for(identifier)
450
+ find_selenium_elements(identifier, Elements::RadioButton, 'input', :type => 'radio')
451
+ end
452
+
453
+ #
454
+ # platform method to return the text for a div
455
+ # See PageObject::Accessors#div
456
+ #
457
+ def div_text_for(identifier)
458
+ process_selenium_call(identifier, Elements::Div, 'div') do |how, what|
459
+ @browser.find_element(how, what).text
460
+ end
461
+ end
462
+
463
+ #
464
+ # platform method to return a PageObject::Elements::Div element
465
+ # See PageObject::Accessors#div
466
+ #
467
+ def div_for(identifier)
468
+ find_selenium_element(identifier, Elements::Div, 'div')
469
+ end
470
+
471
+ #
472
+ # platform method to retrieve all div elements
473
+ #
474
+ def divs_for(identifier)
475
+ find_selenium_elements(identifier, Elements::Div, 'div')
476
+ end
477
+
478
+ #
479
+ # platform method to return the text for a span
480
+ # See PageObject::Accessors#span
481
+ #
482
+ def span_text_for(identifier)
483
+ process_selenium_call(identifier, Elements::Span, 'span') do |how, what|
484
+ @browser.find_element(how, what).text
485
+ end
486
+ end
487
+
488
+ #
489
+ # platform method to return a PageObject::Elements::Span element
490
+ # See PageObject::Accessors#span
491
+ #
492
+ def span_for(identifier)
493
+ find_selenium_element(identifier, Elements::Span, 'span')
494
+ end
495
+
496
+ #
497
+ # platform method to retrieve all span elements
498
+ #
499
+ def spans_for(identifier)
500
+ find_selenium_elements(identifier, Elements::Span, 'span')
501
+ end
502
+
503
+ #
504
+ # platform method to click a button
505
+ # See PageObject::Accessors#button
506
+ #
507
+ def click_button_for(identifier)
508
+ process_selenium_call(identifier, Elements::Button, 'input', :type => 'submit') do |how, what|
509
+ @browser.find_element(how, what).click
510
+ end
511
+ end
512
+
513
+ #
514
+ # platform method to retrieve a button element
515
+ # See PageObject::Accessors#button
516
+ #
517
+ def button_for(identifier)
518
+ find_selenium_element(identifier, Elements::Button, 'input', :type => 'submit')
519
+ end
520
+
521
+ #
522
+ # platform method to retrieve an array of button elements
523
+ #
524
+ def buttons_for(identifier)
525
+ find_selenium_elements(identifier, Elements::Button, 'input', :type => 'submit')
526
+ end
527
+
528
+ #
529
+ # platform method to return the text for a table
530
+ # See PageObject::Accessors#table
531
+ #
532
+ def table_text_for(identifier)
533
+ process_selenium_call(identifier, Elements::Table, 'table') do |how, what|
534
+ @browser.find_element(how, what).text
535
+ end
536
+ end
537
+
538
+ #
539
+ # platform method to retrieve a table element
540
+ # See PageObject::Accessors#table
541
+ #
542
+ def table_for(identifier)
543
+ find_selenium_element(identifier, Elements::Table, 'table')
544
+ end
545
+
546
+ #
547
+ # platform method to retrieve all table elements
548
+ #
549
+ def tables_for(identifier)
550
+ find_selenium_elements(identifier, Elements::Table, 'table')
551
+ end
552
+
553
+ #
554
+ # platform method to retrieve the text from a table cell
555
+ # See PageObject::Accessors#cell
556
+ #
557
+ def cell_text_for(identifier)
558
+ process_selenium_call(identifier, Elements::TableCell, 'td') do |how, what|
559
+ @browser.find_element(how, what).text
560
+ end
561
+ end
562
+
563
+ #
564
+ # platform method to retrieve a table cell element
565
+ # See PageObject::Accessors#cell
566
+ #
567
+ def cell_for(identifier)
568
+ find_selenium_element(identifier, Elements::TableCell, 'td')
569
+ end
570
+
571
+ #
572
+ # platform method to retrieve all table cell elements
573
+ #
574
+ def cells_for(identifier)
575
+ find_selenium_elements(identifier, Elements::TableCell, 'td')
576
+ end
577
+
578
+ #
579
+ # platform method to retrieve an image element
580
+ # See PageObject::Accessors#image
581
+ #
582
+ def image_for(identifier)
583
+ find_selenium_element(identifier, Elements::Image, 'img')
584
+ end
585
+
586
+ #
587
+ # platform method to retrieve all image elements
588
+ #
589
+ def images_for(identifier)
590
+ find_selenium_elements(identifier, Elements::Image, 'img')
591
+ end
592
+
593
+ #
594
+ # platform method to retrieve a form element
595
+ # See PageObject::Accessors#form
596
+ #
597
+ def form_for(identifier)
598
+ find_selenium_element(identifier, Elements::Form, 'form')
599
+ end
600
+
601
+ #
602
+ # platform method to retrieve all forms
603
+ #
604
+ def forms_for(identifier)
605
+ find_selenium_elements(identifier, Elements::Form, 'form')
606
+ end
607
+
608
+ #
609
+ # platform method to retrieve the text from a list item
610
+ # See PageObject::Accessors#list_item
611
+ #
612
+ def list_item_text_for(identifier)
613
+ process_selenium_call(identifier, Elements::ListItem, 'li') do |how, what|
614
+ @browser.find_element(how, what).text
615
+ end
616
+ end
617
+
618
+ #
619
+ # platform method to retrieve a list item element
620
+ # See PageObject::Accessors#list_item
621
+ #
622
+ def list_item_for(identifier)
623
+ find_selenium_element(identifier, Elements::ListItem, 'li')
624
+ end
625
+
626
+ #
627
+ # platform method to retrieve all list items
628
+ #
629
+ def list_items_for(identifier)
630
+ find_selenium_elements(identifier, Elements::ListItem, 'li')
631
+ end
632
+
633
+ #
634
+ # platform method to retrieve the text from an unordered list
635
+ # See PageObject::Accessors#unordered_list
636
+ #
637
+ def unordered_list_text_for(identifier)
638
+ process_selenium_call(identifier, Elements::UnorderedList, 'ul') do |how, what|
639
+ @browser.find_element(how, what).text
640
+ end
641
+ end
642
+
643
+ #
644
+ # platform method to retrieve an unordered list element
645
+ # See PageObject::Accessors#unordered_list
646
+ #
647
+ def unordered_list_for(identifier)
648
+ find_selenium_element(identifier, Elements::UnorderedList, 'ul')
649
+ end
650
+
651
+ #
652
+ # platform method to retrieve all unordered lists
653
+ #
654
+ def unordered_lists_for(identifier)
655
+ find_selenium_elements(identifier, Elements::UnorderedList, 'ul')
656
+ end
657
+
658
+ #
659
+ # platform method to retrieve the text from an ordered list
660
+ # See PageObject::Accessors#ordered_list
661
+ #
662
+ def ordered_list_text_for(identifier)
663
+ process_selenium_call(identifier, Elements::OrderedList, 'ol') do |how, what|
664
+ @browser.find_element(how, what).text
665
+ end
666
+ end
667
+
668
+ #
669
+ # platform method to retrieve an ordered list element
670
+ # See PageObject::Accessors#ordered_list
671
+ #
672
+ def ordered_list_for(identifier)
673
+ find_selenium_element(identifier, Elements::OrderedList, 'ol')
674
+ end
675
+
676
+ #
677
+ # platform method to retrieve all ordered lists
678
+ #
679
+ def ordered_lists_for(identifier)
680
+ find_selenium_elements(identifier, Elements::OrderedList, 'ol')
681
+ end
682
+
683
+ #
684
+ # platform method to retrieve the text from a h1
685
+ # See PageObject::Accessors#h1
686
+ #
687
+ def h1_text_for(identifier)
688
+ process_selenium_call(identifier, Elements::Heading, 'h1') do |how, what|
689
+ @browser.find_element(how, what).text
690
+ end
691
+ end
692
+
693
+ #
694
+ # platform method to retrieve the h1 element
695
+ # See PageObject::Accessors#h1
696
+ #
697
+ def h1_for(identifier)
698
+ find_selenium_element(identifier, Elements::Heading, 'h1')
699
+ end
700
+
701
+ #
702
+ # platform method to retrieve all h1 elements
703
+ #
704
+ def h1s_for(identifier)
705
+ find_selenium_elements(identifier, Elements::Heading, 'h1')
706
+ end
707
+
708
+ #
709
+ # platform method to retrieve the text from a h2
710
+ # See PageObject::Accessors#h2
711
+ #
712
+ def h2_text_for(identifier)
713
+ process_selenium_call(identifier, Elements::Heading, 'h2') do |how, what|
714
+ @browser.find_element(how, what).text
715
+ end
716
+ end
717
+
718
+ #
719
+ # platform method to retrieve the h2 element
720
+ # See PageObject::Accessors#h2
721
+ #
722
+ def h2_for(identifier)
723
+ find_selenium_element(identifier, Elements::Heading, 'h2')
724
+ end
725
+
726
+ #
727
+ # platform method to retrieve all h2 elements
728
+ #
729
+ def h2s_for(identifier)
730
+ find_selenium_elements(identifier, Elements::Heading, 'h2')
731
+ end
732
+
733
+ #
734
+ # platform method to retrieve the text from a h3
735
+ # See PageObject::Accessors#h3
736
+ #
737
+ def h3_text_for(identifier)
738
+ process_selenium_call(identifier, Elements::Heading, 'h3') do |how, what|
739
+ @browser.find_element(how, what).text
740
+ end
741
+ end
742
+
743
+ #
744
+ # platform method to retrieve the h3 element
745
+ # See PageObject::Accessors#h3
746
+ #
747
+ def h3_for(identifier)
748
+ find_selenium_element(identifier, Elements::Heading, 'h3')
749
+ end
750
+
751
+ #
752
+ # platform method to retrieve all h3 elements
753
+ #
754
+ def h3s_for(identifier)
755
+ find_selenium_elements(identifier, Elements::Heading, 'h3')
756
+ end
757
+
758
+ #
759
+ # platform method to retrieve the text from a h4
760
+ # See PageObject::Accessors#h4
761
+ #
762
+ def h4_text_for(identifier)
763
+ process_selenium_call(identifier, Elements::Heading, 'h4') do |how, what|
764
+ @browser.find_element(how, what).text
765
+ end
766
+ end
767
+
768
+ #
769
+ # platform method to retrieve the h4 element
770
+ # See PageObject::Accessors#h4
771
+ #
772
+ def h4_for(identifier)
773
+ find_selenium_element(identifier, Elements::Heading, 'h4')
774
+ end
775
+
776
+ #
777
+ # platform method to retrieve all h4 elements
778
+ #
779
+ def h4s_for(identifier)
780
+ find_selenium_elements(identifier, Elements::Heading, 'h4')
781
+ end
782
+
783
+ #
784
+ # platform method to retrieve the text from a h5
785
+ # See PageObject::Accessors#h5
786
+ #
787
+ def h5_text_for(identifier)
788
+ process_selenium_call(identifier, Elements::Heading, 'h5') do |how, what|
789
+ @browser.find_element(how, what).text
790
+ end
791
+ end
792
+
793
+ #
794
+ # platform method to retrieve the h5 element
795
+ # See PageObject::Accessors#h5
796
+ #
797
+ def h5_for(identifier)
798
+ find_selenium_element(identifier, Elements::Heading, 'h5')
799
+ end
800
+
801
+ #
802
+ # platform method to retrieve all h5 elements
803
+ #
804
+ def h5s_for(identifier)
805
+ find_selenium_elements(identifier, Elements::Heading, 'h5')
806
+ end
807
+
808
+ #
809
+ # platform method to retrieve the text from a h6
810
+ # See PageObject::Accessors#h6
811
+ #
812
+ def h6_text_for(identifier)
813
+ process_selenium_call(identifier, Elements::Heading, 'h6') do |how, what|
814
+ @browser.find_element(how, what).text
815
+ end
816
+ end
817
+
818
+ #
819
+ # platform method to retrieve the h6 element
820
+ # See PageObject::Accessors#h6
821
+ #
822
+ def h6_for(identifier)
823
+ find_selenium_element(identifier, Elements::Heading, 'h6')
824
+ end
825
+
826
+ #
827
+ # platform method to retrieve all h6 elements
828
+ #
829
+ def h6s_for(identifier)
830
+ find_selenium_elements(identifier, Elements::Heading, 'h6')
831
+ end
832
+
833
+ #
834
+ # platform method to retrieve the text for a paragraph
835
+ # See PageObject::Accessors#paragraph
836
+ #
837
+ def paragraph_text_for(identifier)
838
+ process_selenium_call(identifier, Elements::Paragraph, 'p') do |how, what|
839
+ @browser.find_element(how, what).text
840
+ end
841
+ end
842
+
843
+ #
844
+ # platform method to retrieve the paragraph element
845
+ # See PageObject::Accessors#paragraph
846
+ #
847
+ def paragraph_for(identifier)
848
+ find_selenium_element(identifier, Elements::Paragraph, 'p')
849
+ end
850
+
851
+ #
852
+ # platform method to retrieve all paragraph elements
853
+ #
854
+ def paragraphs_for(identifier)
855
+ find_selenium_elements(identifier, Elements::Paragraph, 'p')
856
+ end
857
+
858
+ #
859
+ # platform method to return the text for a label
860
+ # See PageObject::Accessors#label
861
+ #
862
+ def label_text_for(identifier)
863
+ process_selenium_call(identifier, Elements::Label, 'label') do |how, what|
864
+ @browser.find_element(how, what).text
865
+ end
866
+ end
867
+
868
+
869
+ #
870
+ # platform method to return a PageObject::Elements::Label element
871
+ # See PageObject::Accessors#label
872
+ #
873
+ def label_for(identifier)
874
+ find_selenium_element(identifier, Elements::Label, 'label')
875
+ end
876
+
877
+
878
+ #
879
+ # platform method to retrieve all label elements
880
+ #
881
+ def labels_for(identifier)
882
+ find_selenium_elements(identifier, Elements::Label, 'label')
883
+ end
884
+
885
+ #
886
+ # platform method to set the file on a file_field element
887
+ # See PageObject::Accessors#file_field
888
+ #
889
+ def file_field_value_set(identifier, value)
890
+ process_selenium_call(identifier, Elements::FileField, 'input', :type => 'file') do |how, what|
891
+ @browser.find_element(how, what).send_keys(value)
892
+ end
893
+ end
894
+
895
+ #
896
+ # platform method to retrieve a file_field element
897
+ # See PageObject::Accessors#file_field
898
+ #
899
+ def file_field_for(identifier)
900
+ find_selenium_element(identifier, Elements::FileField, 'input', :type => 'file')
901
+ end
902
+
903
+ #
904
+ # platform method to return an array of file field elements
905
+ #
906
+ def file_fields_for(identifier)
907
+ find_selenium_elements(identifier, Elements::FileField, 'input', :type => 'file')
908
+ end
909
+
910
+ #
911
+ # platform method to click on an area
912
+ #
913
+ def click_area_for(identifier)
914
+ process_selenium_call(identifier, Elements::Area, 'area') do |how, what|
915
+ @browser.find_element(how, what).click
916
+ end
917
+ end
918
+
919
+ #
920
+ # platform method to retrieve an area element
921
+ #
922
+ def area_for(identifier)
923
+ find_selenium_element(identifier, Elements::Area, 'area')
924
+ end
925
+
926
+ #
927
+ # platform method to return an array of area elements
928
+ #
929
+ def areas_for(identifier)
930
+ find_selenium_elements(identifier, Elements::Area, 'area')
931
+ end
932
+
933
+ #
934
+ # platform method to retrieve a canvas element
935
+ #
936
+ def canvas_for(identifier)
937
+ find_selenium_element(identifier, Elements::Canvas, 'canvas')
938
+ end
939
+
940
+ #
941
+ # platform method to return an array of canvas elements
942
+ #
943
+ def canvass_for(identifier)
944
+ find_selenium_elements(identifier, Elements::Canvas, 'canvas')
945
+ end
946
+
947
+ #
948
+ # platform method to retrieve an audio element
949
+ #
950
+ def audio_for(identifier)
951
+ find_selenium_element(identifier, Elements::Audio, 'audio')
952
+ end
953
+
954
+ #
955
+ # platform method to return an array of audio elements
956
+ #
957
+ def audios_for(identifier)
958
+ find_selenium_elements(identifier, Elements::Audio, 'audio')
959
+ end
960
+
961
+ #
962
+ # platform method to retrieve a video element
963
+ #
964
+ def video_for(identifier)
965
+ find_selenium_element(identifier, Elements::Video, 'video')
966
+ end
967
+
968
+ #
969
+ # platform method to return an array of video elements
970
+ #
971
+ def videos_for(identifier)
972
+ find_selenium_elements(identifier, Elements::Video, 'video')
973
+ end
974
+
975
+ #
976
+ # platform method to retrieve a generic element
977
+ # See PageObject::Accessors#element
978
+ #
979
+ def element_for(tag, identifier)
980
+ find_selenium_element(identifier, Elements::Element, tag.to_s)
981
+ end
982
+
983
+ #
984
+ # platform method to retrieve a collection of generic elements
985
+ # See PageObject::Accessors#elements
986
+ #
987
+ def elements_for(tag, identifier)
988
+ find_selenium_elements(identifier, Elements::Element, tag.to_s)
989
+ end
990
+
991
+ #
992
+ # platform method to return a svg element
993
+ #
994
+ def svg_for(identifier)
995
+ find_selenium_element(identifier, Elements::Element, 'svg')
996
+ end
997
+
998
+ #
999
+ # platform method to return an array of svg elements
1000
+ #
1001
+ def svgs_for(identifier)
1002
+ find_selenium_elements(identifier, Elements::Element, 'svg')
1003
+ end
1004
+
1005
+ private
1006
+
1007
+ def process_selenium_call(identifier, type, tag, other=nil)
1008
+ how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1009
+ switch_to_frame(frame_identifiers)
1010
+ value = yield how, what
1011
+ @browser.switch_to.default_content unless frame_identifiers.nil?
1012
+ value
1013
+ end
1014
+
1015
+ def find_selenium_element(identifier, type, tag, other=nil)
1016
+ regexp = delete_regexp(identifier)
1017
+ how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1018
+ switch_to_frame(frame_identifiers)
1019
+ begin
1020
+ unless regexp
1021
+ element = @browser.find_element(how, what)
1022
+ else
1023
+ elements = @browser.find_elements(how, what)
1024
+ element = elements.find {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1025
+ end
1026
+ rescue Selenium::WebDriver::Error::NoSuchElementError
1027
+ @browser.switch_to.default_content unless frame_identifiers.nil?
1028
+ return build_null_object(identifier, type, tag, other)
1029
+ end
1030
+ @browser.switch_to.default_content unless frame_identifiers.nil?
1031
+ type.new(element, :platform => :selenium_webdriver)
1032
+ end
1033
+
1034
+ def find_selenium_elements(identifier, type, tag, other=nil)
1035
+ regexp = delete_regexp(identifier)
1036
+ how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)
1037
+ switch_to_frame(frame_identifiers)
1038
+ unless regexp
1039
+ elements = @browser.find_elements(how, what)
1040
+ else
1041
+ eles = @browser.find_elements(how, what)
1042
+ elements = eles.find_all {|ele| matches_selector?(ele, regexp[0], regexp[1])}
1043
+ end
1044
+ @browser.switch_to.default_content unless frame_identifiers.nil?
1045
+ elements.map { |element| type.new(element, :platform => :selenium_webdriver) }
1046
+ end
1047
+
1048
+ def build_null_object(identifier, type, tag, other)
1049
+ null_element = SurrogateSeleniumElement.new
1050
+ null_element.identifier = identifier
1051
+ null_element.type = type
1052
+ null_element.tag = tag
1053
+ null_element.other = other
1054
+ null_element.platform = self
1055
+ Elements::Element.new(null_element, :platform => :selenium_webdriver)
1056
+ end
1057
+
1058
+ def delete_regexp(identifier)
1059
+ regexp = identifier.find {|ident| ident[1].is_a?(Regexp)}
1060
+ identifier.delete(regexp[0]) if regexp
1061
+ regexp
1062
+ end
1063
+
1064
+ def parse_identifiers(identifier, element, tag_name=nil, additional=nil)
1065
+ frame_identifiers = identifier.delete(:frame)
1066
+ identifier = add_tagname_if_needed identifier, tag_name, additional if tag_name
1067
+ how, what = element.selenium_identifier_for identifier
1068
+ return how, what, frame_identifiers
1069
+ end
1070
+
1071
+ def add_tagname_if_needed identifier, tag, additional=nil
1072
+ return identifier if identifier.length < 2 and supported_identifier(identifier, tag, additional)
1073
+ identifier[:tag_name] = tag
1074
+ if additional
1075
+ additional.each do |key, value|
1076
+ identifier[key] = value
1077
+ end
1078
+ end
1079
+ identifier
1080
+ end
1081
+
1082
+ def supported_identifier(identifier, tag, additional)
1083
+ return false if identifier.size == 0
1084
+ return false if identifier[:index]
1085
+ return false if identifier[:action] and tag == 'form'
1086
+ return false if identifier[:alt] and tag == 'img'
1087
+ return false if identifier[:alt] and tag == 'input' and
1088
+ ['submit', 'image', 'button', 'reset'].include? additional[:type]
1089
+ return false if identifier[:href] and tag == 'a'
1090
+ return false if identifier[:src] and tag == 'input' and
1091
+ ['submit', 'image', 'button', 'reset'].include? additional[:type]
1092
+ return false if identifier[:src] and tag == 'img'
1093
+ return false if identifier[:label]
1094
+ return false if identifier[:text] and tag == 'input' and additional[:type] == 'hidden'
1095
+ return false if identifier[:text] and tag == 'input' and additional[:type] == 'text'
1096
+ return false if identifier[:text] and ['div', 'span', 'td', 'label', 'li'].include? tag
1097
+ return false if identifier[:title] and tag == 'input' and additional[:type] == 'text'
1098
+ return false if identifier[:title] and tag == 'input' and additional[:type] == 'file'
1099
+ return false if identifier[:title] and tag == 'a'
1100
+ return false if identifier[:title] and tag == 'span'
1101
+ return false if identifier[:title] and tag == 'div'
1102
+ return false if identifier[:value] and tag == 'input' and
1103
+ ['radio', 'submit', 'image', 'button', 'reset', 'checkbox', 'hidden'].include? additional[:type]
1104
+ true
1105
+ end
1106
+
1107
+ def matches_selector?(element, how, what)
1108
+ what === fetch_value(element, how)
1109
+ end
1110
+
1111
+ def fetch_value(element, how)
1112
+ case how
1113
+ when :text
1114
+ element.text
1115
+ when :tag_name
1116
+ element.tag_name.downcase
1117
+ when :href
1118
+ (href = element.attribute(:href)) && href.strip
1119
+ else
1120
+ element.attribute(how.to_s.gsub("_", "-").to_sym)
1121
+ end
1122
+ end
1123
+
1124
+ def switch_to_frame(frame_identifiers)
1125
+ unless frame_identifiers.nil?
1126
+ frame_identifiers.each do |frame|
1127
+ frame_id = frame.values.first
1128
+ value = frame_id.values.first
1129
+ @browser.switch_to.frame(value)
1130
+ end
1131
+ end
1132
+ end
1133
+ end
1134
+ end
1135
+ end
1136
+ end