watir 6.0.0.beta5 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -2
  3. data/CHANGES.md +10 -1
  4. data/Gemfile +4 -0
  5. data/README.md +37 -23
  6. data/Rakefile +6 -10
  7. data/appveyor.yml +13 -0
  8. data/lib/watir.rb +20 -21
  9. data/lib/watir/alert.rb +21 -5
  10. data/lib/watir/browser.rb +9 -3
  11. data/lib/watir/elements/checkbox.rb +0 -1
  12. data/lib/watir/elements/element.rb +92 -50
  13. data/lib/watir/elements/form.rb +1 -2
  14. data/lib/watir/elements/iframe.rb +6 -2
  15. data/lib/watir/elements/image.rb +1 -1
  16. data/lib/watir/elements/option.rb +0 -1
  17. data/lib/watir/elements/radio.rb +0 -1
  18. data/lib/watir/elements/select.rb +1 -10
  19. data/lib/watir/extensions/select_text.rb +1 -1
  20. data/lib/watir/legacy_wait.rb +126 -0
  21. data/lib/watir/locators/text_field/locator.rb +0 -3
  22. data/lib/watir/locators/text_field/selector_builder.rb +1 -4
  23. data/lib/watir/locators/text_field/selector_builder/xpath.rb +0 -2
  24. data/lib/watir/locators/text_field/validator.rb +1 -9
  25. data/lib/watir/row_container.rb +1 -1
  26. data/lib/watir/user_editable.rb +7 -5
  27. data/lib/watir/wait.rb +56 -135
  28. data/lib/watir/wait/timer.rb +29 -2
  29. data/lib/watir/window.rb +11 -1
  30. data/lib/watirspec.rb +81 -0
  31. data/lib/watirspec/guards.rb +58 -0
  32. data/lib/watirspec/implementation.rb +33 -0
  33. data/lib/watirspec/rake_tasks.rb +115 -0
  34. data/lib/watirspec/runner.rb +53 -0
  35. data/lib/watirspec/server.rb +99 -0
  36. data/lib/watirspec/server/app.rb +62 -0
  37. data/spec/browser_spec.rb +5 -5
  38. data/spec/click_spec.rb +1 -1
  39. data/spec/container_spec.rb +1 -1
  40. data/spec/element_locator_spec.rb +1 -1
  41. data/spec/element_spec.rb +20 -34
  42. data/spec/implementation_spec.rb +24 -0
  43. data/spec/input_spec.rb +1 -1
  44. data/spec/spec_helper.rb +35 -3
  45. data/spec/special_chars_spec.rb +1 -1
  46. data/spec/watirspec/.gitignore +2 -0
  47. data/spec/watirspec/after_hooks_spec.rb +179 -0
  48. data/spec/watirspec/alert_spec.rb +101 -0
  49. data/spec/watirspec/browser_spec.rb +291 -0
  50. data/spec/watirspec/cookies_spec.rb +147 -0
  51. data/spec/watirspec/drag_and_drop_spec.rb +49 -0
  52. data/spec/watirspec/element_hidden_spec.rb +65 -0
  53. data/spec/watirspec/elements/area_spec.rb +76 -0
  54. data/spec/watirspec/elements/areas_spec.rb +42 -0
  55. data/spec/watirspec/elements/button_spec.rb +270 -0
  56. data/spec/watirspec/elements/buttons_spec.rb +55 -0
  57. data/spec/watirspec/elements/checkbox_spec.rb +279 -0
  58. data/spec/watirspec/elements/checkboxes_spec.rb +44 -0
  59. data/spec/watirspec/elements/collections_spec.rb +16 -0
  60. data/spec/watirspec/elements/dd_spec.rb +126 -0
  61. data/spec/watirspec/elements/dds_spec.rb +42 -0
  62. data/spec/watirspec/elements/del_spec.rb +128 -0
  63. data/spec/watirspec/elements/dels_spec.rb +40 -0
  64. data/spec/watirspec/elements/div_spec.rb +199 -0
  65. data/spec/watirspec/elements/divs_spec.rb +42 -0
  66. data/spec/watirspec/elements/dl_spec.rb +144 -0
  67. data/spec/watirspec/elements/dls_spec.rb +43 -0
  68. data/spec/watirspec/elements/dt_spec.rb +126 -0
  69. data/spec/watirspec/elements/dts_spec.rb +42 -0
  70. data/spec/watirspec/elements/element_spec.rb +361 -0
  71. data/spec/watirspec/elements/elements_spec.rb +15 -0
  72. data/spec/watirspec/elements/em_spec.rb +100 -0
  73. data/spec/watirspec/elements/ems_spec.rb +43 -0
  74. data/spec/watirspec/elements/filefield_spec.rb +181 -0
  75. data/spec/watirspec/elements/filefields_spec.rb +43 -0
  76. data/spec/watirspec/elements/font_spec.rb +29 -0
  77. data/spec/watirspec/elements/form_spec.rb +70 -0
  78. data/spec/watirspec/elements/forms_spec.rb +44 -0
  79. data/spec/watirspec/elements/frame_spec.rb +121 -0
  80. data/spec/watirspec/elements/frames_spec.rb +41 -0
  81. data/spec/watirspec/elements/hidden_spec.rb +102 -0
  82. data/spec/watirspec/elements/hiddens_spec.rb +43 -0
  83. data/spec/watirspec/elements/hn_spec.rb +96 -0
  84. data/spec/watirspec/elements/hns_spec.rb +38 -0
  85. data/spec/watirspec/elements/iframe_spec.rb +174 -0
  86. data/spec/watirspec/elements/iframes_spec.rb +47 -0
  87. data/spec/watirspec/elements/image_spec.rb +164 -0
  88. data/spec/watirspec/elements/images_spec.rb +40 -0
  89. data/spec/watirspec/elements/ins_spec.rb +129 -0
  90. data/spec/watirspec/elements/inses_spec.rb +40 -0
  91. data/spec/watirspec/elements/label_spec.rb +79 -0
  92. data/spec/watirspec/elements/labels_spec.rb +40 -0
  93. data/spec/watirspec/elements/li_spec.rb +115 -0
  94. data/spec/watirspec/elements/link_spec.rb +174 -0
  95. data/spec/watirspec/elements/links_spec.rb +44 -0
  96. data/spec/watirspec/elements/lis_spec.rb +42 -0
  97. data/spec/watirspec/elements/map_spec.rb +79 -0
  98. data/spec/watirspec/elements/maps_spec.rb +41 -0
  99. data/spec/watirspec/elements/meta_spec.rb +23 -0
  100. data/spec/watirspec/elements/metas_spec.rb +40 -0
  101. data/spec/watirspec/elements/ol_spec.rb +89 -0
  102. data/spec/watirspec/elements/ols_spec.rb +40 -0
  103. data/spec/watirspec/elements/option_spec.rb +152 -0
  104. data/spec/watirspec/elements/p_spec.rb +115 -0
  105. data/spec/watirspec/elements/pre_spec.rb +115 -0
  106. data/spec/watirspec/elements/pres_spec.rb +40 -0
  107. data/spec/watirspec/elements/ps_spec.rb +40 -0
  108. data/spec/watirspec/elements/radio_spec.rb +263 -0
  109. data/spec/watirspec/elements/radios_spec.rb +43 -0
  110. data/spec/watirspec/elements/select_list_spec.rb +379 -0
  111. data/spec/watirspec/elements/select_lists_spec.rb +46 -0
  112. data/spec/watirspec/elements/span_spec.rb +129 -0
  113. data/spec/watirspec/elements/spans_spec.rb +40 -0
  114. data/spec/watirspec/elements/strong_spec.rb +92 -0
  115. data/spec/watirspec/elements/strongs_spec.rb +43 -0
  116. data/spec/watirspec/elements/table_nesting_spec.rb +51 -0
  117. data/spec/watirspec/elements/table_spec.rb +146 -0
  118. data/spec/watirspec/elements/tables_spec.rb +42 -0
  119. data/spec/watirspec/elements/tbody_spec.rb +96 -0
  120. data/spec/watirspec/elements/tbodys_spec.rb +62 -0
  121. data/spec/watirspec/elements/td_spec.rb +71 -0
  122. data/spec/watirspec/elements/tds_spec.rb +53 -0
  123. data/spec/watirspec/elements/text_field_spec.rb +302 -0
  124. data/spec/watirspec/elements/text_fields_spec.rb +44 -0
  125. data/spec/watirspec/elements/textarea_spec.rb +26 -0
  126. data/spec/watirspec/elements/textareas_spec.rb +24 -0
  127. data/spec/watirspec/elements/tfoot_spec.rb +91 -0
  128. data/spec/watirspec/elements/tfoots_spec.rb +68 -0
  129. data/spec/watirspec/elements/thead_spec.rb +91 -0
  130. data/spec/watirspec/elements/theads_spec.rb +68 -0
  131. data/spec/watirspec/elements/tr_spec.rb +80 -0
  132. data/spec/watirspec/elements/trs_spec.rb +61 -0
  133. data/spec/watirspec/elements/ul_spec.rb +79 -0
  134. data/spec/watirspec/elements/uls_spec.rb +39 -0
  135. data/spec/watirspec/html/alerts.html +12 -0
  136. data/spec/watirspec/html/aria_attributes.html +9 -0
  137. data/spec/watirspec/html/class_locator.html +8 -0
  138. data/spec/watirspec/html/clicks.html +19 -0
  139. data/spec/watirspec/html/closeable.html +13 -0
  140. data/spec/watirspec/html/collections.html +15 -0
  141. data/spec/watirspec/html/css/jquery-ui-1.8.17.custom.css +287 -0
  142. data/spec/watirspec/html/data_attributes.html +9 -0
  143. data/spec/watirspec/html/definition_lists.html +48 -0
  144. data/spec/watirspec/html/drag_and_drop.html +106 -0
  145. data/spec/watirspec/html/font.html +10 -0
  146. data/spec/watirspec/html/forms_with_input_elements.html +170 -0
  147. data/spec/watirspec/html/frame_1.html +22 -0
  148. data/spec/watirspec/html/frame_2.html +16 -0
  149. data/spec/watirspec/html/frames.html +11 -0
  150. data/spec/watirspec/html/hover.html +12 -0
  151. data/spec/watirspec/html/iframe_1.html +22 -0
  152. data/spec/watirspec/html/iframes.html +12 -0
  153. data/spec/watirspec/html/images.html +28 -0
  154. data/spec/watirspec/html/images/1.gif +0 -0
  155. data/spec/watirspec/html/images/2.gif +0 -0
  156. data/spec/watirspec/html/images/3.gif +0 -0
  157. data/spec/watirspec/html/images/button.png +0 -0
  158. data/spec/watirspec/html/images/circle.png +0 -0
  159. data/spec/watirspec/html/images/map.gif +0 -0
  160. data/spec/watirspec/html/images/map2.gif +0 -0
  161. data/spec/watirspec/html/images/minus.gif +0 -0
  162. data/spec/watirspec/html/images/originaltriangle.png +0 -0
  163. data/spec/watirspec/html/images/plus.gif +0 -0
  164. data/spec/watirspec/html/images/square.png +0 -0
  165. data/spec/watirspec/html/images/triangle.png +0 -0
  166. data/spec/watirspec/html/inner_outer.html +5 -0
  167. data/spec/watirspec/html/javascript/helpers.js +16 -0
  168. data/spec/watirspec/html/javascript/jquery-1.7.1.min.js +4 -0
  169. data/spec/watirspec/html/javascript/jquery-ui-1.8.17.custom.min.js +68 -0
  170. data/spec/watirspec/html/keylogger.html +15 -0
  171. data/spec/watirspec/html/modal_dialog.html +9 -0
  172. data/spec/watirspec/html/multiple_ids.html +14 -0
  173. data/spec/watirspec/html/nested_frame_1.html +1 -0
  174. data/spec/watirspec/html/nested_frame_2.html +9 -0
  175. data/spec/watirspec/html/nested_frame_3.html +14 -0
  176. data/spec/watirspec/html/nested_frames.html +10 -0
  177. data/spec/watirspec/html/nested_iframe_2.html +12 -0
  178. data/spec/watirspec/html/nested_iframes.html +9 -0
  179. data/spec/watirspec/html/nested_tables.html +203 -0
  180. data/spec/watirspec/html/non_control_elements.html +135 -0
  181. data/spec/watirspec/html/removed_element.html +24 -0
  182. data/spec/watirspec/html/right_click.html +11 -0
  183. data/spec/watirspec/html/special_chars.html +12 -0
  184. data/spec/watirspec/html/tables.html +121 -0
  185. data/spec/watirspec/html/timeout_window_location.html +19 -0
  186. data/spec/watirspec/html/uneven_table.html +20 -0
  187. data/spec/watirspec/html/wait.html +65 -0
  188. data/spec/watirspec/html/watirspec.css +0 -0
  189. data/spec/watirspec/html/window_switching.html +12 -0
  190. data/spec/watirspec/relaxed_locate_spec.rb +214 -0
  191. data/spec/watirspec/screenshot_spec.rb +29 -0
  192. data/spec/watirspec/wait_spec.rb +304 -0
  193. data/spec/watirspec/window_switching_spec.rb +411 -0
  194. data/spec/{implementation.rb → watirspec_helper.rb} +13 -13
  195. data/support/appveyor.cmd +9 -0
  196. data/support/doctest_helper.rb +5 -1
  197. data/support/travis.sh +0 -1
  198. data/watir.gemspec +3 -4
  199. metadata +323 -30
  200. data/.gitmodules +0 -3
@@ -0,0 +1,29 @@
1
+ require "base64"
2
+ require "watirspec_helper"
3
+
4
+ describe "Watir::Screenshot" do
5
+ let(:png_header) { "\211PNG".force_encoding('ASCII-8BIT') }
6
+
7
+ describe '#png' do
8
+ it 'gets png representation of screenshot' do
9
+ expect(browser.screenshot.png[0..3]).to eq png_header
10
+ end
11
+ end
12
+
13
+ describe '#base64' do
14
+ it 'gets base64 representation of screenshot' do
15
+ image = browser.screenshot.base64
16
+ expect(Base64.decode64(image)[0..3]).to eq png_header
17
+ end
18
+ end
19
+
20
+ describe '#save' do
21
+ it 'saves screenshot to given file' do
22
+ path = "#{Dir.tmpdir}/test#{Time.now.to_i}.png"
23
+ expect(File).to_not exist(path)
24
+ browser.screenshot.save(path)
25
+ expect(File).to exist(path)
26
+ expect(File.open(path, "rb") { |io| io.read }[0..3]).to eq png_header
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,304 @@
1
+ require "watirspec_helper"
2
+
3
+ not_compliant_on :safari do
4
+ describe Watir::Wait do
5
+ describe "#until" do
6
+ it "waits until the block returns true" do
7
+ Watir::Wait.until(timeout: 0.5) { @result = true }
8
+ expect(@result).to be true
9
+ end
10
+
11
+ it "executes block if timeout is zero" do
12
+ Watir::Wait.until(timeout: 0) { @result = true }
13
+ expect(@result).to be true
14
+ end
15
+
16
+ it "times out" do
17
+ expect { Watir::Wait.until(timeout: 0.5) { false } }.to raise_error(Watir::Wait::TimeoutError)
18
+ end
19
+
20
+ it "times out with a custom message" do
21
+ expect {
22
+ Watir::Wait.until(timeout: 0.5, message: "oops") { false }
23
+ }.to raise_error(Watir::Wait::TimeoutError, "timed out after 0.5 seconds, oops")
24
+ end
25
+
26
+ it "uses timer for waiting" do
27
+ timer = Watir::Wait.timer
28
+ expect(timer).to receive(:wait).with(0.5).and_call_original
29
+ Watir::Wait.until(timeout: 0.5) { true }
30
+ end
31
+
32
+ it "ordered pairs are deprecated" do
33
+ message = /Instead of passing arguments into Wait#until method, use keywords/
34
+ expect { Watir::Wait.until(0) { true } }.to output(message).to_stderr
35
+ end
36
+ end
37
+
38
+ describe "#while" do
39
+ it "waits while the block returns true" do
40
+ expect(Watir::Wait.while(timeout: 0.5) { false }).to be_nil
41
+ end
42
+
43
+ it "executes block if timeout is zero" do
44
+ expect(Watir::Wait.while(timeout: 0) { false }).to be_nil
45
+ end
46
+
47
+ it "times out" do
48
+ expect { Watir::Wait.while(timeout: 0.5) { true } }.to raise_error(Watir::Wait::TimeoutError)
49
+ end
50
+
51
+ it "times out with a custom message" do
52
+ expect {
53
+ Watir::Wait.while(timeout: 0.5, message: "oops") { true }
54
+ }.to raise_error(Watir::Wait::TimeoutError, "timed out after 0.5 seconds, oops")
55
+ end
56
+
57
+ it "uses timer for waiting" do
58
+ timer = Watir::Wait.timer
59
+ expect(timer).to receive(:wait).with(0.5).and_call_original
60
+ Watir::Wait.while(timeout: 0.5) { false }
61
+ end
62
+
63
+ it "ordered pairs are deprecated" do
64
+ message = /Instead of passing arguments into Wait#while method, use keywords/
65
+ expect { Watir::Wait.while(0) { false } }.to output(message).to_stderr
66
+ end
67
+ end
68
+
69
+ describe "#timer" do
70
+ it "returns default timer" do
71
+ expect(Watir::Wait.timer).to be_a(Watir::Wait::Timer)
72
+ end
73
+ end
74
+
75
+ describe "#timer=" do
76
+ after { Watir::Wait.timer = nil }
77
+
78
+ it "changes default timer" do
79
+ timer = Class.new
80
+ Watir::Wait.timer = timer
81
+ expect(Watir::Wait.timer).to eq(timer)
82
+ end
83
+ end
84
+ end
85
+
86
+ describe Watir::Element do
87
+ before do
88
+ browser.goto WatirSpec.url_for("wait.html")
89
+ end
90
+
91
+ # TODO: This is deprecated; remove in future version
92
+ not_compliant_on :relaxed_locate do
93
+ describe "#when_present" do
94
+ it "invokes subsequent method calls when the element becomes present" do
95
+ browser.a(id: 'show_bar').click
96
+
97
+ bar = browser.div(id: 'bar')
98
+ bar.when_present(2).click
99
+ expect(bar.text).to eq "changed"
100
+ end
101
+
102
+ it "times out when given a block" do
103
+ expect { browser.div(id: 'bar').when_present(1) {} }.to raise_error(Watir::Wait::TimeoutError)
104
+ end
105
+
106
+ it "times out when not given a block" do
107
+ message = /^timed out after 1 seconds, waiting for (\{:id=>"bar", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"bar"\}) to become present$/
108
+ expect { browser.div(id: 'bar').when_present(1).click }.to raise_error(Watir::Wait::TimeoutError, message)
109
+ end
110
+
111
+ it "responds to Element methods" do
112
+ decorator = browser.div.when_present
113
+
114
+ expect(decorator).to respond_to(:exist?)
115
+ expect(decorator).to respond_to(:present?)
116
+ expect(decorator).to respond_to(:click)
117
+ end
118
+
119
+ it "delegates present? to element" do
120
+ Object.class_eval do
121
+ def present?
122
+ false
123
+ end
124
+ end
125
+ element = browser.a(id: "show_bar").when_present(1)
126
+ expect(element).to be_present
127
+ end
128
+
129
+ it "processes before calling present?" do
130
+ browser.a(id: 'show_bar').click
131
+ expect(browser.div(id: 'bar').when_present.present?).to be true
132
+ end
133
+ end
134
+ end
135
+
136
+ not_compliant_on :relaxed_locate do
137
+ describe "#wait_until &:enabled?" do
138
+ it "invokes subsequent method calls when the element becomes enabled" do
139
+ browser.a(id: 'enable_btn').click
140
+
141
+ btn = browser.button(id: 'btn')
142
+ btn.wait_until(timeout: 2, &:enabled?).click
143
+ Watir::Wait.while { btn.enabled? }
144
+ expect(btn.disabled?).to be true
145
+ end
146
+
147
+ it "times out" do
148
+ message = /^timed out after 1 seconds, waiting for true condition on (\{:id=>"btn", :tag_name=>"button"\}|\{:tag_name=>"button", :id=>"btn"\})$/
149
+ expect { browser.button(id: 'btn').wait_until(timeout: 1, &:enabled?).click }.to raise_error(Watir::Wait::TimeoutError, message)
150
+ end
151
+
152
+ it "responds to Element methods" do
153
+ element = browser.button.wait_until { true }
154
+
155
+ expect(element).to respond_to(:exist?)
156
+ expect(element).to respond_to(:present?)
157
+ expect(element).to respond_to(:click)
158
+ end
159
+
160
+ it "can be chained with #wait_until &:present?" do
161
+ browser.a(id: 'show_and_enable_btn').click
162
+ browser.button(id: 'btn2').wait_until(&:present?).wait_until(&:enabled?).click
163
+
164
+ expect(browser.button(id: 'btn2')).to exist
165
+ expect(browser.button(id: 'btn2')).to be_enabled
166
+ end
167
+ end
168
+ end
169
+
170
+ describe "#wait_until_present" do
171
+ it "it waits until the element appears" do
172
+ browser.a(id: 'show_bar').click
173
+ expect { browser.div(id: 'bar').wait_until_present(timeout: 5) }.to_not raise_exception
174
+ end
175
+
176
+ it "times out if the element doesn't appear" do
177
+ message = /^timed out after 1 seconds, waiting for true condition on (\{:id=>"bar", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"bar"\})$/
178
+ expect { browser.div(id: 'bar').wait_until_present(timeout: 1) }.to raise_error(Watir::Wait::TimeoutError, message)
179
+ end
180
+
181
+ it "ordered pairs are deprecated" do
182
+ browser.a(id: 'show_bar').click
183
+ message = /Instead of passing arguments into #wait_until_present method, use keywords/
184
+ expect { browser.div(id: 'bar').wait_until_present(5) }.to output(message).to_stderr
185
+ end
186
+ end
187
+
188
+ describe "#wait_while_present" do
189
+ it "waits until the element disappears" do
190
+ browser.a(id: 'hide_foo').click
191
+ expect { browser.div(id: 'foo').wait_while_present(timeout: 1) }.to_not raise_exception
192
+ end
193
+
194
+ it "times out if the element doesn't disappear" do
195
+ message = /^timed out after 1 seconds, waiting for false condition on (\{:id=>"foo", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"foo"\})$/
196
+ expect { browser.div(id: 'foo').wait_while_present(timeout: 1) }.to raise_error(Watir::Wait::TimeoutError, message)
197
+ end
198
+
199
+ it "ordered pairs are deprecated" do
200
+ browser.a(id: 'hide_foo').click
201
+ message = /Instead of passing arguments into #wait_while_present method, use keywords/
202
+ expect { browser.div(id: 'foo').wait_while_present(5) }.to output(message).to_stderr
203
+ end
204
+ end
205
+
206
+ describe "#wait_until" do
207
+ it "returns element for additional actions" do
208
+ element = browser.div(id: 'foo')
209
+ expect(element.wait_until(&:exist?)).to eq element
210
+ end
211
+
212
+ it "accepts self in block" do
213
+ element = browser.div(id: 'bar')
214
+ browser.a(id: 'show_bar').click
215
+ expect { element.wait_until { |el| el.text == 'bar' } }.to_not raise_exception
216
+ end
217
+
218
+ it "accepts any values in block" do
219
+ element = browser.div(id: 'bar')
220
+ expect { element.wait_until { true } }.to_not raise_exception
221
+ end
222
+
223
+ it "accepts just a timeout parameter" do
224
+ element = browser.div(id: 'bar')
225
+ expect { element.wait_until(timeout: 0) { true } }.to_not raise_exception
226
+ end
227
+
228
+ it "accepts just a message parameter" do
229
+ element = browser.div(id: 'bar')
230
+ expect { element.wait_until(message: 'no') { true } }.to_not raise_exception
231
+ end
232
+ end
233
+
234
+ describe "#wait_while" do
235
+ it "returns element for additional actions" do
236
+ element = browser.div(id: 'foo')
237
+ browser.a(id: 'hide_foo').click
238
+ expect(element.wait_while(&:present?)).to eq element
239
+ end
240
+
241
+ it "accepts self in block" do
242
+ element = browser.div(id: 'foo')
243
+ browser.a(id: 'hide_foo').click
244
+ expect { element.wait_while { |el| el.text == 'foo' } }.to_not raise_exception
245
+ end
246
+
247
+ it "accepts any values in block" do
248
+ element = browser.div(id: 'foo')
249
+ expect { element.wait_while { false } }.to_not raise_exception
250
+ end
251
+
252
+ it "accepts just a timeout parameter" do
253
+ element = browser.div(id: 'foo')
254
+ expect { element.wait_while(timeout: 0) { false } }.to_not raise_exception
255
+ end
256
+
257
+ it "accepts just a message parameter" do
258
+ element = browser.div(id: 'foo')
259
+ expect { element.wait_while(message: 'no') { false } }.to_not raise_exception
260
+ end
261
+ end
262
+ end
263
+
264
+ describe Watir do
265
+ describe "#default_timeout" do
266
+ before do
267
+ Watir.default_timeout = 1
268
+
269
+ browser.goto WatirSpec.url_for("wait.html")
270
+ end
271
+
272
+ after do
273
+ # Reset the default timeout
274
+ Watir.default_timeout = 30
275
+ end
276
+
277
+ context "when no timeout is specified" do
278
+ it "is used by Wait#until" do
279
+ expect {
280
+ Watir::Wait.until { false }
281
+ }.to raise_error(Watir::Wait::TimeoutError)
282
+ end
283
+
284
+ it "is used by Wait#while" do
285
+ expect {
286
+ Watir::Wait.while { true }
287
+ }.to raise_error(Watir::Wait::TimeoutError)
288
+ end
289
+
290
+ it "is used by Element#wait_until_present" do
291
+ expect {
292
+ browser.div(id: 'bar').wait_until_present
293
+ }.to raise_error(Watir::Wait::TimeoutError)
294
+ end
295
+
296
+ it "is used by Element#wait_while_present" do
297
+ expect {
298
+ browser.div(id: 'foo').wait_while_present
299
+ }.to raise_error(Watir::Wait::TimeoutError)
300
+ end
301
+ end
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,411 @@
1
+ require "watirspec_helper"
2
+
3
+ not_compliant_on :safari do
4
+ describe "Browser" do
5
+ before do
6
+ url = WatirSpec.url_for("window_switching.html")
7
+ browser.goto url
8
+ browser.a(id: "open").click
9
+ Watir::Wait.until { browser.windows.size == 2 }
10
+ end
11
+
12
+ after do
13
+ browser.window(index: 0).use
14
+ browser.windows[1..-1].each(&:close)
15
+ end
16
+
17
+ describe "#windows" do
18
+ it "returns an array of window handles" do
19
+ wins = browser.windows
20
+ expect(wins).to_not be_empty
21
+ wins.each { |win| expect(win).to be_kind_of(Watir::Window) }
22
+ end
23
+
24
+ it "only returns windows matching the given selector" do
25
+ expect(browser.windows(title: "closeable window").size).to eq 1
26
+ end
27
+
28
+ it "raises ArgumentError if the selector is invalid" do
29
+ expect { browser.windows(name: "foo") }.to raise_error(ArgumentError)
30
+ end
31
+
32
+ it "returns an empty array if no window matches the selector" do
33
+ expect(browser.windows(title: "noop")).to eq []
34
+ end
35
+ end
36
+
37
+ describe "#window" do
38
+ it "finds window by :url" do
39
+ w = browser.window(url: /closeable\.html/).use
40
+ expect(w).to be_kind_of(Watir::Window)
41
+ end
42
+
43
+ it "finds window by :title" do
44
+ w = browser.window(title: "closeable window").use
45
+ expect(w).to be_kind_of(Watir::Window)
46
+ end
47
+
48
+ it "finds window by :index" do
49
+ w = browser.window(index: 1).use
50
+ expect(w).to be_kind_of(Watir::Window)
51
+ end
52
+
53
+ it "should not find incorrect handle" do
54
+ expect(browser.window(handle: 'bar')).to_not be_present
55
+ end
56
+
57
+ it "returns the current window if no argument is given" do
58
+ expect(browser.window.url).to match(/window_switching\.html/)
59
+ end
60
+
61
+ it "stores the reference to a window when no argument is given" do
62
+ original_window = browser.window
63
+ browser.window(index: 1).use
64
+ expect(original_window.url).to match(/window_switching\.html/)
65
+ end
66
+
67
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1223277", :firefox do
68
+ it "it executes the given block in the window" do
69
+ browser.window(title: "closeable window") do
70
+ link = browser.a(id: "close")
71
+ expect(link).to exist
72
+ link.click
73
+ end.wait_while_present
74
+
75
+ expect(browser.windows.size).to eq 1
76
+ end
77
+ end
78
+
79
+ it "raises ArgumentError if the selector is invalid" do
80
+ expect { browser.window(name: "foo") }.to raise_error(ArgumentError)
81
+ end
82
+
83
+ it "raises a NoMatchingWindowFoundException error if no window matches the selector" do
84
+ expect { browser.window(title: "noop").use }.to raise_no_matching_window_exception
85
+ end
86
+
87
+ it "raises a NoMatchingWindowFoundException error if there's no window at the given index" do
88
+ expect { browser.window(index: 100).use }.to raise_no_matching_window_exception
89
+ end
90
+
91
+ it "raises NoMatchingWindowFoundException error when attempting to use a window with an incorrect handle" do
92
+ expect { browser.window(handle: 'bar').use }.to raise_no_matching_window_exception
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "Window" do
98
+ context 'multiple windows' do
99
+ before do
100
+ browser.goto WatirSpec.url_for("window_switching.html")
101
+ browser.a(id: "open").click
102
+ Watir::Wait.until { browser.windows.size == 2 }
103
+ end
104
+
105
+ after do
106
+ browser.window(index: 0).use
107
+ browser.windows[1..-1].each(&:close)
108
+ end
109
+
110
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1280517", :firefox do
111
+ describe "#close" do
112
+ it "closes a window" do
113
+ browser.a(id: "open").click
114
+ Watir::Wait.until { browser.windows.size == 3 }
115
+
116
+ browser.window(title: "closeable window").close
117
+ expect(browser.windows.size).to eq 2
118
+ end
119
+
120
+ it "closes the current window" do
121
+ browser.a(id: "open").click
122
+ Watir::Wait.until { browser.windows.size == 3 }
123
+
124
+ window = browser.window(title: "closeable window").use
125
+ window.close
126
+ expect(browser.windows.size).to eq 2
127
+ end
128
+ end
129
+ end
130
+
131
+ describe "#use" do
132
+ it "switches to the window" do
133
+ browser.window(title: "closeable window").use
134
+ expect(browser.title).to eq "closeable window"
135
+ end
136
+ end
137
+
138
+ describe "#current?" do
139
+ it "returns true if it is the current window" do
140
+ expect(browser.window(title: browser.title)).to be_current
141
+ end
142
+
143
+ it "returns false if it is not the current window" do
144
+ expect(browser.window(title: "closeable window")).to_not be_current
145
+ end
146
+ end
147
+
148
+ describe "#title" do
149
+ it "returns the title of the window" do
150
+ titles = browser.windows.map(&:title)
151
+ expect(titles.size).to eq 2
152
+
153
+ expect(titles.sort).to eq ["window switching", "closeable window"].sort
154
+ end
155
+
156
+ it "does not change the current window" do
157
+ expect(browser.title).to eq "window switching"
158
+ expect(browser.windows.find { |w| w.title == "closeable window" }).to_not be_nil
159
+ expect(browser.title).to eq "window switching"
160
+ end
161
+ end
162
+
163
+ describe "#url" do
164
+ it "returns the url of the window" do
165
+ expect(browser.windows.select { |w| w.url =~ (/window_switching\.html/) }.size).to eq 1
166
+ expect(browser.windows.select { |w| w.url =~ (/closeable\.html$/) }.size).to eq 1
167
+ end
168
+
169
+ it "does not change the current window" do
170
+ expect(browser.url).to match(/window_switching\.html/)
171
+ expect(browser.windows.find { |w| w.url =~ (/closeable\.html/) }).to_not be_nil
172
+ expect(browser.url).to match(/window_switching/)
173
+ end
174
+ end
175
+
176
+ describe "#eql?" do
177
+ it "knows when two windows are equal" do
178
+ expect(browser.window).to eq browser.window(index: 0)
179
+ end
180
+
181
+ it "knows when two windows are not equal" do
182
+ win1 = browser.window(index: 0)
183
+ win2 = browser.window(index: 1)
184
+
185
+ expect(win1).to_not eq win2
186
+ end
187
+ end
188
+
189
+ not_compliant_on :relaxed_locate do
190
+ describe "#wait_until &:present?" do
191
+ it "times out waiting for a non-present window" do
192
+ expect {
193
+ browser.window(title: "noop").wait_until(timeout: 0.5, &:present?)
194
+ }.to raise_error(Watir::Wait::TimeoutError)
195
+ end
196
+ end
197
+ end
198
+ end
199
+
200
+ context "with a closed window" do
201
+ before do
202
+ browser.goto WatirSpec.url_for("window_switching.html")
203
+ browser.a(id: "open").click
204
+ Watir::Wait.until { browser.windows.size == 2 }
205
+ end
206
+
207
+ after do
208
+ browser.window(index: 0).use
209
+ browser.windows[1..-1].each(&:close)
210
+ end
211
+
212
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1223277", :firefox do
213
+ describe "#exists?" do
214
+ it "returns false if previously referenced window is closed" do
215
+ window = browser.window(title: "closeable window")
216
+ window.use
217
+ browser.a(id: "close").click
218
+ Watir::Wait.until { browser.windows.size == 1 }
219
+ expect(window).to_not be_present
220
+ end
221
+
222
+ it "returns false if closed window is referenced" do
223
+ browser.window(title: "closeable window").use
224
+ browser.a(id: "close").click
225
+ Watir::Wait.until { browser.windows.size == 1 }
226
+ expect(browser.window).to_not be_present
227
+ end
228
+ end
229
+ end
230
+
231
+ describe "#current?" do
232
+ it "returns false if the referenced window is closed" do
233
+ original_window = browser.window
234
+ browser.window(title: "closeable window").use
235
+ original_window.close
236
+ expect(original_window).to_not be_current
237
+ end
238
+ end
239
+
240
+ describe "#eql?" do
241
+ it "should return false when checking equivalence to a closed window" do
242
+ original_window = browser.window
243
+ other_window = browser.window(index: 1)
244
+ other_window.use
245
+ original_window.close
246
+ expect(other_window == original_window).to be false
247
+ end
248
+ end
249
+
250
+ describe "#use" do
251
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1223277", :firefox do
252
+ it "raises NoMatchingWindowFoundException error when attempting to use a referenced window that is closed" do
253
+ original_window = browser.window
254
+ browser.window(index: 1).use
255
+ original_window.close
256
+ expect { original_window.use }.to raise_no_matching_window_exception
257
+ end
258
+
259
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1223277", :firefox do
260
+ it "raises NoMatchingWindowFoundException error when attempting to use the current window if it is closed" do
261
+ browser.window(title: "closeable window").use
262
+ browser.a(id: "close").click
263
+ Watir::Wait.until { browser.windows.size == 1 }
264
+ expect { browser.window.use }.to raise_no_matching_window_exception
265
+ end
266
+ end
267
+ end
268
+ end
269
+ end
270
+
271
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1223277", :firefox do
272
+ context "with current window closed" do
273
+ before do
274
+ browser.goto WatirSpec.url_for("window_switching.html")
275
+ browser.a(id: "open").click
276
+ Watir::Wait.until { browser.windows.size == 2 }
277
+ browser.window(title: "closeable window").use
278
+ browser.a(id: "close").click
279
+ Watir::Wait.until { browser.windows.size == 1 }
280
+ end
281
+
282
+ after do
283
+ browser.window(index: 0).use
284
+ browser.windows[1..-1].each(&:close)
285
+ end
286
+
287
+ describe "#present?" do
288
+ it "should find window by index" do
289
+ expect(browser.window(index: 0)).to be_present
290
+ end
291
+
292
+ it "should find window by url" do
293
+ expect(browser.window(url: /window_switching\.html/)).to be_present
294
+ end
295
+
296
+ it "should find window by title" do
297
+ expect(browser.window(title: "window switching")).to be_present
298
+ end
299
+ end
300
+
301
+ describe "#use" do
302
+
303
+ context "switching windows without blocks" do
304
+ it "by index" do
305
+ browser.window(index: 0).use
306
+ expect(browser.title).to be == "window switching"
307
+ end
308
+
309
+ it "by url" do
310
+ browser.window(url: /window_switching\.html/).use
311
+ expect(browser.title).to be == "window switching"
312
+ end
313
+
314
+ it "by title" do
315
+ browser.window(title: "window switching").use
316
+ expect(browser.url).to match(/window_switching\.html/)
317
+ end
318
+ end
319
+
320
+ context "Switching windows with blocks" do
321
+ it "by index" do
322
+ browser.window(index: 0).use { expect(browser.title).to be == "window switching" }
323
+ end
324
+
325
+ it "by url" do
326
+ browser.window(url: /window_switching\.html/).use { expect(browser.title).to be == "window switching" }
327
+ end
328
+
329
+ it "by title" do
330
+ browser.window(title: "window switching").use { expect(browser.url).to match(/window_switching\.html/) }
331
+ end
332
+ end
333
+
334
+ end
335
+ end
336
+ end
337
+
338
+ context "manipulating size and position" do
339
+ before do
340
+ browser.goto WatirSpec.url_for("window_switching.html")
341
+ end
342
+
343
+ compliant_on :ff_legacy, :chrome do
344
+ it "should get the size of the current window" do
345
+ size = browser.window.size
346
+
347
+ expect(size.width).to be > 0
348
+ expect(size.height).to be > 0
349
+ end
350
+
351
+ it "should get the position of the current window" do
352
+ pos = browser.window.position
353
+
354
+ expect(pos.x).to be >= 0
355
+ expect(pos.y).to be >= 0
356
+ end
357
+ end
358
+
359
+ bug "https://github.com/SeleniumHQ/selenium/issues/2856", %i(remote firefox) do
360
+ it "should resize the window" do
361
+ initial_size = browser.window.size
362
+ browser.window.resize_to(
363
+ initial_size.width - 20,
364
+ initial_size.height - 20
365
+ )
366
+
367
+ new_size = browser.window.size
368
+
369
+ expect(new_size.width).to eq initial_size.width - 20
370
+ expect(new_size.height).to eq initial_size.height - 20
371
+ end
372
+ end
373
+
374
+ not_compliant_on :firefox do
375
+ bug "https://github.com/detro/ghostdriver/issues/466", :phantomjs do
376
+ it "should move the window" do
377
+ initial_pos = browser.window.position
378
+
379
+ browser.window.move_to(
380
+ initial_pos.x + 2,
381
+ initial_pos.y + 2
382
+ )
383
+
384
+ new_pos = browser.window.position
385
+ expect(new_pos.x).to eq initial_pos.x + 2
386
+ expect(new_pos.y).to eq initial_pos.y + 2
387
+ end
388
+ end
389
+ end
390
+
391
+ compliant_on :window_manager do
392
+ bug "https://github.com/SeleniumHQ/selenium/issues/2856", %i(remote firefox) do
393
+ it "should maximize the window" do
394
+ initial_size = browser.window.size
395
+ browser.window.resize_to(
396
+ initial_size.width,
397
+ initial_size.height - 20
398
+ )
399
+
400
+ browser.window.maximize
401
+ browser.wait_until { browser.window.size != initial_size }
402
+
403
+ new_size = browser.window.size
404
+ expect(new_size.width).to be >= initial_size.width
405
+ expect(new_size.height).to be > (initial_size.height - 20)
406
+ end
407
+ end
408
+ end
409
+ end
410
+ end
411
+ end