forked from watir/watirspec
/
window_switching_spec.rb
244 lines (195 loc) · 6.95 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
require File.expand_path("../spec_helper", __FILE__)
not_compliant_on [:webdriver, :iphone], [: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
begin
browser.window(:title => "closeable window").close
rescue
# not ideal - clean these up
end
end
describe "#windows" do
it "returns an array of window handles" do
wins = browser.windows
wins.should_not be_empty
wins.each { |win| win.should be_kind_of(Window) }
end
it "only returns windows matching the given selector" do
browser.windows(:title => "closeable window").size.should == 1
end
it "raises ArgumentError if the selector is invalid" do
lambda { browser.windows(:name => "foo") }.should raise_error(ArgumentError)
end
it "raises returns an empty array if no window matches the selector" do
browser.windows(:title => "noop").should == []
end
end
describe "#window" do
it "finds window by :url" do
w = browser.window(:url => /closeable\.html/).use
w.should be_kind_of(Window)
end
it "finds window by :title" do
w = browser.window(:title => "closeable window").use
w.should be_kind_of(Window)
end
it "finds window by :index" do
w = browser.window(:index => 1).use
w.should be_kind_of(Window)
end
it "returns the current window if no argument is given" do
browser.window.url.should =~ /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")
link.should exist
link.click
end.wait_while_present
browser.windows.size.should == 1
end
end
it "raises ArgumentError if the selector is invalid" do
lambda { browser.window(:name => "foo") }.should raise_error(ArgumentError)
end
it "raises an error if no window matches the selector" do
lambda { browser.window(:title => "noop").use }.should raise_error
end
it "raises an error if there's no window at the given index" do
lambda { browser.window(:index => 100).use }.should raise_error
end
end
end
describe "Window" do
before do
url = WatirSpec.url_for("window_switching.html")
browser.goto url
browser.a(:id => "open").click
end
after do
browser.window(:title => "closeable window").close
end
describe "#close" do
it "closes the window" do
browser.windows.size.should == 2
browser.a(:id => "open").click
browser.windows.size.should == 3
browser.window(:title => "closeable window").close
browser.windows.size.should == 2
end
end
describe "#use" do
it "switches to the window" do
browser.window(:title => "closeable window").use
browser.title.should == "closeable window"
end
end
describe "#current?" do
it "returns true if it is the current window" do
browser.window(:title => browser.title).should be_current
end
it "returns false if it is not the current window" do
browser.window(:title => "closeable window").should_not be_current
end
end
describe "#title" do
it "returns the title of the window" do
titles = browser.windows.map { |e| e.title }
titles.size.should == 2
titles.sort.should == ["window switching", "closeable window"].sort
end
it "does not change the current window" do
browser.title.should == "window switching"
browser.windows.find { |w| w.title == "closeable window" }.should_not be_nil
browser.title.should == "window switching"
end
end
describe "#url" do
it "returns the url of the window" do
browser.windows.size.should == 2
browser.windows.select { |w| w.url =~ /window_switching\.html/ }.size.should == 1
browser.windows.select { |w| w.url =~ /closeable\.html$/ }.size.should == 1
end
it "does not change the current window" do
browser.url.should =~ /window_switching\.html/
browser.windows.find { |w| w.url =~ /closeable\.html/ }.should_not be_nil
browser.url.should =~ /window_switching/
end
end
describe "#==" do
it "knows when two windows are equal" do
browser.window.should == browser.window
end
it "knows when two windows are not equal" do
win1 = browser.window
win2 = browser.window(:title => "closeable window")
win1.should_not == 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
did_yield.should be_true
end
it "times out waiting for a non-present window" do
lambda {
browser.window(:title => "noop").wait_until_present(0.5)
}.should raise_error(Wait::TimeoutError)
end
end
describe "size and position" do
compliant_on [:webdriver, :firefox], [:webdriver, :chrome] do
it "should get the size of the current window" do
size = browser.window.size
size.width.should > 0
size.height.should > 0
end
it "should get the position of the current window" do
pos = browser.window.position
pos.x.should >= 0
pos.y.should >= 0
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
new_size.width.should == initial_size.width - 10
new_size.height.should == 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
new_pos.x.should == initial_pos.x + 10
new_pos.y.should == initial_pos.y + 10
end
compliant_on [: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
new_size.width.should > initial_size.width
new_size.height.should > initial_size.height
end
end
end
end
end
end