This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
wait_spec.rb
279 lines (224 loc) · 8.45 KB
/
wait_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
not_compliant_on :safari do
describe Watir::Wait do
describe "#until" do
it "returns result of block if truthy" do
result = 'catter'
expect(Wait.until(0.5) { result }).to be result
end
it "waits until the block returns true" do
expect(Wait.until(0.5) { true }).to be true
end
it "executes block if timeout is zero" do
expect(Wait.until(0) { true }).to be true
end
it "times out" do
expect {Wait.until(0.5) { false }}.to raise_error(Watir::Wait::TimeoutError)
end
it "times out with a custom message" do
expect {
Wait.until(0.5, "oops") { false }
}.to raise_error(Watir::Wait::TimeoutError, "timed out after 0.5 seconds, oops")
end
it "uses timer for waiting" do
timer = Wait.timer
expect(timer).to receive(:wait).with(0.5).and_call_original
Wait.until(0.5) { true }
end
end
describe "#while" do
it "waits while the block returns true" do
expect(Wait.while(0.5) { false }).to be_nil
end
it "executes block if timeout is zero" do
expect(Wait.while(0) { false }).to be_nil
end
it "times out" do
expect {Wait.while(0.5) { true }}.to raise_error(Watir::Wait::TimeoutError)
end
it "times out with a custom message" do
expect {
Wait.while(0.5, "oops") { true }
}.to raise_error(Watir::Wait::TimeoutError, "timed out after 0.5 seconds, oops")
end
it "uses timer for waiting" do
timer = Wait.timer
expect(timer).to receive(:wait).with(0.5).and_call_original
Wait.while(0.5) { false }
end
end
describe "#timer" do
it "returns default timer" do
expect(Wait.timer).to be_a(Wait::Timer)
end
end
describe "#timer=" do
after { Wait.timer = nil }
it "changes default timer" do
timer = Class.new
Wait.timer = timer
expect(Wait.timer).to eq(timer)
end
end
end
describe Watir::Element do
before do
browser.goto WatirSpec.url_for("wait.html")
end
describe "#when_present" do
it "yields when the element becomes present" do
called = false
browser.a(id: 'show_bar').click
browser.div(id: 'bar').when_present(2) { called = true }
expect(called).to be true
end
it "invokes subsequent method calls when the element becomes present" do
browser.a(id: 'show_bar').click
bar = browser.div(id: 'bar')
bar.when_present(2).click
expect(bar.text).to eq "changed"
end
it "times out when given a block" do
expect { browser.div(id: 'bar').when_present(1) {}}.to raise_error(Watir::Wait::TimeoutError)
end
it "times out when not given a block" do
expect { browser.div(id: 'bar').when_present(1).click }.to raise_error(Watir::Wait::TimeoutError,
/^timed out after 1 seconds, waiting for (\{:id=>"bar", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"bar"\}) to become present$/
)
end
it "responds to Element methods" do
decorator = browser.div.when_present
expect(decorator).to respond_to(:exist?)
expect(decorator).to respond_to(:present?)
expect(decorator).to respond_to(:click)
end
it "delegates present? to element" do
Object.class_eval do
def present?
false
end
end
element = browser.a(id: "show_bar").when_present(1)
expect(element).to be_present
end
it "processes before calling present?" do
browser.a(id: 'show_bar').click
expect(browser.div(id: 'bar').when_present.present?).to be true
end
end
describe "#when_enabled" do
it "yields when the element becomes enabled" do
called = false
browser.a(id: 'enable_btn').click
browser.button(id: 'btn').when_enabled(2) { called = true }
expect(called).to be true
end
it "invokes subsequent method calls when the element becomes enabled" do
browser.a(id: 'enable_btn').click
btn = browser.button(id: 'btn')
btn.when_enabled(2).click
expect(btn.disabled?).to be true
end
it "times out when given a block" do
expect { browser.button(id: 'btn').when_enabled(1) {}}.to raise_error(Watir::Wait::TimeoutError)
end
it "times out when not given a block" do
expect { browser.button(id: 'btn').when_enabled(1).click }.to raise_error(Watir::Wait::TimeoutError,
/^timed out after 1 seconds, waiting for (\{:id=>"btn", :tag_name=>"button"\}|\{:tag_name=>"button", :id=>"btn"\}) to become enabled$/
)
end
it "times out when not given a block" do
expect { browser.button(id: 'btn').when_enabled(1).click }.to raise_error(Watir::Wait::TimeoutError,
/timed out after 1 seconds, waiting for {:id=>"btn", :tag_name=>"button"} to become enabled$/
)
end
it "responds to Element methods" do
decorator = browser.button.when_enabled
expect(decorator).to respond_to(:exist?)
expect(decorator).to respond_to(:present?)
expect(decorator).to respond_to(:click)
end
it "can be chained with #when_present" do
browser.a(id: 'show_and_enable_btn').click
browser.button(id: 'btn2').when_present(1).when_enabled(1).click
expect(browser.button(id: 'btn2')).to exist
expect(browser.button(id: 'btn2')).to be_enabled
end
end
describe "#wait_until_present" do
it "it waits until the element appears" do
browser.a(id: 'show_bar').click
expect { browser.div(id: 'bar').wait_until_present(5) }.to_not raise_exception
end
it "times out if the element doesn't appear" do
expect { browser.div(id: 'bar').wait_until_present(1) }.to raise_error(Watir::Wait::TimeoutError,
/^timed out after 1 seconds, waiting for (\{:id=>"bar", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"bar"\}) to become present$/
)
end
end
describe "#wait_while_present" do
it "waits until the element disappears" do
browser.a(id: 'hide_foo').click
expect { browser.div(id: 'foo').wait_while_present(1) }.to_not raise_exception
end
it "times out if the element doesn't disappear" do
expect { browser.div(id: 'foo').wait_while_present(1) }.to raise_error(Watir::Wait::TimeoutError,
/^timed out after 1 seconds, waiting for (\{:id=>"foo", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"foo"\}) to disappear$/
)
end
end
describe "#wait_until_stale" do
it "waits until the element disappears" do
stale_element = browser.div(id: 'foo')
stale_element.exists?
browser.refresh
expect { stale_element.wait_until_stale(1) }.to_not raise_exception
end
it "times out if the element doesn't go stale" do
element = browser.div(id: 'foo')
element.exists?
expect { element.wait_until_stale(1) }.to raise_error(Watir::Wait::TimeoutError,
/^timed out after 1 seconds, waiting for (\{:id=>"foo", :tag_name=>"div"\}|\{:tag_name=>"div", :id=>"foo"\}) to become stale$/
)
end
end
end
describe "Watir.default_timeout" do
before do
Watir.default_timeout = 1
browser.goto WatirSpec.url_for("wait.html")
end
after do
# Reset the default timeout
Watir.default_timeout = 30
end
context "when no timeout is specified" do
it "is used by Wait#until" do
expect {
Wait.until { false }
}.to raise_error(Watir::Wait::TimeoutError)
end
it "is used by Wait#while" do
expect {
Wait.while { true }
}.to raise_error(Watir::Wait::TimeoutError)
end
it "is used by Element#when_present" do
expect {
browser.div(id: 'bar').when_present.click
}.to raise_error(Watir::Wait::TimeoutError)
end
it "is used by Element#wait_until_present" do
expect {
browser.div(id: 'bar').wait_until_present
}.to raise_error(Watir::Wait::TimeoutError)
end
it "is used by Element#wait_while_present" do
expect {
browser.div(id: 'foo').wait_while_present
}.to raise_error(Watir::Wait::TimeoutError)
end
end
end
end