forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_switching_spec.rb
397 lines (321 loc) · 12.4 KB
/
window_switching_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
require File.expand_path("../spec_helper", __FILE__)
not_compliant_on %i(webdriver iphone), %i(webdriver safari) do
describe "Browser" do
before do
url = WatirSpec.url_for("window_switching.html")
browser.goto url
browser.a(id: "open").click
end
after do
browser.window(index: 0).use
browser.windows[1..-1].each(&:close)
end
describe "#windows" do
it "returns an array of window handles" do
wins = browser.windows
expect(wins).to_not be_empty
wins.each { |win| expect(win).to be_kind_of(Window) }
end
it "only returns windows matching the given selector" do
expect(browser.windows(title: "closeable window").size).to eq 1
end
it "raises ArgumentError if the selector is invalid" do
expect { browser.windows(name: "foo") }.to raise_error(ArgumentError)
end
it "returns an empty array if no window matches the selector" do
expect(browser.windows(title: "noop")).to eq []
end
end
describe "#window" do
it "finds window by :url" do
w = browser.window(url: /closeable\.html/).use
expect(w).to be_kind_of(Window)
end
it "finds window by :title" do
w = browser.window(title: "closeable window").use
expect(w).to be_kind_of(Window)
end
it "finds window by :index" do
w = browser.window(index: 1).use
expect(w).to be_kind_of(Window)
end
it "should not find incorrect handle" do
expect(browser.window(handle: 'bar')).to_not be_present
end
it "returns the current window if no argument is given" do
expect(browser.window.url).to match(/window_switching\.html/)
end
it "stores the reference to a window when no argument is given" do
original_window = browser.window
browser.window(index: 1).use
expect(original_window.url).to match(/window_switching\.html/)
end
bug "http://github.com/jarib/celerity/issues#issue/17", :celerity do
it "it executes the given block in the window" do
browser.window(title: "closeable window") do
link = browser.a(id: "close")
expect(link).to exist
link.click
end.wait_while_present
expect(browser.windows.size).to eq 1
end
end
it "raises ArgumentError if the selector is invalid" do
expect { browser.window(name: "foo") }.to raise_error(ArgumentError)
end
it "raises a NoMatchingWindowFoundException error if no window matches the selector" do
expect { browser.window(title: "noop").use }.to raise_error(Watir::Exception::NoMatchingWindowFoundException)
end
it "raises a NoMatchingWindowFoundException error if there's no window at the given index" do
expect { browser.window(index: 100).use }.to raise_error(Watir::Exception::NoMatchingWindowFoundException)
end
it "raises NoMatchingWindowFoundException error when attempting to use a window with an incorrect handle" do
expect { browser.window(handle: 'bar').use }.to raise_error(Watir::Exception::NoMatchingWindowFoundException)
end
end
end
describe "Window" do
context 'multiple windows' do
before do
browser.goto WatirSpec.url_for("window_switching.html")
browser.a(id: "open").click
end
after do
browser.window(index: 0).use
browser.windows[1..-1].each(&:close)
end
describe "#close" do
it "closes a window" do
expect(browser.windows.size).to eq 2
browser.a(id: "open").click
expect(browser.windows.size).to eq 3
browser.window(title: "closeable window").close
expect(browser.windows.size).to eq 2
end
it "closes the current window" do
expect(browser.windows.size).to eq 2
browser.a(id: "open").click
expect(browser.windows.size).to eq 3
window = browser.window(title: "closeable window").use
window.close
expect(browser.windows.size).to eq 2
end
end
describe "#use" do
it "switches to the window" do
browser.window(title: "closeable window").use
expect(browser.title).to eq "closeable window"
end
end
describe "#current?" do
it "returns true if it is the current window" do
expect(browser.window(title: browser.title)).to be_current
end
it "returns false if it is not the current window" do
expect(browser.window(title: "closeable window")).to_not be_current
end
end
describe "#title" do
it "returns the title of the window" do
titles = browser.windows.map(&:title)
expect(titles.size).to eq 2
expect(titles.sort).to eq ["window switching", "closeable window"].sort
end
it "does not change the current window" do
expect(browser.title).to eq "window switching"
expect(browser.windows.find { |w| w.title == "closeable window" }).to_not be_nil
expect(browser.title).to eq "window switching"
end
end
describe "#url" do
it "returns the url of the window" do
expect(browser.windows.size).to eq 2
expect(browser.windows.select { |w| w.url =~ (/window_switching\.html/) }.size).to eq 1
expect(browser.windows.select { |w| w.url =~ (/closeable\.html$/) }.size).to eq 1
end
it "does not change the current window" do
expect(browser.url).to match(/window_switching\.html/)
expect(browser.windows.find { |w| w.url =~ (/closeable\.html/) }).to_not be_nil
expect(browser.url).to match(/window_switching/)
end
end
describe "#eql?" do
it "knows when two windows are equal" do
expect(browser.window).to eq browser.window(index: 0)
end
it "knows when two windows are not equal" do
win1 = browser.window(index: 0)
win2 = browser.window(index: 1)
expect(win1).to_not eq win2
end
end
describe "#when_present" do
it "waits until the window is present" do
# TODO: improve this spec.
did_yield = false
browser.window(title: "closeable window").when_present do
did_yield = true
end
expect(did_yield).to be true
end
it "times out waiting for a non-present window" do
expect {
browser.window(title: "noop").wait_until_present(0.5)
}.to raise_error(Wait::TimeoutError)
end
end
end
context "with a closed window" do
before do
browser.goto WatirSpec.url_for("window_switching.html")
browser.a(id: "open").click
end
after do
browser.window(index: 0).use
browser.windows[1..-1].each(&:close)
end
describe "#exists?" do
it "returns false if previously referenced window is closed" do
window = browser.window(title: "closeable window")
window.use
browser.a(id: "close").click
expect(window).to_not be_present
end
bug "https://code.google.com/p/chromedriver/issues/detail?id=950", %i(webdriver chrome) do
it "returns false if closed window is referenced" do
browser.window(title: "closeable window").use
browser.a(id: "close").click
expect(browser.window).to_not be_present
end
end
end
describe "#current?" do
it "returns false if the referenced window is closed" do
original_window = browser.window
browser.window(title: "closeable window").use
original_window.close
expect(original_window).to_not be_current
end
end
describe "#eql?" do
it "should return false when checking equivalence to a closed window" do
original_window = browser.window
other_window = browser.window(index: 1)
other_window.use
original_window.close
expect(other_window == original_window).to be false
end
end
describe "#use" do
it "raises NoMatchingWindowFoundException error when attempting to use a referenced window that is closed" do
original_window = browser.window
browser.window(index: 1).use
original_window.close
expect { original_window.use }.to raise_error(Watir::Exception::NoMatchingWindowFoundException)
end
it "raises NoMatchingWindowFoundException error when attempting to use the current window if it is closed" do
browser.window(title: "closeable window").use
browser.a(id: "close").click
expect { browser.window.use }.to raise_error(Watir::Exception::NoMatchingWindowFoundException)
end
end
end
context "with current window closed" do
before do
browser.goto WatirSpec.url_for("window_switching.html")
browser.a(id: "open").click
browser.window(title: "closeable window").use
browser.a(id: "close").click
end
after do
browser.window(index: 0).use
browser.windows[1..-1].each(&:close)
end
describe "#present?" do
it "should find window by index" do
expect(browser.window(index: 0)).to be_present
end
it "should find window by url" do
expect(browser.window(url: /window_switching\.html/)).to be_present
end
it "should find window by title" do
expect(browser.window(title: "window switching")).to be_present
end
end
describe "#use" do
context "switching windows without blocks" do
it "by index" do
browser.window(index: 0).use
expect(browser.title).to be == "window switching"
end
it "by url" do
browser.window(url: /window_switching\.html/).use
expect(browser.title).to be == "window switching"
end
it "by title" do
browser.window(title: "window switching").use
expect(browser.url).to match(/window_switching\.html/)
end
end
context "Switching windows with blocks" do
it "by index" do
browser.window(index: 0).use { expect(browser.title).to be == "window switching" }
end
it "by url" do
browser.window(url: /window_switching\.html/).use { expect(browser.title).to be == "window switching" }
end
it "by title" do
browser.window(title: "window switching").use { expect(browser.url).to match(/window_switching\.html/) }
end
end
end
end
context "manipulating size and position" do
before do
browser.goto WatirSpec.url_for("window_switching.html")
end
compliant_on %i(webdriver firefox), %i(webdriver chrome) do
it "should get the size of the current window" do
size = browser.window.size
expect(size.width).to be > 0
expect(size.height).to be > 0
end
it "should get the position of the current window" do
pos = browser.window.position
expect(pos.x).to be >= 0
expect(pos.y).to be >= 0
end
end
it "should resize the window" do
initial_size = browser.window.size
browser.window.resize_to(
initial_size.width - 10,
initial_size.height - 10
)
new_size = browser.window.size
expect(new_size.width).to eq initial_size.width - 10
expect(new_size.height).to eq initial_size.height - 10
end
it "should move the window" do
initial_pos = browser.window.position
browser.window.move_to(
initial_pos.x + 10,
initial_pos.y + 10
)
new_pos = browser.window.position
expect(new_pos.x).to eq initial_pos.x + 10
expect(new_pos.y).to eq initial_pos.y + 10
end
compliant_on %i(webdriver firefox window_manager) do
it "should maximize the window" do
initial_size = browser.window.size
browser.window.maximize
browser.wait_until { browser.window.size != initial_size }
new_size = browser.window.size
expect(new_size.width).to be > initial_size.width
expect(new_size.height).to be > initial_size.height
end
end
end
end
end