forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_spec.rb
220 lines (183 loc) · 6.76 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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
not_compliant_on [:webdriver, :safari] do
describe Watir::Wait do
describe "#until" do
it "waits until the block returns true" do
expect(Wait.until(0.5) { 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 "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", :needs_server => true)
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
not_compliant_on :watir_classic do
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
end
deviates_on :watir_classic do
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
end
it "responds to Element methods" do
decorator = browser.div.when_present
decorator.should respond_to(:exist?)
decorator.should respond_to(:present?)
decorator.should 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
end
describe "#wait_until_present" do
it "it waits until the element appears" do
browser.a(:id, 'show_bar').click
browser.div(:id, 'bar').wait_until_present(5)
end
not_compliant_on :watir_classic do
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
deviates_on :watir_classic do
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
end
describe "#wait_while_present" do
it "waits until the element disappears" do
browser.a(:id, 'hide_foo').click
browser.div(:id, 'foo').wait_while_present(1)
end
not_compliant_on :watir_classic do
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
deviates_on :watir_classic do
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
end
end
describe "Watir.default_timeout" do
before do
Watir.default_timeout = 1
browser.goto WatirSpec.url_for("wait.html", :needs_server => true)
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