watir 7.0.0 → 7.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (258) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/chrome.yml +29 -16
  3. data/.github/workflows/edge.yml +19 -11
  4. data/.github/workflows/firefox.yml +28 -16
  5. data/.github/workflows/ie.yml +13 -7
  6. data/.github/workflows/safari.yml +22 -11
  7. data/.github/workflows/unit.yml +31 -23
  8. data/.rubocop.yml +33 -13
  9. data/.rubocop_todo.yml +7 -26
  10. data/CHANGES.md +33 -0
  11. data/Gemfile +6 -1
  12. data/LICENSE +2 -2
  13. data/README.md +15 -11
  14. data/Rakefile +9 -7
  15. data/lib/watir/adjacent.rb +3 -1
  16. data/lib/watir/after_hooks.rb +5 -3
  17. data/lib/watir/alert.rb +2 -0
  18. data/lib/watir/aliases.rb +2 -0
  19. data/lib/watir/attribute_helper.rb +4 -2
  20. data/lib/watir/browser.rb +10 -14
  21. data/lib/watir/capabilities.rb +53 -33
  22. data/lib/watir/cell_container.rb +2 -0
  23. data/lib/watir/container.rb +2 -0
  24. data/lib/watir/cookies.rb +5 -5
  25. data/lib/watir/element_collection.rb +3 -1
  26. data/lib/watir/elements/button.rb +2 -0
  27. data/lib/watir/elements/cell.rb +2 -0
  28. data/lib/watir/elements/checkbox.rb +2 -0
  29. data/lib/watir/elements/date_field.rb +2 -0
  30. data/lib/watir/elements/date_time_field.rb +2 -0
  31. data/lib/watir/elements/dlist.rb +3 -1
  32. data/lib/watir/elements/element.rb +68 -118
  33. data/lib/watir/elements/file_field.rb +2 -0
  34. data/lib/watir/elements/font.rb +2 -0
  35. data/lib/watir/elements/form.rb +2 -0
  36. data/lib/watir/elements/hidden.rb +2 -0
  37. data/lib/watir/elements/iframe.rb +2 -0
  38. data/lib/watir/elements/image.rb +2 -0
  39. data/lib/watir/elements/input.rb +2 -0
  40. data/lib/watir/elements/link.rb +2 -0
  41. data/lib/watir/elements/list.rb +2 -0
  42. data/lib/watir/elements/option.rb +2 -0
  43. data/lib/watir/elements/radio.rb +2 -0
  44. data/lib/watir/elements/row.rb +2 -0
  45. data/lib/watir/elements/select.rb +2 -0
  46. data/lib/watir/elements/table.rb +3 -1
  47. data/lib/watir/elements/table_cell.rb +2 -0
  48. data/lib/watir/elements/table_row.rb +2 -0
  49. data/lib/watir/elements/table_section.rb +2 -0
  50. data/lib/watir/elements/text_area.rb +2 -0
  51. data/lib/watir/elements/text_field.rb +2 -0
  52. data/lib/watir/exception.rb +2 -0
  53. data/lib/watir/extensions/nokogiri.rb +2 -0
  54. data/lib/watir/generator/base/generator.rb +2 -0
  55. data/lib/watir/generator/base/idl_sorter.rb +3 -1
  56. data/lib/watir/generator/base/spec_extractor.rb +6 -4
  57. data/lib/watir/generator/base/util.rb +2 -0
  58. data/lib/watir/generator/base/visitor.rb +5 -5
  59. data/lib/watir/generator/base.rb +2 -1
  60. data/lib/watir/generator/html/generator.rb +2 -0
  61. data/lib/watir/generator/html/spec_extractor.rb +2 -0
  62. data/lib/watir/generator/html/visitor.rb +2 -0
  63. data/lib/watir/generator/html.rb +2 -0
  64. data/lib/watir/generator/svg/generator.rb +2 -0
  65. data/lib/watir/generator/svg/spec_extractor.rb +2 -0
  66. data/lib/watir/generator/svg/visitor.rb +2 -0
  67. data/lib/watir/generator/svg.rb +2 -0
  68. data/lib/watir/generator.rb +2 -0
  69. data/lib/watir/has_window.rb +3 -1
  70. data/lib/watir/http_client.rb +2 -0
  71. data/lib/watir/js_execution.rb +2 -0
  72. data/lib/watir/js_snippets/isElementInViewport.js +20 -0
  73. data/lib/watir/js_snippets.rb +2 -0
  74. data/lib/watir/locators/anchor/selector_builder.rb +5 -0
  75. data/lib/watir/locators/button/matcher.rb +2 -0
  76. data/lib/watir/locators/button/selector_builder/xpath.rb +15 -19
  77. data/lib/watir/locators/button/selector_builder.rb +2 -0
  78. data/lib/watir/locators/cell/selector_builder/xpath.rb +2 -0
  79. data/lib/watir/locators/cell/selector_builder.rb +2 -0
  80. data/lib/watir/locators/element/locator.rb +2 -0
  81. data/lib/watir/locators/element/matcher.rb +4 -1
  82. data/lib/watir/locators/element/selector_builder/regexp_disassembler.rb +4 -1
  83. data/lib/watir/locators/element/selector_builder/xpath.rb +16 -15
  84. data/lib/watir/locators/element/selector_builder/xpath_support.rb +4 -2
  85. data/lib/watir/locators/element/selector_builder.rb +16 -6
  86. data/lib/watir/locators/option/matcher.rb +2 -0
  87. data/lib/watir/locators/option/selector_builder/xpath.rb +2 -0
  88. data/lib/watir/locators/option/selector_builder.rb +2 -0
  89. data/lib/watir/locators/row/selector_builder/xpath.rb +2 -0
  90. data/lib/watir/locators/row/selector_builder.rb +2 -0
  91. data/lib/watir/locators/text_area/selector_builder/xpath.rb +2 -0
  92. data/lib/watir/locators/text_area/selector_builder.rb +2 -0
  93. data/lib/watir/locators/text_field/matcher.rb +2 -4
  94. data/lib/watir/locators/text_field/selector_builder/xpath.rb +2 -0
  95. data/lib/watir/locators/text_field/selector_builder.rb +2 -0
  96. data/lib/watir/locators.rb +2 -0
  97. data/lib/watir/logger.rb +2 -0
  98. data/lib/watir/navigation.rb +3 -1
  99. data/lib/watir/radio_set.rb +2 -0
  100. data/lib/watir/row_container.rb +2 -0
  101. data/lib/watir/screenshot.rb +2 -0
  102. data/lib/watir/scroll.rb +29 -3
  103. data/lib/watir/search_context.rb +96 -0
  104. data/lib/watir/shadow_root.rb +60 -0
  105. data/lib/watir/user_editable.rb +3 -1
  106. data/lib/watir/version.rb +3 -1
  107. data/lib/watir/wait/timer.rb +3 -1
  108. data/lib/watir/wait.rb +5 -5
  109. data/lib/watir/window.rb +7 -3
  110. data/lib/watir/window_collection.rb +4 -1
  111. data/lib/watir.rb +4 -0
  112. data/lib/watirspec/guards.rb +2 -0
  113. data/lib/watirspec/implementation.rb +14 -10
  114. data/lib/watirspec/rake_tasks.rb +4 -4
  115. data/lib/watirspec/remote_server.rb +5 -3
  116. data/lib/watirspec/runner.rb +3 -1
  117. data/lib/watirspec/server/app.rb +2 -0
  118. data/lib/watirspec/server.rb +2 -0
  119. data/lib/watirspec.rb +7 -4
  120. data/spec/locator_spec_helper.rb +2 -8
  121. data/spec/spec_helper.rb +2 -0
  122. data/spec/unit/capabilities_spec.rb +561 -550
  123. data/spec/unit/element_locator_spec.rb +71 -58
  124. data/spec/unit/match_elements/button_spec.rb +58 -49
  125. data/spec/unit/match_elements/element_spec.rb +305 -309
  126. data/spec/unit/match_elements/text_field_spec.rb +78 -73
  127. data/spec/unit/selector_builder/anchor_spec.rb +41 -33
  128. data/spec/unit/selector_builder/button_spec.rb +211 -201
  129. data/spec/unit/selector_builder/cell_spec.rb +50 -42
  130. data/spec/unit/selector_builder/element_spec.rb +636 -628
  131. data/spec/unit/selector_builder/row_spec.rb +121 -104
  132. data/spec/unit/selector_builder/text_field_spec.rb +197 -182
  133. data/spec/unit/selector_builder/textarea_spec.rb +24 -14
  134. data/spec/unit/unit_helper.rb +2 -0
  135. data/spec/unit/wait_spec.rb +77 -71
  136. data/spec/watirspec/adjacent_spec.rb +251 -247
  137. data/spec/watirspec/after_hooks_spec.rb +160 -162
  138. data/spec/watirspec/alert_spec.rb +63 -58
  139. data/spec/watirspec/browser_spec.rb +411 -407
  140. data/spec/watirspec/capabilities_spec.rb +566 -0
  141. data/spec/watirspec/cookies_spec.rb +122 -118
  142. data/spec/watirspec/drag_and_drop_spec.rb +30 -14
  143. data/spec/watirspec/element_hidden_spec.rb +68 -60
  144. data/spec/watirspec/elements/area_spec.rb +51 -47
  145. data/spec/watirspec/elements/areas_spec.rb +29 -25
  146. data/spec/watirspec/elements/button_spec.rb +255 -251
  147. data/spec/watirspec/elements/buttons_spec.rb +38 -34
  148. data/spec/watirspec/elements/checkbox_spec.rb +50 -36
  149. data/spec/watirspec/elements/checkboxes_spec.rb +31 -27
  150. data/spec/watirspec/elements/collections_spec.rb +128 -119
  151. data/spec/watirspec/elements/date_field_spec.rb +187 -183
  152. data/spec/watirspec/elements/date_fields_spec.rb +31 -27
  153. data/spec/watirspec/elements/date_time_field_spec.rb +199 -195
  154. data/spec/watirspec/elements/date_time_fields_spec.rb +32 -28
  155. data/spec/watirspec/elements/dd_spec.rb +89 -85
  156. data/spec/watirspec/elements/dds_spec.rb +29 -25
  157. data/spec/watirspec/elements/del_spec.rb +108 -104
  158. data/spec/watirspec/elements/dels_spec.rb +28 -24
  159. data/spec/watirspec/elements/div_spec.rb +227 -211
  160. data/spec/watirspec/elements/divs_spec.rb +39 -35
  161. data/spec/watirspec/elements/dl_spec.rb +112 -110
  162. data/spec/watirspec/elements/dls_spec.rb +30 -26
  163. data/spec/watirspec/elements/dt_spec.rb +88 -84
  164. data/spec/watirspec/elements/dts_spec.rb +29 -25
  165. data/spec/watirspec/elements/element_spec.rb +815 -715
  166. data/spec/watirspec/elements/elements_spec.rb +42 -38
  167. data/spec/watirspec/elements/em_spec.rb +64 -60
  168. data/spec/watirspec/elements/ems_spec.rb +30 -26
  169. data/spec/watirspec/elements/filefield_spec.rb +100 -96
  170. data/spec/watirspec/elements/filefields_spec.rb +30 -26
  171. data/spec/watirspec/elements/font_spec.rb +22 -18
  172. data/spec/watirspec/elements/form_spec.rb +53 -49
  173. data/spec/watirspec/elements/forms_spec.rb +31 -27
  174. data/spec/watirspec/elements/frame_spec.rb +91 -87
  175. data/spec/watirspec/elements/frames_spec.rb +29 -25
  176. data/spec/watirspec/elements/hidden_spec.rb +81 -77
  177. data/spec/watirspec/elements/hiddens_spec.rb +30 -26
  178. data/spec/watirspec/elements/hn_spec.rb +61 -57
  179. data/spec/watirspec/elements/hns_spec.rb +28 -24
  180. data/spec/watirspec/elements/iframe_spec.rb +197 -191
  181. data/spec/watirspec/elements/iframes_spec.rb +33 -29
  182. data/spec/watirspec/elements/image_spec.rb +146 -142
  183. data/spec/watirspec/elements/images_spec.rb +28 -24
  184. data/spec/watirspec/elements/input_spec.rb +11 -7
  185. data/spec/watirspec/elements/ins_spec.rb +108 -104
  186. data/spec/watirspec/elements/inses_spec.rb +28 -24
  187. data/spec/watirspec/elements/label_spec.rb +58 -54
  188. data/spec/watirspec/elements/labels_spec.rb +28 -24
  189. data/spec/watirspec/elements/li_spec.rb +76 -72
  190. data/spec/watirspec/elements/link_spec.rb +175 -179
  191. data/spec/watirspec/elements/links_spec.rb +47 -43
  192. data/spec/watirspec/elements/lis_spec.rb +29 -25
  193. data/spec/watirspec/elements/list_spec.rb +46 -42
  194. data/spec/watirspec/elements/map_spec.rb +59 -55
  195. data/spec/watirspec/elements/maps_spec.rb +29 -25
  196. data/spec/watirspec/elements/meta_spec.rb +18 -14
  197. data/spec/watirspec/elements/metas_spec.rb +28 -24
  198. data/spec/watirspec/elements/ol_spec.rb +58 -65
  199. data/spec/watirspec/elements/ols_spec.rb +28 -24
  200. data/spec/watirspec/elements/option_spec.rb +93 -89
  201. data/spec/watirspec/elements/p_spec.rb +79 -75
  202. data/spec/watirspec/elements/pre_spec.rb +77 -73
  203. data/spec/watirspec/elements/pres_spec.rb +28 -24
  204. data/spec/watirspec/elements/ps_spec.rb +28 -24
  205. data/spec/watirspec/elements/radio_spec.rb +253 -249
  206. data/spec/watirspec/elements/radios_spec.rb +30 -26
  207. data/spec/watirspec/elements/select_list_spec.rb +539 -522
  208. data/spec/watirspec/elements/select_lists_spec.rb +34 -30
  209. data/spec/watirspec/elements/span_spec.rb +114 -110
  210. data/spec/watirspec/elements/spans_spec.rb +28 -24
  211. data/spec/watirspec/elements/strong_spec.rb +60 -58
  212. data/spec/watirspec/elements/strongs_spec.rb +30 -26
  213. data/spec/watirspec/elements/table_spec.rb +194 -139
  214. data/spec/watirspec/elements/tables_spec.rb +30 -26
  215. data/spec/watirspec/elements/tbody_spec.rb +71 -67
  216. data/spec/watirspec/elements/tbodys_spec.rb +42 -38
  217. data/spec/watirspec/elements/td_spec.rb +68 -64
  218. data/spec/watirspec/elements/tds_spec.rb +43 -39
  219. data/spec/watirspec/elements/text_field_spec.rb +208 -204
  220. data/spec/watirspec/elements/text_fields_spec.rb +32 -28
  221. data/spec/watirspec/elements/textarea_spec.rb +22 -18
  222. data/spec/watirspec/elements/textareas_spec.rb +20 -16
  223. data/spec/watirspec/elements/tfoot_spec.rb +67 -63
  224. data/spec/watirspec/elements/tfoots_spec.rb +44 -40
  225. data/spec/watirspec/elements/thead_spec.rb +67 -63
  226. data/spec/watirspec/elements/theads_spec.rb +44 -40
  227. data/spec/watirspec/elements/tr_spec.rb +50 -46
  228. data/spec/watirspec/elements/trs_spec.rb +44 -40
  229. data/spec/watirspec/elements/ul_spec.rb +58 -54
  230. data/spec/watirspec/elements/uls_spec.rb +28 -24
  231. data/spec/watirspec/html/child_frame.html +29 -0
  232. data/spec/watirspec/html/class_locator.html +2 -0
  233. data/spec/watirspec/html/scroll.html +8 -25
  234. data/spec/watirspec/html/scroll_nested.html +17 -0
  235. data/spec/watirspec/html/scroll_nested_offscreen.html +18 -0
  236. data/spec/watirspec/html/shadow_dom.html +28 -0
  237. data/spec/watirspec/html/sticky_elements.html +10 -0
  238. data/spec/watirspec/radio_set_spec.rb +271 -267
  239. data/spec/watirspec/screenshot_spec.rb +22 -18
  240. data/spec/watirspec/scroll_spec.rb +179 -77
  241. data/spec/watirspec/shadow_root_spec.rb +106 -0
  242. data/spec/watirspec/support/rspec_matchers.rb +35 -32
  243. data/spec/watirspec/user_editable_spec.rb +202 -198
  244. data/spec/watirspec/wait_spec.rb +291 -295
  245. data/spec/watirspec/window_switching_spec.rb +426 -428
  246. data/spec/watirspec_helper.rb +25 -42
  247. data/support/doctest_helper.rb +2 -0
  248. data/support/version_differ.rb +2 -0
  249. data/watir.gemspec +14 -10
  250. metadata +91 -240
  251. data/.github/actions/enable-safari/action.yml +0 -11
  252. data/.github/actions/install-chrome/action.yml +0 -12
  253. data/.github/actions/setup-linux/action.yml +0 -8
  254. data/lib/watir-webdriver.rb +0 -2
  255. data/spec/watirspec/attributes_spec.rb +0 -16
  256. data/spec/watirspec/elements/table_nesting_spec.rb +0 -49
  257. data/spec/watirspec/html/hover.html +0 -12
  258. data/spec/watirspec/special_chars_spec.rb +0 -21
@@ -1,969 +1,1069 @@
1
- require 'watirspec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'Element' do
4
- before :each do
5
- @c = Selenium::WebDriver::Platform.mac? ? :command : :control
6
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
7
- end
3
+ require 'watirspec_helper'
8
4
 
9
- describe '.new' do
10
- it "finds elements matching the conditions when given a hash of :how => 'what' arguments" do
11
- expect(browser.checkbox(name: 'new_user_interests', title: 'Dancing is fun!').value).to eq 'dancing'
12
- expect(browser.text_field(class_name: 'name', index: 1).id).to eq 'new_user_last_name'
5
+ module Watir
6
+ describe Element do
7
+ before do
8
+ @c = Selenium::WebDriver::Platform.mac? ? :command : :control
9
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
13
10
  end
14
11
 
15
- it "raises UnknownObjectException when given a hash of :how => 'what' arguments (non-existing object)" do
16
- expect { browser.text_field(index: 100, name: 'foo').id }.to raise_unknown_object_exception
17
- end
12
+ describe '.new' do
13
+ it "finds elements matching the conditions when given a hash of :how => 'what' arguments" do
14
+ expect(browser.checkbox(name: 'new_user_interests', title: 'Dancing is fun!').value).to eq 'dancing'
15
+ expect(browser.text_field(class_name: 'name', index: 1).id).to eq 'new_user_last_name'
16
+ end
17
+
18
+ it "raises UnknownObjectException when given a hash of :how => 'what' arguments (non-existing object)" do
19
+ expect { browser.text_field(index: 100, name: 'foo').id }.to raise_unknown_object_exception
20
+ end
18
21
 
19
- it 'raises ArgumentError if given the wrong number of arguments' do
20
- container = double('container').as_null_object
21
- expect { Watir::Element.new(container, 1, 2, 3, 4) }.to raise_error(ArgumentError)
22
- expect { Watir::Element.new(container, 'foo') }.to raise_error(ArgumentError)
22
+ it 'raises ArgumentError if given the wrong number of arguments' do
23
+ expect { described_class.new(browser.wd, 'foo') }.to raise_error(ArgumentError)
24
+ end
23
25
  end
24
- end
25
26
 
26
- describe '#element_call' do
27
- it 'handles exceptions when taking an action on a stale element' do
28
- browser.goto WatirSpec.url_for('removed_element.html')
27
+ describe '#element_call' do
28
+ it 'handles exceptions when taking an action on a stale element' do
29
+ browser.goto WatirSpec.url_for('removed_element.html')
29
30
 
30
- element = browser.div(id: 'text').locate
31
+ element = browser.div(id: 'text').locate
31
32
 
32
- browser.refresh
33
+ browser.refresh
33
34
 
34
- expect(element).to be_stale
35
- expect { element.text }.to_not raise_error
36
- end
35
+ expect(element).to be_stale
36
+ expect { element.text }.not_to raise_error
37
+ end
37
38
 
38
- it 'relocates stale element when taking an action on it' do
39
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
40
- element = browser.text_field(id: 'new_user_first_name').locate
41
- browser.refresh
42
- expect { element.click }.not_to raise_exception
39
+ it 'relocates stale element when taking an action on it' do
40
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
41
+ element = browser.text_field(id: 'new_user_first_name').locate
42
+ browser.refresh
43
+ expect { element.click }.not_to raise_exception
44
+ end
43
45
  end
44
- end
45
46
 
46
- describe '#eq and #eql?' do
47
- before { browser.goto WatirSpec.url_for('definition_lists.html') }
47
+ describe '#eq and #eql?' do
48
+ before { browser.goto WatirSpec.url_for('definition_lists.html') }
48
49
 
49
- it 'returns true if the two elements point to the same DOM element' do
50
- a = browser.dl(id: 'experience-list')
51
- b = browser.dl
50
+ it 'returns true if the two elements point to the same DOM element' do
51
+ a = browser.dl(id: 'experience-list')
52
+ b = browser.dl
52
53
 
53
- expect(a).to eq b
54
- expect(a).to eql(b)
55
- end
54
+ expect(a).to eq b
55
+ expect(a).to eql(b)
56
+ end
56
57
 
57
- it 'returns false if the two elements are not the same' do
58
- a = browser.dls[0]
59
- b = browser.dls[1]
58
+ it 'returns false if the two elements are not the same' do
59
+ a = browser.dls[0]
60
+ b = browser.dls[1]
60
61
 
61
- expect(a).to_not eq b
62
- expect(a).to_not eql(b)
63
- end
62
+ expect(a).not_to eq b
63
+ expect(a).not_to eql(b)
64
+ end
64
65
 
65
- it 'returns false if the other object is not an Element' do
66
- expect(browser.dl).to_not eq 1
66
+ it 'returns false if the other object is not an Element' do
67
+ expect(browser.dl).not_to eq 1
68
+ end
67
69
  end
68
- end
69
70
 
70
- describe 'data-* attributes' do
71
- before { browser.goto WatirSpec.url_for('data_attributes.html') }
71
+ describe 'data-* attributes' do
72
+ before { browser.goto WatirSpec.url_for('data_attributes.html') }
72
73
 
73
- it 'finds elements by a data-* attribute' do
74
- expect(browser.p(data_type: 'ruby-library')).to exist
75
- end
74
+ it 'finds elements by a data-* attribute' do
75
+ expect(browser.p(data_type: 'ruby-library')).to exist
76
+ end
76
77
 
77
- it 'returns the value of a data-* attribute' do
78
- expect(browser.p.data_type).to eq 'ruby-library'
78
+ it 'returns the value of a data-* attribute' do
79
+ expect(browser.p.data_type).to eq 'ruby-library'
80
+ end
79
81
  end
80
- end
81
82
 
82
- describe 'aria-* attributes' do
83
- before { browser.goto WatirSpec.url_for('aria_attributes.html') }
83
+ describe 'aria-* attributes' do
84
+ before { browser.goto WatirSpec.url_for('aria_attributes.html') }
84
85
 
85
- it 'finds elements by a aria-* attribute' do
86
- expect(browser.p(aria_label: 'ruby-library')).to exist
87
- end
86
+ it 'finds elements by a aria-* attribute' do
87
+ expect(browser.p(aria_label: 'ruby-library')).to exist
88
+ end
88
89
 
89
- it 'returns the value of a aria-* attribute' do
90
- expect(browser.p.aria_label).to eq 'ruby-library'
90
+ it 'returns the value of a aria-* attribute' do
91
+ expect(browser.p.aria_label).to eq 'ruby-library'
92
+ end
91
93
  end
92
- end
93
94
 
94
- describe 'visible text' do
95
- it 'finds elements by visible text' do
96
- browser.goto WatirSpec.url_for('non_control_elements.html')
95
+ describe 'visible text' do
96
+ it 'finds elements by visible text' do
97
+ browser.goto WatirSpec.url_for('non_control_elements.html')
97
98
 
98
- expect(browser.element(visible_text: 'all visible')).to exist
99
- expect(browser.element(visible_text: /all visible/)).to exist
100
- expect(browser.element(visible_text: /some visible/)).to exist
101
- expect(browser.element(visible_text: 'Link 2', class: 'external')).to exist
102
- expect(browser.element(visible_text: /Link 2/, class: 'external')).to exist
103
- end
99
+ expect(browser.element(visible_text: 'all visible')).to exist
100
+ expect(browser.element(visible_text: /all visible/)).to exist
101
+ expect(browser.element(visible_text: /some visible/)).to exist
102
+ expect(browser.element(visible_text: 'Link 2', class: 'external')).to exist
103
+ expect(browser.element(visible_text: /Link 2/, class: 'external')).to exist
104
+ end
104
105
 
105
- it 'finds elements by visible text in spite of hidden text',
106
- except: {browser: :safari, reason: 'Safari is not filtering out hidden text'} do
107
- browser.goto WatirSpec.url_for('non_control_elements.html')
106
+ it 'finds elements by visible text in spite of hidden text',
107
+ except: {browser: :safari, reason: 'Safari is not filtering out hidden text'} do
108
+ browser.goto WatirSpec.url_for('non_control_elements.html')
108
109
 
109
- expect(browser.element(visible_text: 'some visible')).to exist
110
- expect(browser.element(visible_text: 'none visible')).not_to exist
111
- expect(browser.element(visible_text: /none visible/)).not_to exist
112
- end
110
+ expect(browser.element(visible_text: 'some visible')).to exist
111
+ expect(browser.element(visible_text: 'none visible')).not_to exist
112
+ expect(browser.element(visible_text: /none visible/)).not_to exist
113
+ end
113
114
 
114
- it 'raises exception unless value is a String or a RegExp' do
115
- browser.goto WatirSpec.url_for('non_control_elements.html')
116
- msg = /expected one of \[String, Regexp\], got 7:Integer/
117
- expect { browser.element(visible_text: 7).exists? }.to raise_exception(TypeError, msg)
118
- end
115
+ it 'raises exception unless value is a String or a RegExp' do
116
+ browser.goto WatirSpec.url_for('non_control_elements.html')
117
+ msg = /expected one of \[String, Regexp\], got 7:Integer/
118
+ expect { browser.element(visible_text: 7).exists? }.to raise_exception(TypeError, msg)
119
+ end
119
120
 
120
- it 'raises exception unless key is valid' do
121
- browser.goto WatirSpec.url_for('non_control_elements.html')
122
- msg = /Unable to build XPath using 7:Integer/
123
- expect { browser.element(7 => /foo/).exists? }.to raise_exception(Watir::Exception::Error, msg)
121
+ it 'raises exception unless key is valid' do
122
+ browser.goto WatirSpec.url_for('non_control_elements.html')
123
+ msg = /Unable to build XPath using 7:Integer/
124
+ expect { browser.element(7 => /foo/).exists? }.to raise_exception(Watir::Exception::Error, msg)
125
+ end
124
126
  end
125
- end
126
127
 
127
- describe 'finding with unknown tag name' do
128
- it 'finds an element without arguments' do
129
- expect(browser.element).to exist
130
- end
128
+ describe 'finding with unknown tag name' do
129
+ it 'finds an element without arguments' do
130
+ expect(browser.element).to exist
131
+ end
131
132
 
132
- it 'finds an element by xpath' do
133
- expect(browser.element(xpath: "//*[@for='new_user_first_name']")).to exist
134
- end
133
+ it 'finds an element by xpath' do
134
+ expect(browser.element(xpath: "//*[@for='new_user_first_name']")).to exist
135
+ end
135
136
 
136
- it 'finds an element by arbitrary attribute' do
137
- expect(browser.element(title: 'no title')).to exist
138
- end
137
+ it 'finds an element by arbitrary attribute' do
138
+ expect(browser.element(title: 'no title')).to exist
139
+ end
139
140
 
140
- it 'finds several elements by xpath' do
141
- expect(browser.elements(xpath: '//a').length).to eq 1
142
- end
141
+ it 'finds several elements by xpath' do
142
+ expect(browser.elements(xpath: '//a').length).to eq 1
143
+ end
143
144
 
144
- it 'finds several elements by arbitrary attribute' do
145
- expect(browser.elements(id: /^new_user/).length).to eq 33
146
- end
145
+ it 'finds several elements by arbitrary attribute' do
146
+ expect(browser.elements(id: /^new_user/).length).to eq 33
147
+ end
147
148
 
148
- it "finds an element from an element's subtree" do
149
- expect(browser.fieldset.element(id: 'first_label')).to exist
150
- expect(browser.field_set.element(id: 'first_label')).to exist
151
- end
149
+ it "finds an element from an element's subtree" do
150
+ expect(browser.fieldset.element(id: 'first_label')).to exist
151
+ expect(browser.field_set.element(id: 'first_label')).to exist
152
+ end
152
153
 
153
- it "finds several elements from an element's subtree" do
154
- expect(browser.fieldset.elements(xpath: './/label').length).to eq 23
154
+ it "finds several elements from an element's subtree" do
155
+ expect(browser.fieldset.elements(xpath: './/label').length).to eq 23
156
+ end
155
157
  end
156
- end
157
158
 
158
- describe '#to_subtype' do
159
- it 'returns a CheckBox instance' do
160
- e = browser.input(xpath: "//input[@type='checkbox']").to_subtype
161
- expect(e).to be_kind_of(Watir::CheckBox)
162
- end
159
+ describe '#to_subtype' do
160
+ it 'returns a CheckBox instance' do
161
+ e = browser.input(xpath: "//input[@type='checkbox']").to_subtype
162
+ expect(e).to be_a(Watir::CheckBox)
163
+ end
163
164
 
164
- it 'returns a Radio instance' do
165
- e = browser.input(xpath: "//input[@type='radio']").to_subtype
166
- expect(e).to be_kind_of(Watir::Radio)
167
- end
165
+ it 'returns a Radio instance' do
166
+ e = browser.input(xpath: "//input[@type='radio']").to_subtype
167
+ expect(e).to be_a(Watir::Radio)
168
+ end
168
169
 
169
- it 'returns a Button instance' do
170
- es = [
171
- browser.input(xpath: "//input[@type='button']").to_subtype,
172
- browser.input(xpath: "//input[@type='submit']").to_subtype,
173
- browser.input(xpath: "//input[@type='reset']").to_subtype,
174
- browser.input(xpath: "//input[@type='image']").to_subtype
175
- ]
170
+ it 'returns a Button instance' do
171
+ es = [
172
+ browser.input(xpath: "//input[@type='button']").to_subtype,
173
+ browser.input(xpath: "//input[@type='submit']").to_subtype,
174
+ browser.input(xpath: "//input[@type='reset']").to_subtype,
175
+ browser.input(xpath: "//input[@type='image']").to_subtype
176
+ ]
176
177
 
177
- es.all? { |e| expect(e).to be_kind_of(Watir::Button) }
178
- end
178
+ es.all? { |e| expect(e).to be_a(Watir::Button) }
179
+ end
179
180
 
180
- it 'returns a TextField instance' do
181
- e = browser.input(xpath: "//input[@type='text']").to_subtype
182
- expect(e).to be_kind_of(Watir::TextField)
183
- end
181
+ it 'returns a TextField instance' do
182
+ e = browser.input(xpath: "//input[@type='text']").to_subtype
183
+ expect(e).to be_a(Watir::TextField)
184
+ end
184
185
 
185
- it 'returns a FileField instance' do
186
- e = browser.input(xpath: "//input[@type='file']").to_subtype
187
- expect(e).to be_kind_of(Watir::FileField)
188
- end
186
+ it 'returns a FileField instance' do
187
+ e = browser.input(xpath: "//input[@type='file']").to_subtype
188
+ expect(e).to be_a(Watir::FileField)
189
+ end
189
190
 
190
- it 'returns a Div instance' do
191
- el = browser.element(xpath: "//*[@id='messages']").to_subtype
192
- expect(el).to be_kind_of(Watir::Div)
191
+ it 'returns a Div instance' do
192
+ el = browser.element(xpath: "//*[@id='messages']").to_subtype
193
+ expect(el).to be_a(Watir::Div)
194
+ end
193
195
  end
194
- end
195
196
 
196
- describe '#focus' do
197
- it 'fires the onfocus event for the given element' do
198
- tf = browser.text_field(id: 'new_user_occupation')
199
- expect(tf.value).to eq 'Developer'
200
- tf.focus
201
- expect(browser.div(id: 'onfocus_test').text).to eq 'changed by onfocus event'
197
+ describe '#focus' do
198
+ it 'fires the onfocus event for the given element' do
199
+ tf = browser.text_field(id: 'new_user_occupation')
200
+ expect(tf.value).to eq 'Developer'
201
+ tf.focus
202
+ expect(browser.div(id: 'onfocus_test').text).to eq 'changed by onfocus event'
203
+ end
202
204
  end
203
- end
204
205
 
205
- describe '#focused?',
206
- except: {browser: :firefox,
207
- remote: true,
208
- reason: 'https://github.com/SeleniumHQ/selenium/issues/2555'} do
209
- it 'knows if the element is focused' do
210
- expect(browser.element(id: 'new_user_first_name')).to be_focused
211
- expect(browser.element(id: 'new_user_last_name')).to_not be_focused
206
+ describe '#focused?',
207
+ except: {browser: :firefox,
208
+ driver: :remote_driver,
209
+ reason: 'https://github.com/SeleniumHQ/selenium/issues/2555'} do
210
+ it 'knows if the element is focused' do
211
+ expect(browser.element(id: 'new_user_first_name')).to be_focused
212
+ expect(browser.element(id: 'new_user_last_name')).not_to be_focused
213
+ end
212
214
  end
213
- end
214
215
 
215
- describe '#fire_event' do
216
- it 'should fire the given event' do
217
- expect(browser.div(id: 'onfocus_test').text).to be_empty
218
- browser.text_field(id: 'new_user_occupation').fire_event('onfocus')
219
- expect(browser.div(id: 'onfocus_test').text).to eq 'changed by onfocus event'
216
+ describe '#fire_event' do
217
+ it 'fires the given event' do
218
+ expect(browser.div(id: 'onfocus_test').text).to be_empty
219
+ browser.text_field(id: 'new_user_occupation').fire_event('onfocus')
220
+ expect(browser.div(id: 'onfocus_test').text).to eq 'changed by onfocus event'
221
+ end
220
222
  end
221
- end
222
223
 
223
- describe '#cache=' do
224
- it 'bypasses selector location' do
225
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
224
+ describe '#cache=' do
225
+ it 'bypasses selector location' do
226
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
226
227
 
227
- wd = browser.div.wd
228
- element = Watir::Element.new(browser, id: 'not_valid')
229
- element.cache = wd
230
-
231
- expect(element).to exist
232
- end
228
+ wd = browser.div.wd
229
+ element = described_class.new(browser, id: 'not_valid')
230
+ element.cache = wd
233
231
 
234
- it 'can be cleared' do
235
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
232
+ expect(element).to exist
233
+ end
236
234
 
237
- wd = browser.div.wd
238
- element = Watir::Element.new(browser, id: 'not_valid')
239
- element.cache = wd
235
+ it 'can be cleared' do
236
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
240
237
 
241
- browser.refresh
242
- expect(element).to_not exist
243
- end
244
- end
238
+ wd = browser.div.wd
239
+ element = described_class.new(browser, id: 'not_valid')
240
+ element.cache = wd
245
241
 
246
- describe '#exists?' do
247
- before do
248
- browser.goto WatirSpec.url_for('removed_element.html')
242
+ browser.refresh
243
+ expect(element).not_to exist
244
+ end
249
245
  end
250
246
 
251
- it 'element from a collection is re-looked up after it becomes stale',
252
- except: {browser: :ie, reason: 'throwing NoSuchElement instead'} do
253
- element = browser.divs(id: 'text').first.locate
254
-
255
- browser.refresh
256
-
257
- expect(element).to be_stale
258
- expect(element).to exist
259
- end
247
+ describe '#exists?' do
248
+ before do
249
+ browser.goto WatirSpec.url_for('removed_element.html')
250
+ end
260
251
 
261
- it 'element from a selenium element throws an exception when relocated',
262
- except: {browser: :ie, reason: 'throwing NoSuchElement instead'} do
263
- div = browser.div.locate
264
- element = browser.element(element: div.wd)
252
+ it 'element from a collection is re-looked up after it becomes stale',
253
+ except: {browser: :ie, reason: 'throwing NoSuchElement instead'} do
254
+ element = browser.divs(id: 'text').first.locate
265
255
 
266
- browser.refresh
267
- expect(element).to be_stale
256
+ browser.refresh
268
257
 
269
- msg = 'Can not relocate a Watir element initialized by a Selenium element'
270
- expect { element.exists? }.to raise_exception(Watir::Exception::LocatorException, msg)
271
- end
258
+ expect(element).to be_stale
259
+ expect(element).to exist
260
+ end
272
261
 
273
- it 'element from a selenium element with other locators throws an exception' do
274
- div = browser.div.locate
275
- element = browser.element(element: div.wd, id: 'foo')
262
+ it 'element from a selenium element throws an exception when relocated',
263
+ except: {browser: :ie, reason: 'throwing NoSuchElement instead'} do
264
+ div = browser.div.locate
265
+ element = browser.element(element: div.wd)
276
266
 
277
- browser.refresh
267
+ browser.refresh
268
+ expect(element).to be_stale
278
269
 
279
- msg = 'Can not relocate a Watir element initialized by a Selenium element'
280
- expect { element.exists? }.to raise_exception(Watir::Exception::LocatorException, msg)
281
- end
270
+ msg = 'Can not relocate a Watir element initialized by a Selenium element'
271
+ expect { element.exists? }.to raise_exception(Watir::Exception::LocatorException, msg)
272
+ end
282
273
 
283
- it 'returns false when tag name does not match id' do
284
- watir_element = browser.span(id: 'text')
285
- expect(watir_element).to_not exist
286
- end
287
- end
274
+ it 'element from a selenium element with other locators throws an exception' do
275
+ div = browser.div.locate
276
+ element = browser.element(element: div.wd, id: 'foo')
288
277
 
289
- describe '#present?' do
290
- before do
291
- browser.goto(WatirSpec.url_for('wait.html'))
292
- end
278
+ browser.refresh
293
279
 
294
- it 'returns true if the element exists and is visible' do
295
- expect(browser.div(id: 'foo')).to be_present
296
- end
280
+ msg = 'Can not relocate a Watir element initialized by a Selenium element'
281
+ expect { element.exists? }.to raise_exception(Watir::Exception::LocatorException, msg)
282
+ end
297
283
 
298
- it 'returns false if the element exists but is not visible' do
299
- expect(browser.div(id: 'bar')).to_not be_present
284
+ it 'returns false when tag name does not match id' do
285
+ watir_element = browser.span(id: 'text')
286
+ expect(watir_element).not_to exist
287
+ end
300
288
  end
301
289
 
302
- it 'returns false if the element does not exist' do
303
- expect(browser.div(id: 'should-not-exist')).to_not be_present
304
- end
290
+ describe '#present?' do
291
+ before do
292
+ browser.goto(WatirSpec.url_for('wait.html'))
293
+ end
305
294
 
306
- it 'handles staleness' do
307
- element = browser.div(id: 'foo').locate
295
+ it 'returns true if the element exists and is visible' do
296
+ expect(browser.div(id: 'foo')).to be_present
297
+ end
308
298
 
309
- allow(element).to receive(:stale?).and_return(true)
299
+ it 'returns false if the element exists but is not visible' do
300
+ expect(browser.div(id: 'bar')).not_to be_present
301
+ end
310
302
 
311
- expect(element).to be_present
312
- end
313
- end
303
+ it 'returns false if the element does not exist' do
304
+ expect(browser.div(id: 'should-not-exist')).not_to be_present
305
+ end
314
306
 
315
- describe '#enabled?' do
316
- before do
317
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
318
- end
307
+ it 'handles staleness' do
308
+ element = browser.div(id: 'foo').locate
319
309
 
320
- it 'returns true if the element is enabled' do
321
- expect(browser.button(name: 'new_user_submit')).to be_enabled
322
- end
310
+ allow(element).to receive(:stale?).and_return(true)
323
311
 
324
- it 'returns false if the element is disabled' do
325
- expect(browser.button(name: 'new_user_submit_disabled')).to_not be_enabled
312
+ expect(element).to be_present
313
+ end
326
314
  end
327
315
 
328
- it "raises UnknownObjectException if the element doesn't exist" do
329
- expect { browser.button(name: 'no_such_name').enabled? }.to raise_unknown_object_exception
330
- end
331
- end
316
+ describe '#enabled?' do
317
+ before do
318
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
319
+ end
332
320
 
333
- describe '#stale?' do
334
- it 'returns true if the element is stale' do
335
- element = browser.button(name: 'new_user_submit_disabled').locate
321
+ it 'returns true if the element is enabled' do
322
+ expect(browser.button(name: 'new_user_submit')).to be_enabled
323
+ end
336
324
 
337
- browser.refresh
325
+ it 'returns false if the element is disabled' do
326
+ expect(browser.button(name: 'new_user_submit_disabled')).not_to be_enabled
327
+ end
338
328
 
339
- expect(element).to be_stale
329
+ it "raises UnknownObjectException if the element doesn't exist" do
330
+ expect { browser.button(name: 'no_such_name').enabled? }.to raise_unknown_object_exception
331
+ end
340
332
  end
341
333
 
342
- it 'returns true the second time if the element is stale',
343
- except: {browser: :ie, reason: 'throwing NoSuchElement instead'} do
344
- element = browser.div.locate
334
+ describe '#stale?' do
335
+ it 'returns true if the element is stale' do
336
+ element = browser.button(name: 'new_user_submit_disabled').locate
345
337
 
346
- browser.refresh
338
+ browser.refresh
347
339
 
348
- expect(element).to be_stale
349
- expect(element).to be_stale
350
- end
351
-
352
- it 'returns false if the element is not stale' do
353
- element = browser.button(name: 'new_user_submit_disabled').locate
340
+ expect(element).to be_stale
341
+ end
354
342
 
355
- expect(element).to_not be_stale
356
- end
357
- end
343
+ it 'returns true the second time if the element is stale',
344
+ except: {browser: :ie, reason: 'throwing NoSuchElement instead'} do
345
+ element = browser.div.locate
358
346
 
359
- describe '#exist?' do
360
- context ':class locator' do
361
- before do
362
- browser.goto(WatirSpec.url_for('class_locator.html'))
363
- end
347
+ browser.refresh
364
348
 
365
- it 'matches when the element has a single class' do
366
- e = browser.div(class: 'a')
367
- expect(e).to exist
368
- expect(e.class_name).to eq 'a'
349
+ expect(element).to be_stale
350
+ expect(element).to be_stale
369
351
  end
370
352
 
371
- it 'matches when the element has several classes' do
372
- e = browser.div(class: 'b')
373
- expect(e).to exist
374
- expect(e.class_name).to eq 'a b c'
375
- end
353
+ it 'returns false if the element is not stale' do
354
+ element = browser.button(name: 'new_user_submit_disabled').locate
376
355
 
377
- it 'does not match only part of the class name' do
378
- expect(browser.div(class: 'bc')).to_not exist
356
+ expect(element).not_to be_stale
379
357
  end
358
+ end
380
359
 
381
- it 'matches part of the class name when given a regexp' do
382
- expect(browser.div(class: /c/)).to exist
383
- end
360
+ describe '#exist?' do
361
+ context 'when using :class locator' do
362
+ before do
363
+ browser.goto(WatirSpec.url_for('class_locator.html'))
364
+ end
384
365
 
385
- context 'with multiple classes' do
386
366
  it 'matches when the element has a single class' do
387
- e = browser.div(class: ['a'])
367
+ e = browser.div(class: 'a')
388
368
  expect(e).to exist
389
369
  expect(e.class_name).to eq 'a'
390
370
  end
391
371
 
392
- it 'matches a non-ordered subset' do
393
- e = browser.div(class: %w[c a])
372
+ it 'matches when the element has several classes' do
373
+ e = browser.div(class: 'b')
394
374
  expect(e).to exist
395
375
  expect(e.class_name).to eq 'a b c'
396
376
  end
397
377
 
398
- it 'matches one with a negation' do
399
- e = browser.div(class: ['!a'])
400
- expect(e).to exist
401
- expect(e.class_name).to eq 'abc'
378
+ it 'does not match only part of the class name' do
379
+ expect(browser.div(class: 'bc')).not_to exist
402
380
  end
403
381
 
404
- it 'matches multiple with a negation' do
405
- e = browser.div(class: ['a', '!c', 'b'])
406
- expect(e).to exist
407
- expect(e.class_name).to eq 'a b'
382
+ it 'matches part of the class name when given a regexp' do
383
+ expect(browser.div(class: /c/)).to exist
384
+ end
385
+
386
+ context 'with multiple classes' do
387
+ it 'matches when the element has a single class' do
388
+ e = browser.div(class: ['a'])
389
+ expect(e).to exist
390
+ expect(e.class_name).to eq 'a'
391
+ end
392
+
393
+ it 'matches a non-ordered subset' do
394
+ e = browser.div(class: %w[c a])
395
+ expect(e).to exist
396
+ expect(e.class_name).to eq 'a b c'
397
+ end
398
+
399
+ it 'matches one with a negation' do
400
+ e = browser.div(class: ['!a'])
401
+ expect(e).to exist
402
+ expect(e.class_name).to eq 'abc'
403
+ end
404
+
405
+ it 'matches multiple with a negation' do
406
+ e = browser.div(class: ['a', '!c', 'b'])
407
+ expect(e).to exist
408
+ expect(e.class_name).to eq 'a b'
409
+ end
410
+
411
+ it 'matches with any whitespace' do
412
+ e = browser.div(class: %w[newline tabbed])
413
+ expect(e).to exist
414
+ expect(e.class_name).to eq "first\nnewline\ttabbed last"
415
+ end
408
416
  end
409
417
  end
410
- end
411
418
 
412
- context 'attribute presence' do
413
- before { browser.goto WatirSpec.url_for('data_attributes.html') }
419
+ context 'when using attribute presence' do
420
+ before { browser.goto WatirSpec.url_for('data_attributes.html') }
421
+
422
+ it 'finds element by attribute presence' do
423
+ expect(browser.p(data_type: true)).to exist
424
+ expect(browser.p(class: true)).not_to exist
425
+ end
414
426
 
415
- it 'finds element by attribute presence' do
416
- expect(browser.p(data_type: true)).to exist
417
- expect(browser.p(class: true)).not_to exist
427
+ it 'finds element by attribute absence' do
428
+ expect(browser.p(data_type: false)).not_to exist
429
+ expect(browser.p(class: false)).to exist
430
+ end
418
431
  end
419
432
 
420
- it 'finds element by attribute absence' do
421
- expect(browser.p(data_type: false)).not_to exist
422
- expect(browser.p(class: false)).to exist
433
+ context 'when using :index locator' do
434
+ before { browser.goto WatirSpec.url_for('data_attributes.html') }
435
+
436
+ it 'finds the first element by index: 0' do
437
+ expect(browser.element(index: 0).tag_name).to eq 'html'
438
+ end
439
+
440
+ it 'finds the second element by index: 1' do
441
+ expect(browser.element(index: 1).tag_name).to eq 'head'
442
+ end
443
+
444
+ it 'finds the last element by index: -1' do
445
+ expect(browser.element(index: -1).tag_name).to eq 'div'
446
+ end
423
447
  end
424
- end
425
448
 
426
- context ':index locator' do
427
- before { browser.goto WatirSpec.url_for('data_attributes.html') }
449
+ it "doesn't raise when called on nested elements" do
450
+ expect(browser.div(id: 'no_such_div').link(id: 'no_such_id')).not_to exist
451
+ end
428
452
 
429
- it 'finds the first element by index: 0' do
430
- expect(browser.element(index: 0).tag_name).to eq 'html'
453
+ it "doesn't raise when selector has with :xpath has :index" do
454
+ expect(browser.div(xpath: '//div', index: 1)).to exist
431
455
  end
432
456
 
433
- it 'finds the second element by index: 1' do
434
- expect(browser.element(index: 1).tag_name).to eq 'head'
457
+ it "doesn't raise when selector has with :css has :index" do
458
+ expect(browser.div(css: 'div', index: 1)).to exist
435
459
  end
436
460
 
437
- it 'finds the last element by index: -1' do
438
- expect(browser.element(index: -1).tag_name).to eq 'div'
461
+ it 'finds element by Selenium name locator' do
462
+ expect(browser.element(name: 'new_user_first_name')).to exist
463
+ expect(browser.element(name: /new_user_first_name/)).to exist
439
464
  end
440
- end
441
465
 
442
- it "doesn't raise when called on nested elements" do
443
- expect(browser.div(id: 'no_such_div').link(id: 'no_such_id')).to_not exist
466
+ it 'returns false when tag name does not match id' do
467
+ watir_element = browser.span(id: 'text')
468
+ expect(watir_element).not_to exist
469
+ end
444
470
  end
445
471
 
446
- it "doesn't raise when selector has with :xpath has :index" do
447
- expect(browser.div(xpath: '//div', index: 1)).to exist
448
- end
472
+ describe '#send_keys' do
473
+ before do
474
+ browser.goto(WatirSpec.url_for('keylogger.html'))
475
+ end
449
476
 
450
- it "doesn't raise when selector has with :css has :index" do
451
- expect(browser.div(css: 'div', index: 1)).to exist
452
- end
477
+ let(:receiver) { browser.text_field(id: 'receiver') }
478
+ let(:events) { browser.element(id: 'output').ps.size }
453
479
 
454
- it 'finds element by Selenium name locator' do
455
- expect(browser.element(name: 'new_user_first_name')).to exist
456
- expect(browser.element(name: /new_user_first_name/)).to exist
457
- end
480
+ it 'sends keystrokes to the element' do
481
+ receiver.send_keys 'hello world'
482
+ expect(receiver.value).to eq 'hello world'
483
+ expect(events).to eq 11
484
+ end
458
485
 
459
- it 'returns false when tag name does not match id' do
460
- watir_element = browser.span(id: 'text')
461
- expect(watir_element).to_not exist
462
- end
463
- end
486
+ it 'accepts arbitrary list of arguments' do
487
+ receiver.send_keys 'hello', 'world'
488
+ expect(receiver.value).to eq 'helloworld'
489
+ expect(events).to eq 10
490
+ end
464
491
 
465
- describe '#send_keys' do
466
- before(:each) do
467
- browser.goto(WatirSpec.url_for('keylogger.html'))
468
- end
492
+ context 'with key combinations',
493
+ except: [{browser: :firefox, reason: 'https://github.com/mozilla/geckodriver/issues/245'},
494
+ {browser: :safari}] do
495
+ it 'performs from array' do
496
+ receiver.send_keys 'foo'
497
+ receiver.send_keys [@c, 'a']
498
+ receiver.send_keys :backspace
499
+ expect(receiver.value).to be_empty
500
+ expect(events).to eq 6
501
+ end
469
502
 
470
- let(:receiver) { browser.text_field(id: 'receiver') }
471
- let(:events) { browser.element(id: 'output').ps.size }
503
+ it 'performs from multiple arrays' do
504
+ receiver.send_keys 'foo'
505
+ receiver.send_keys [@c, 'a'], [@c, 'x']
506
+ expect(receiver.value).to be_empty
507
+ expect(events).to eq 7
508
+ end
472
509
 
473
- it 'sends keystrokes to the element' do
474
- receiver.send_keys 'hello world'
475
- expect(receiver.value).to eq 'hello world'
476
- expect(events).to eq 11
510
+ it 'supports combination of strings and arrays' do
511
+ receiver.send_keys 'foo', [@c, 'a'], :backspace
512
+ expect(receiver.value).to be_empty
513
+ expect(events).to eq 6
514
+ end
515
+ end
477
516
  end
478
517
 
479
- it 'accepts arbitrary list of arguments' do
480
- receiver.send_keys 'hello', 'world'
481
- expect(receiver.value).to eq 'helloworld'
482
- expect(events).to eq 10
518
+ describe '#click' do
519
+ it 'accepts modifiers', except: {browser: :ie} do
520
+ browser.a.click(@c)
521
+ expect { browser.windows.wait_until(size: 2) }.not_to raise_exception
522
+ ensure
523
+ browser.windows.restore!
524
+ end
483
525
  end
484
526
 
485
- context 'with key combinations',
486
- except: [{browser: :firefox, reason: 'https://github.com/mozilla/geckodriver/issues/245'},
487
- {browser: :safari}] do
488
- it 'performs from array' do
489
- receiver.send_keys 'foo'
490
- receiver.send_keys [@c, 'a']
491
- receiver.send_keys :backspace
492
- expect(receiver.value).to be_empty
493
- expect(events).to eq 6
527
+ describe '#set' do
528
+ it 'clicks an element by default that does not define #set' do
529
+ browser.goto(WatirSpec.url_for('non_control_elements.html'))
530
+ browser.element(id: 'best_language').set
531
+ expect(browser.div(id: 'best_language').text).to eq 'Ruby!'
494
532
  end
495
533
 
496
- it 'performs from multiple arrays' do
497
- receiver.send_keys 'foo'
498
- receiver.send_keys [@c, 'a'], [@c, 'x']
499
- expect(receiver.value).to be_empty
500
- expect(events).to eq 7
534
+ it 'clicks an element that does not define #set with provided modifiers', except: {browser: :ie} do
535
+ browser.a.set(@c)
536
+ browser.wait_until { |b| b.windows.size > 1 }
537
+ expect(browser.windows.size).to eq 2
538
+ ensure
539
+ browser.windows.restore!
501
540
  end
502
541
 
503
- it 'supports combination of strings and arrays' do
504
- receiver.send_keys 'foo', [@c, 'a'], :backspace
505
- expect(receiver.value).to be_empty
506
- expect(events).to eq 6
542
+ it 'does not click an element that does not define #set when passed false' do
543
+ browser.goto(WatirSpec.url_for('non_control_elements.html'))
544
+ browser.element(id: 'best_language').set(false)
545
+ expect(browser.div(id: 'best_language').text).not_to eq 'Ruby!'
507
546
  end
508
- end
509
- end
510
547
 
511
- describe '#click' do
512
- it 'accepts modifiers', except: {browser: :ie} do
513
- browser.a.click(@c)
514
- expect { browser.windows.wait_until(size: 2) }.to_not raise_exception
515
- ensure
516
- browser.windows.restore!
517
- end
518
- end
548
+ it 'clicks a Button' do
549
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
550
+ browser.button(id: 'delete_user_submit').set
551
+ browser.wait_until { |b| !b.url.include? 'forms_with_input_elements.html' }
552
+ expect(browser.text).to include('Semantic table')
553
+ end
519
554
 
520
- describe '#set' do
521
- it 'clicks an element by default that does not define #set' do
522
- browser.goto(WatirSpec.url_for('non_control_elements.html'))
523
- browser.element(id: 'best_language').set
524
- expect(browser.div(id: 'best_language').text).to eq 'Ruby!'
525
- end
555
+ it 'sends keys to text element' do
556
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
557
+ browser.element(id: 'new_user_email').set('Bye Cruel World')
558
+ expect(browser.text_field(id: 'new_user_email').value).to eq 'Bye Cruel World'
559
+ end
526
560
 
527
- it 'clicks an element that does not define #set with provided modifiers', except: {browser: :ie} do
528
- browser.a.set(@c)
529
- browser.wait_until { |b| b.windows.size > 1 }
530
- expect(browser.windows.size).to eq 2
531
- ensure
532
- browser.windows.restore!
533
- end
561
+ it 'sends keys to text area' do
562
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
563
+ browser.element(id: 'delete_user_comment').set('Hello Cruel World')
564
+ expect(browser.textarea(id: 'delete_user_comment').value).to eq 'Hello Cruel World'
565
+ end
534
566
 
535
- it 'does not click an element that does not define #set when passed false' do
536
- browser.goto(WatirSpec.url_for('non_control_elements.html'))
537
- browser.element(id: 'best_language').set(false)
538
- expect(browser.div(id: 'best_language').text).not_to eq 'Ruby!'
539
- end
567
+ it 'checks a checkbox' do
568
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
569
+ browser.element(id: 'new_user_interests_cars').set
570
+ expect(browser.checkbox(id: 'new_user_interests_cars')).to be_set
571
+ end
540
572
 
541
- it 'clicks a Button' do
542
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
543
- browser.button(id: 'delete_user_submit').set
544
- browser.wait_until { |b| !b.url.include? 'forms_with_input_elements.html' }
545
- expect(browser.text).to include('Semantic table')
546
- end
573
+ it 'unchecks a checkbox' do
574
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
575
+ browser.element(id: 'new_user_interests_books').set(false)
576
+ expect(browser.checkbox(id: 'new_user_interests_books')).not_to be_set
577
+ end
547
578
 
548
- it 'sends keys to text element' do
549
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
550
- browser.element(id: 'new_user_email').set('Bye Cruel World')
551
- expect(browser.text_field(id: 'new_user_email').value).to eq 'Bye Cruel World'
552
- end
579
+ it 'clicks a Radio' do
580
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
581
+ browser.radio(id: 'new_user_newsletter_no').set
582
+ expect(browser.radio(id: 'new_user_newsletter_no')).to be_set
583
+ expect(messages.size).to eq 1
584
+ end
553
585
 
554
- it 'sends keys to text area' do
555
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
556
- browser.element(id: 'delete_user_comment').set('Hello Cruel World')
557
- expect(browser.textarea(id: 'delete_user_comment').value).to eq 'Hello Cruel World'
558
- end
586
+ it 'does not click a Radio when false or already clicked' do
587
+ browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
588
+ browser.element(id: 'new_user_newsletter_no').set(false)
589
+ browser.element(id: 'new_user_newsletter_yes').set(true)
590
+ expect(messages.size).to eq 0
591
+ end
559
592
 
560
- it 'checks a checkbox' do
561
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
562
- browser.element(id: 'new_user_interests_cars').set
563
- expect(browser.checkbox(id: 'new_user_interests_cars')).to be_set
564
- end
593
+ it 'uploads a file' do
594
+ browser.element(name: 'new_user_portrait').set __FILE__
565
595
 
566
- it 'unchecks a checkbox' do
567
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
568
- browser.element(id: 'new_user_interests_books').set(false)
569
- expect(browser.checkbox(id: 'new_user_interests_books')).to_not be_set
570
- end
596
+ expect(browser.file_field(name: 'new_user_portrait').value).to include(File.basename(__FILE__))
597
+ end
571
598
 
572
- it 'clicks a Radio' do
573
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
574
- browser.radio(id: 'new_user_newsletter_no').set
575
- expect(browser.radio(id: 'new_user_newsletter_no')).to be_set
576
- expect(messages.size).to eq 1
577
- end
599
+ it 'selects an option' do
600
+ browser.select_list(name: 'new_user_languages').clear
601
+ browser.element(name: 'new_user_languages').set('2')
602
+ expect(browser.select_list(name: 'new_user_languages').selected_options.map(&:text)).to eq %w[EN]
603
+ end
578
604
 
579
- it 'does not click a Radio when false or already clicked' do
580
- browser.goto(WatirSpec.url_for('forms_with_input_elements.html'))
581
- browser.element(id: 'new_user_newsletter_no').set(false)
582
- browser.element(id: 'new_user_newsletter_yes').set(true)
583
- expect(messages.size).to eq 0
605
+ it 'sends keys to content editable element' do
606
+ browser.element(id: 'contenteditable').set('Bar')
607
+ expect(browser.div(id: 'contenteditable').text).to eq 'Bar'
608
+ end
584
609
  end
585
610
 
586
- it 'uploads a file' do
587
- browser.element(name: 'new_user_portrait').set __FILE__
611
+ describe '#flash' do
612
+ let(:h2) { browser.h2(text: 'Add user') }
613
+ let(:h1) { browser.h1(text: 'User administration') }
588
614
 
589
- expect(browser.file_field(name: 'new_user_portrait').value).to include(File.basename(__FILE__))
590
- end
615
+ it 'returns the element on which it was called' do
616
+ expect(h2.flash).to eq h2
617
+ end
591
618
 
592
- it 'selects an option' do
593
- browser.select_list(name: 'new_user_languages').clear
594
- browser.element(name: 'new_user_languages').set('2')
595
- expect(browser.select_list(name: 'new_user_languages').selected_options.map(&:text)).to eq %w[EN]
596
- end
619
+ it 'keeps the element background color after flashing' do
620
+ expect(h2.style('background-color')).to eq h2.flash(:rainbow).style('background-color')
621
+ expect(h1.style('background-color')).to eq h1.flash.style('background-color')
622
+ end
597
623
 
598
- it 'sends keys to content editable element' do
599
- browser.element(id: 'contenteditable').set('Bar')
600
- expect(browser.div(id: 'contenteditable').text).to eq 'Bar'
624
+ it 'responds to preset symbols like :fast and :slow' do
625
+ expect(h1.flash(:rainbow)).to eq h1
626
+ expect(h2.flash(:slow)).to eq h2
627
+ expect(h1.flash(:fast)).to eq h1
628
+ expect(h2.flash(:long)).to eq h2
629
+ end
601
630
  end
602
- end
603
631
 
604
- describe '#flash' do
605
- let(:h2) { browser.h2(text: 'Add user') }
606
- let(:h1) { browser.h1(text: 'User administration') }
632
+ describe '#hover' do
633
+ def element_color(element)
634
+ case element.style('color')
635
+ when 'rgba(0, 0, 255, 1)'
636
+ :blue
637
+ when 'rgba(255, 165, 0, 1)', 'rgb(255, 165, 0)'
638
+ :orange
639
+ else
640
+ raise rgba
641
+ end
642
+ end
607
643
 
608
- it 'returns the element on which it was called' do
609
- expect(h2.flash).to eq h2
610
- end
644
+ it 'allows scrolling to top', except: {browser: :ie} do
645
+ browser.goto(WatirSpec.url_for('scroll.html'))
646
+ element = browser.div(id: 'center')
611
647
 
612
- it 'should keep the element background color after flashing' do
613
- expect(h2.style('background-color')).to eq h2.flash(:rainbow).style('background-color')
614
- expect(h1.style('background-color')).to eq h1.flash.style('background-color')
615
- end
648
+ element.hover(scroll_to: :top)
649
+ expect(element_color(element)).to eq :orange
616
650
 
617
- it 'should respond to preset symbols like :fast and :slow' do
618
- expect(h1.flash(:rainbow)).to eq h1
619
- expect(h2.flash(:slow)).to eq h2
620
- expect(h1.flash(:fast)).to eq h1
621
- expect(h2.flash(:long)).to eq h2
622
- end
623
- end
651
+ element_top = browser.execute_script('return arguments[0].getBoundingClientRect().top', element)
652
+ expect(element_top).to be_within(1).of(0)
653
+ end
624
654
 
625
- describe '#hover' do
626
- it 'should hover over the element', except: {browser: :ie} do
627
- browser.goto WatirSpec.url_for('hover.html')
628
- link = browser.a
655
+ it 'scrolls to center by default', except: {browser: :ie} do
656
+ browser.goto(WatirSpec.url_for('scroll.html'))
657
+ element = browser.div(id: 'center')
629
658
 
630
- expect(link.style('font-size')).to eq '10px'
631
- link.scroll.to
632
- link.hover
633
- link.wait_until { |l| l.style('font-size') == '20px' }
634
- expect(link.style('font-size')).to eq '20px'
635
- end
636
- end
659
+ element.hover
660
+ expect(element_color(element)).to eq :orange
637
661
 
638
- describe '#inspect' do
639
- before(:each) { browser.goto(WatirSpec.url_for('nested_iframes.html')) }
662
+ element_rect = browser.execute_script('return arguments[0].getBoundingClientRect()', element)
640
663
 
641
- it 'does displays specified element type' do
642
- expect(browser.div.inspect).to include('Watir::Div')
643
- end
664
+ expect(element_rect['top']).to eq(element_rect['bottom'] - element_rect['height'])
665
+ end
644
666
 
645
- it 'does not display element type if not specified' do
646
- element = browser.element(index: 4)
647
- expect(element.inspect).to include('Watir::HTMLElement')
648
- end
667
+ it 'allows scrolling to bottom', except: {browser: :ie} do
668
+ browser.goto(WatirSpec.url_for('scroll.html'))
669
+ element = browser.div(id: 'center')
649
670
 
650
- it 'displays keyword if specified' do
651
- element = browser.h3
652
- element.keyword = 'foo'
653
- expect(element.inspect).to include('keyword: foo')
654
- end
671
+ element.hover(scroll_to: :bottom)
672
+ expect(element_color(element)).to eq :orange
655
673
 
656
- it 'does not display keyword if not specified' do
657
- element = browser.h3
658
- expect(element.inspect).to_not include('keyword')
659
- end
674
+ element_bottom = browser.execute_script('return arguments[0].getBoundingClientRect().bottom', element)
675
+ window_height = browser.execute_script('return window.innerHeight')
660
676
 
661
- it 'locate is false when not located' do
662
- element = browser.div(id: 'not_present')
663
- expect(element.inspect).to include('located: false')
664
- end
677
+ expect(element_bottom).to be_within(1).of(window_height)
678
+ end
665
679
 
666
- it 'locate is true when located' do
667
- element = browser.h3
668
- element.exists?
669
- expect(element.inspect).to include('located: true')
670
- end
680
+ it 'allows not scrolling', except: {browser: %i[chrome edge],
681
+ reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3955'} do
682
+ browser.goto(WatirSpec.url_for('scroll.html'))
683
+ element = browser.div(id: 'center')
671
684
 
672
- it 'displays selector string for element from colection' do
673
- elements = browser.frames
674
- expect(elements.last.inspect).to include '{:tag_name=>"frame", :index=>-1}'
675
- end
685
+ browser.execute_script('return window.pageYOffset;')
686
+ browser.execute_script('return window.innerHeight;')
676
687
 
677
- it 'displays selector string for nested element' do
678
- browser.goto(WatirSpec.url_for('wait.html'))
679
- element = browser.div(index: 1).div(id: 'div2')
680
- expect(element.inspect).to include '{:index=>1, :tag_name=>"div"} --> {:id=>"div2", :tag_name=>"div"}'
688
+ expect {
689
+ element.hover(scroll_to: nil)
690
+ }.to raise_exception Selenium::WebDriver::Error::MoveTargetOutOfBoundsError
691
+ end
681
692
  end
682
693
 
683
- it 'displays selector string for nested element under frame' do
684
- element = browser.iframe(id: 'one').iframe(id: 'three')
685
- expect(element.inspect).to include '{:id=>"one", :tag_name=>"iframe"} --> {:id=>"three", :tag_name=>"iframe"}'
694
+ describe '#inspect' do
695
+ before { browser.goto(WatirSpec.url_for('nested_iframes.html')) }
696
+
697
+ it 'does displays specified element type' do
698
+ expect(browser.div.inspect).to include('Watir::Div')
699
+ end
700
+
701
+ it 'does not display element type if not specified' do
702
+ element = browser.element(index: 4)
703
+ expect(element.inspect).to include('Watir::HTMLElement')
704
+ end
705
+
706
+ it 'displays keyword if specified' do
707
+ element = browser.h3
708
+ element.keyword = 'foo'
709
+ expect(element.inspect).to include('keyword: foo')
710
+ end
711
+
712
+ it 'does not display keyword if not specified' do
713
+ element = browser.h3
714
+ expect(element.inspect).not_to include('keyword')
715
+ end
716
+
717
+ it 'locate is false when not located' do
718
+ element = browser.div(id: 'not_present')
719
+ expect(element.inspect).to include('located: false')
720
+ end
721
+
722
+ it 'locate is true when located' do
723
+ element = browser.h3
724
+ element.exists?
725
+ expect(element.inspect).to include('located: true')
726
+ end
727
+
728
+ it 'displays selector string for element from colection' do
729
+ elements = browser.frames
730
+ expect(elements.last.inspect).to include '{:tag_name=>"frame", :index=>-1}'
731
+ end
732
+
733
+ it 'displays selector string for nested element' do
734
+ browser.goto(WatirSpec.url_for('wait.html'))
735
+ element = browser.div(index: 1).div(id: 'div2')
736
+ expect(element.inspect).to include '{:index=>1, :tag_name=>"div"} --> {:id=>"div2", :tag_name=>"div"}'
737
+ end
738
+
739
+ it 'displays selector string for nested element under frame' do
740
+ element = browser.iframe(id: 'one').iframe(id: 'three')
741
+ expect(element.inspect).to include '{:id=>"one", :tag_name=>"iframe"} --> {:id=>"three", :tag_name=>"iframe"}'
742
+ end
686
743
  end
687
- end
688
744
 
689
- describe '#text_content' do
690
- it 'returns inner Text code of element' do
691
- browser.goto WatirSpec.url_for('non_control_elements.html')
692
- expect(browser.div(id: 'shown').text_content).to eq('Not shownNot hidden')
745
+ describe '#text_content' do
746
+ it 'returns inner Text code of element' do
747
+ browser.goto WatirSpec.url_for('non_control_elements.html')
748
+ expect(browser.div(id: 'shown').text_content).to eq('Not shownNot hidden')
749
+ end
693
750
  end
694
- end
695
751
 
696
- describe '#inner_text' do
697
- it 'returns inner HTML code of element',
698
- except: {browser: :ie, reason: 'also returning explicitly hidden results'} do
699
- browser.goto WatirSpec.url_for('non_control_elements.html')
700
- div = browser.div(id: 'shown')
701
- expect(div.inner_text).to eq('Not hidden')
752
+ describe '#inner_text' do
753
+ it 'returns inner HTML code of element',
754
+ except: {browser: :ie, reason: 'also returning explicitly hidden results'} do
755
+ browser.goto WatirSpec.url_for('non_control_elements.html')
756
+ div = browser.div(id: 'shown')
757
+ expect(div.inner_text).to eq('Not hidden')
758
+ end
702
759
  end
703
- end
704
760
 
705
- describe '#inner_html' do
706
- it 'returns inner HTML code of element' do
707
- browser.goto WatirSpec.url_for('non_control_elements.html')
708
- div = browser.div(id: 'shown')
709
- expected_text = '<div id="hidden" style="display: none;">Not shown</div><div>Not hidden</div>'
710
- expect(div.inner_html).to eq expected_text
761
+ describe '#inner_html' do
762
+ it 'returns inner HTML code of element' do
763
+ browser.goto WatirSpec.url_for('non_control_elements.html')
764
+ div = browser.div(id: 'shown')
765
+ expected_text = '<div id="hidden" style="display: none;">Not shown</div><div>Not hidden</div>'
766
+ expect(div.inner_html).to eq expected_text
767
+ end
711
768
  end
712
- end
713
769
 
714
- describe '#outer_html' do
715
- it 'returns outer (inner + element itself) HTML code of element' do
716
- browser.goto WatirSpec.url_for('non_control_elements.html')
717
- div = browser.div(id: 'shown')
718
- text = '<div id="shown"><div id="hidden" style="display: none;">Not shown</div><div>Not hidden</div></div>'
719
- expect(div.outer_html).to eq text
770
+ describe '#outer_html' do
771
+ it 'returns outer (inner + element itself) HTML code of element' do
772
+ browser.goto WatirSpec.url_for('non_control_elements.html')
773
+ div = browser.div(id: 'shown')
774
+ text = '<div id="shown"><div id="hidden" style="display: none;">Not shown</div><div>Not hidden</div></div>'
775
+ expect(div.outer_html).to eq text
776
+ end
720
777
  end
721
- end
722
778
 
723
- describe '#select_text and #selected_text' do
724
- it 'selects text and returns selected text',
725
- except: {browser: :ie, reason: 'Select Text atom appears broken in IE 11'} do
726
- browser.goto WatirSpec.url_for('non_control_elements.html')
727
- element = browser.element(visible_text: 'all visible')
728
- element.select_text('all visible')
729
- expect(element.selected_text).to eq 'all visible'
779
+ describe '#select_text and #selected_text' do
780
+ it 'selects text and returns selected text',
781
+ except: {browser: :ie, reason: 'Select Text atom appears broken in IE 11'} do
782
+ browser.goto WatirSpec.url_for('non_control_elements.html')
783
+ element = browser.element(visible_text: 'all visible')
784
+ element.select_text('all visible')
785
+ expect(element.selected_text).to eq 'all visible'
786
+ end
730
787
  end
731
- end
732
788
 
733
- describe '#location' do
734
- it 'returns coordinates for element location' do
735
- location = browser.button(name: 'new_user_image').location
789
+ describe '#location' do
790
+ it 'returns coordinates for element location' do
791
+ location = browser.button(name: 'new_user_image').location
736
792
 
737
- expect(location).to be_a Selenium::WebDriver::Point
738
- expect(location['y']).to be > 0
739
- expect(location['x']).to be > 0
793
+ expect(location).to be_a Selenium::WebDriver::Point
794
+ expect(location['y']).to be > 0
795
+ expect(location['x']).to be > 0
796
+ end
740
797
  end
741
- end
742
798
 
743
- describe '#size' do
744
- it 'returns size of element' do
745
- size = browser.button(name: 'new_user_image').size
746
- expect(size).to be_a Selenium::WebDriver::Dimension
799
+ describe '#size' do
800
+ it 'returns size of element' do
801
+ size = browser.button(name: 'new_user_image').size
802
+ expect(size).to be_a Selenium::WebDriver::Dimension
747
803
 
748
- expected_width = browser.name == :safari ? 105 : 104
749
- expected_height = browser.name == :safari ? 71 : 70
804
+ expected_width = browser.name == :safari ? 105 : 104
805
+ expected_height = browser.name == :safari ? 71 : 70
750
806
 
751
- expect(size['width']).to eq expected_width
752
- expect(size['height']).to eq expected_height
807
+ expect(size['width']).to eq expected_width
808
+ expect(size['height']).to eq expected_height
809
+ end
753
810
  end
754
- end
755
811
 
756
- describe '#height' do
757
- it 'returns height of element' do
758
- height = browser.button(name: 'new_user_image').height
812
+ describe '#height' do
813
+ it 'returns height of element' do
814
+ height = browser.button(name: 'new_user_image').height
759
815
 
760
- expected_height = browser.name == :safari ? 71 : 70
761
- expect(height).to eq expected_height
816
+ expected_height = browser.name == :safari ? 71 : 70
817
+ expect(height).to eq expected_height
818
+ end
762
819
  end
763
- end
764
820
 
765
- describe '#width' do
766
- it 'returns width of element' do
767
- width = browser.button(name: 'new_user_image').width
821
+ describe '#width' do
822
+ it 'returns width of element' do
823
+ width = browser.button(name: 'new_user_image').width
768
824
 
769
- expected_width = browser.name == :safari ? 105 : 104
770
- expect(width).to eq expected_width
825
+ expected_width = browser.name == :safari ? 105 : 104
826
+ expect(width).to eq expected_width
827
+ end
771
828
  end
772
- end
773
829
 
774
- describe '#center' do
775
- it 'returns center of element' do
776
- center = browser.button(name: 'new_user_image').center
830
+ describe '#center' do
831
+ it 'returns center of element' do
832
+ center = browser.button(name: 'new_user_image').center
777
833
 
778
- expect(center).to be_a Selenium::WebDriver::Point
779
- expect(center['y']).to be > 0.0
780
- expect(center['x']).to be > 0.0
834
+ expect(center).to be_a Selenium::WebDriver::Point
835
+ expect(center['y']).to be > 0.0
836
+ expect(center['x']).to be > 0.0
837
+ end
781
838
  end
782
- end
783
839
 
784
- describe '#attribute_value' do
785
- before { browser.goto WatirSpec.url_for('data_attributes.html') }
840
+ describe '#attribute_value' do
841
+ before { browser.goto WatirSpec.url_for('data_attributes.html') }
786
842
 
787
- it 'returns attribute value by string attribute name' do
788
- expect(browser.p.attribute_value('data-type')).to eq 'ruby-library'
789
- end
843
+ it 'returns attribute value by string attribute name' do
844
+ expect(browser.p.attribute_value('data-type')).to eq 'ruby-library'
845
+ end
790
846
 
791
- it 'returns attribute value by symbol attribute name' do
792
- expect(browser.p.attribute_value(:data_type)).to eq 'ruby-library'
847
+ it 'returns attribute value by symbol attribute name' do
848
+ expect(browser.p.attribute_value(:data_type)).to eq 'ruby-library'
849
+ end
793
850
  end
794
- end
795
851
 
796
- describe '#attribute_values' do
797
- before { browser.goto WatirSpec.url_for('data_attributes.html') }
852
+ describe '#attribute_values' do
853
+ before { browser.goto WatirSpec.url_for('data_attributes.html') }
798
854
 
799
- it 'returns a Hash object' do
800
- expect(browser.p.attribute_values).to be_an_instance_of(Hash)
801
- end
855
+ it 'returns a Hash object' do
856
+ expect(browser.p.attribute_values).to be_an_instance_of(Hash)
857
+ end
802
858
 
803
- it 'returns attribute values from an element' do
804
- expected = {data_type: 'ruby-library'}
805
- expect(browser.p.attribute_values).to eq expected
806
- end
859
+ it 'returns attribute values from an element' do
860
+ expected = {data_type: 'ruby-library'}
861
+ expect(browser.p.attribute_values).to eq expected
862
+ end
807
863
 
808
- it 'returns attribute with special characters' do
809
- expected = {data_type: 'description', 'data-type_$p3c!a1' => 'special-description'}
810
- expect(browser.div.attribute_values).to eq expected
811
- end
864
+ it 'returns attribute with special characters' do
865
+ expected = {data_type: 'description', 'data-type_$p3c!a1' => 'special-description'}
866
+ expect(browser.div.attribute_values).to eq expected
867
+ end
812
868
 
813
- it 'returns attribute with special characters as a String' do
814
- expect(browser.div.attribute_values.keys[0]).to be_an_instance_of(String)
869
+ it 'returns attribute with special characters as a String' do
870
+ expect(browser.div.attribute_values.keys[0]).to be_an_instance_of(String)
871
+ end
815
872
  end
816
- end
817
873
 
818
- describe '#attribute_list' do
819
- before { browser.goto WatirSpec.url_for('data_attributes.html') }
874
+ describe '#attribute_list' do
875
+ before { browser.goto WatirSpec.url_for('data_attributes.html') }
820
876
 
821
- it 'returns an Array object' do
822
- expect(browser.div.attribute_list).to be_an_instance_of(Array)
823
- end
877
+ it 'returns an Array object' do
878
+ expect(browser.div.attribute_list).to be_an_instance_of(Array)
879
+ end
824
880
 
825
- it 'returns list of attributes from an element' do
826
- expect(browser.p.attribute_list).to eq [:data_type]
827
- end
881
+ it 'returns list of attributes from an element' do
882
+ expect(browser.p.attribute_list).to eq [:data_type]
883
+ end
828
884
 
829
- it 'returns attribute name with special characters as a String' do
830
- expect(browser.div.attribute_list[0]).to be_an_instance_of(String)
885
+ it 'returns attribute name with special characters as a String' do
886
+ expect(browser.div.attribute_list[0]).to be_an_instance_of(String)
887
+ end
831
888
  end
832
- end
833
889
 
834
- describe '#located?' do
835
- it 'returns true if element has been located' do
836
- expect(browser.form(id: 'new_user')).to_not be_located
837
- end
890
+ describe '#located?' do
891
+ it 'returns true if element has been located' do
892
+ expect(browser.form(id: 'new_user')).not_to be_located
893
+ end
838
894
 
839
- it 'returns false if element has not been located' do
840
- expect(browser.form(id: 'new_user').locate).to be_located
895
+ it 'returns false if element has not been located' do
896
+ expect(browser.form(id: 'new_user').locate).to be_located
897
+ end
841
898
  end
842
- end
843
899
 
844
- describe '#wd' do
845
- it 'returns a Selenium::WebDriver::Element instance' do
846
- element = browser.text_field(id: 'new_user_email')
847
- expect(element.wd).to be_a(Selenium::WebDriver::Element)
900
+ describe '#wd' do
901
+ it 'returns a Selenium::WebDriver::Element instance' do
902
+ element = browser.text_field(id: 'new_user_email')
903
+ expect(element.wd).to be_a(Selenium::WebDriver::Element)
904
+ end
848
905
  end
849
- end
850
906
 
851
- describe '#hash' do
852
- it 'returns a hash' do
853
- element = browser.text_field(id: 'new_user_email')
854
- hash1 = element.hash
855
- hash2 = element.locate.hash
856
- expect(hash1).to be_a Integer
857
- expect(hash2).to be_a Integer
858
- expect(hash1).to_not eq hash2
907
+ describe '#hash' do
908
+ it 'returns a hash' do
909
+ element = browser.text_field(id: 'new_user_email')
910
+ hash1 = element.hash
911
+ hash2 = element.locate.hash
912
+ expect(hash1).to be_a Integer
913
+ expect(hash2).to be_a Integer
914
+ expect(hash1).not_to eq hash2
915
+ end
859
916
  end
860
- end
861
917
 
862
- describe 'Numeric Attribute' do
863
- it 'returns Float value',
864
- except: {browser: :ie, reason: 'not recognizing the value of the element'} do
865
- element = browser.text_field(id: 'number')
866
- expect(element.valueasnumber).to be_a Float
867
- end
918
+ describe 'Numeric Attribute' do
919
+ it 'returns Float value',
920
+ except: {browser: :ie, reason: 'not recognizing the value of the element'} do
921
+ element = browser.text_field(id: 'number')
922
+ expect(element.valueasnumber).to be_a Float
923
+ end
868
924
 
869
- it 'returns nil for unspecified value' do
870
- element = browser.input
871
- expect(element.valueasnumber).to be_nil
872
- end
925
+ it 'returns nil for unspecified value' do
926
+ element = browser.input
927
+ expect(element.valueasnumber).to be_nil
928
+ end
873
929
 
874
- it 'returns Integer value' do
875
- element = browser.form
876
- expect(element.length).to be_a Integer
930
+ it 'returns Integer value' do
931
+ element = browser.form
932
+ expect(element.length).to be_a Integer
933
+ end
877
934
  end
878
- end
879
935
 
880
- describe '#class_name' do
881
- it 'returns single class name' do
882
- expect(browser.form(id: 'new_user').class_name).to eq 'user'
883
- end
936
+ describe 'Special Characters' do
937
+ before do
938
+ browser.goto WatirSpec.url_for('special_chars.html')
939
+ end
884
940
 
885
- it 'returns multiple class names in a String' do
886
- expect(browser.div(id: 'messages').class_name).to eq 'multiple classes here'
887
- end
941
+ it 'finds elements with single quotes' do
942
+ expect(browser.div(text: "single 'quotes'")).to exist
943
+ end
888
944
 
889
- it 'returns an empty string if the element exists but there is no class attribute' do
890
- expect(browser.div(id: 'changed_language').class_name).to eq ''
891
- end
945
+ it 'finds elements with non-standard character locators' do
946
+ expect(browser.div('we{ird' => 'foo')).to exist
947
+ expect(browser.div('we{ird': 'foo')).to exist
948
+ end
892
949
 
893
- it 'raises UnknownObjectException if the element does not exist' do
894
- expect { browser.div(id: 'no_such_id').class_name }.to raise_unknown_object_exception
950
+ it 'finds element with underscored attribute' do
951
+ expect(browser.div('underscored_attribute' => 'true')).to exist
952
+ expect(browser.div('underscored_attribute' => true)).to exist
953
+ end
895
954
  end
896
- end
897
955
 
898
- describe '#classes' do
899
- it 'returns the class attribute if the element exists' do
900
- expect(browser.div(id: 'messages').classes).to eq %w[multiple classes here]
901
- end
956
+ describe '#class_name' do
957
+ it 'returns single class name' do
958
+ expect(browser.form(id: 'new_user').class_name).to eq 'user'
959
+ end
902
960
 
903
- it 'returns an empty array if the element exists but there is no class attribute' do
904
- expect(browser.div(id: 'changed_language').classes).to eq []
905
- end
961
+ it 'returns multiple class names in a String' do
962
+ expect(browser.div(id: 'messages').class_name).to eq 'multiple classes here'
963
+ end
906
964
 
907
- it 'raises UnknownObjectException if the element does not exist' do
908
- expect { browser.div(id: 'no_such_id').classes }.to raise_unknown_object_exception
965
+ it 'returns an empty string if the element exists but there is no class attribute' do
966
+ expect(browser.div(id: 'changed_language').class_name).to eq ''
967
+ end
968
+
969
+ it 'raises UnknownObjectException if the element does not exist' do
970
+ expect { browser.div(id: 'no_such_id').class_name }.to raise_unknown_object_exception
971
+ end
909
972
  end
910
- end
911
973
 
912
- describe '#obscured?' do
913
- before { browser.goto WatirSpec.url_for('obscured.html') }
974
+ describe '#classes' do
975
+ it 'returns the class attribute if the element exists' do
976
+ expect(browser.div(id: 'messages').classes).to eq %w[multiple classes here]
977
+ end
914
978
 
915
- it 'returns false if element center is not covered' do
916
- btn = browser.button(id: 'not_obscured')
917
- expect(btn).not_to be_obscured
918
- expect { btn.click }.not_to raise_exception
919
- end
979
+ it 'returns an empty array if the element exists but there is no class attribute' do
980
+ expect(browser.div(id: 'changed_language').classes).to eq []
981
+ end
920
982
 
921
- it 'returns false if element center is covered by its descendant' do
922
- btn = browser.button(id: 'has_descendant')
923
- expect(btn).not_to be_obscured
924
- expect { btn.click }.not_to raise_exception
983
+ it 'raises UnknownObjectException if the element does not exist' do
984
+ expect { browser.div(id: 'no_such_id').classes }.to raise_unknown_object_exception
985
+ end
925
986
  end
926
987
 
927
- it 'returns true if element center is covered by a non-descendant',
928
- except: {browser: :safari, reason: 'not getting element click intercepted'} do
929
- btn = browser.button(id: 'obscured')
930
- expect(btn).to be_obscured
988
+ describe '#obscured?' do
989
+ before { browser.goto WatirSpec.url_for('obscured.html') }
931
990
 
932
- expect { btn.click }.to raise_exception(Selenium::WebDriver::Error::ElementClickInterceptedError)
933
- end
991
+ it 'returns false if element center is not covered' do
992
+ btn = browser.button(id: 'not_obscured')
993
+ btn.scroll.to :center
994
+ expect { btn.click }.not_to raise_exception
995
+ expect(btn).not_to be_obscured
996
+ end
934
997
 
935
- it 'returns false if element center is surrounded by non-descendants' do
936
- btn = browser.button(id: 'surrounded')
937
- expect(btn).not_to be_obscured
938
- expect { btn.click }.not_to raise_exception
939
- end
998
+ it 'returns false if element center is covered by its descendant' do
999
+ btn = browser.button(id: 'has_descendant')
1000
+ btn.scroll.to :center
1001
+ expect { btn.click }.not_to raise_exception
1002
+ expect(btn).not_to be_obscured
1003
+ end
940
1004
 
941
- it 'scrolls interactive element into view before checking if obscured' do
942
- btn = browser.button(id: 'requires_scrolling')
943
- expect(btn).not_to be_obscured
944
- expect { btn.click }.not_to raise_exception
945
- end
1005
+ it 'returns true if element center is covered by a non-descendant',
1006
+ except: {browser: :safari, reason: 'not getting element click intercepted'} do
1007
+ btn = browser.button(id: 'obscured')
1008
+ btn.scroll.to :center
1009
+ expect { btn.click }.to raise_exception(Selenium::WebDriver::Error::ElementClickInterceptedError)
1010
+ expect(btn).to be_obscured
1011
+ end
946
1012
 
947
- it 'scrolls non-interactive element into view before checking if obscured' do
948
- div = browser.div(id: 'requires_scrolling_container')
949
- expect(div).not_to be_obscured
950
- expect { div.click }.not_to raise_exception
951
- end
1013
+ it 'returns false if element center is surrounded by non-descendants' do
1014
+ btn = browser.button(id: 'surrounded')
1015
+ btn.scroll.to :center
1016
+ expect { btn.click }.not_to raise_exception
1017
+ expect(btn).not_to be_obscured
1018
+ end
952
1019
 
953
- it 'returns true if element cannot be scrolled into view' do
954
- btn = browser.button(id: 'off_screen')
955
- expect(btn).to be_obscured
956
- expect { btn.click }.to raise_unknown_object_exception
957
- end
1020
+ it 'correctly scrolls element below viewport' do
1021
+ browser.goto WatirSpec.url_for('sticky_elements.html')
958
1022
 
959
- it 'returns true if element is hidden' do
960
- btn = browser.button(id: 'hidden')
961
- expect(btn).to be_obscured
962
- expect { btn.click }.to raise_unknown_object_exception
963
- end
1023
+ element = browser.div(id: 'center')
1024
+
1025
+ browser.scroll.to :top
1026
+ expect { element.click }.not_to raise_exception
1027
+
1028
+ browser.scroll.to :top
1029
+ expect(element).not_to be_obscured
1030
+ end
1031
+
1032
+ it 'correctly scrolls element above viewport',
1033
+ except: {browser: %i[chrome edge],
1034
+ reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3954'} do
1035
+ browser.goto WatirSpec.url_for('sticky_elements.html')
1036
+ element = browser.div(id: 'center')
964
1037
 
965
- it 'raises UnknownObjectException if element does not exist' do
966
- expect { browser.button(id: 'does_not_exist').obscured? }.to raise_unknown_object_exception
1038
+ browser.scroll.to :bottom
1039
+ expect { element.click }.not_to raise_exception
1040
+
1041
+ browser.scroll.to :bottom
1042
+ expect(element).not_to be_obscured
1043
+ end
1044
+
1045
+ it 'scrolls non-interactive element into view before checking if obscured' do
1046
+ div = browser.div(id: 'requires_scrolling_container')
1047
+ expect { div.click }.not_to raise_exception
1048
+ browser.scroll.to :top
1049
+ expect(div).not_to be_obscured
1050
+ end
1051
+
1052
+ it 'returns true if element cannot be scrolled into view' do
1053
+ btn = browser.button(id: 'off_screen')
1054
+ expect(btn).to be_obscured
1055
+ expect { btn.click }.to raise_unknown_object_exception
1056
+ end
1057
+
1058
+ it 'returns true if element is hidden' do
1059
+ btn = browser.button(id: 'hidden')
1060
+ expect(btn).to be_obscured
1061
+ expect { btn.click }.to raise_unknown_object_exception
1062
+ end
1063
+
1064
+ it 'raises UnknownObjectException if element does not exist' do
1065
+ expect { browser.button(id: 'does_not_exist').obscured? }.to raise_unknown_object_exception
1066
+ end
967
1067
  end
968
1068
  end
969
1069
  end