Skip to content

Commit b312088

Browse files
titusfortnerjarib
authored andcommitted
Allow switching windows when current window is closed
Signed-off-by: Jari Bakken <[email protected]>
1 parent bd92545 commit b312088

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

rb/lib/selenium/webdriver/common/target_locator.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ def parent_frame
3838

3939
def window(id)
4040
if block_given?
41-
original = @bridge.getCurrentWindowHandle
41+
original = begin
42+
@bridge.getCurrentWindowHandle
43+
rescue Error::NoSuchWindowError
44+
nil
45+
end
46+
4247
@bridge.switchToWindow id
4348

4449
begin
4550
returned = yield
4651
ensure
4752
current_handles = @bridge.getWindowHandles
48-
49-
if current_handles.size == 1
50-
original = current_handles.shift
51-
end
52-
53+
original = current_handles.first unless current_handles.include? original
5354
@bridge.switchToWindow original
5455
returned
5556
end

rb/spec/integration/selenium/webdriver/target_locator_spec.rb

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242

4343
# switching by name not yet supported by safari
4444
not_compliant_on :browser => [:ie, :iphone, :safari] do
45+
after do
46+
reset_driver!
47+
end
48+
4549
it "should switch to a window and back when given a block" do
4650
driver.navigate.to url_for("xhtmlTest.html")
4751

@@ -54,7 +58,6 @@
5458

5559
wait.until { driver.title == "XHTML Test Page" }
5660

57-
reset_driver!
5861
end
5962

6063
it "should handle exceptions inside the block" do
@@ -69,7 +72,6 @@
6972

7073
driver.title.should == "XHTML Test Page"
7174

72-
reset_driver!
7375
end
7476

7577
it "should switch to a window" do
@@ -81,7 +83,6 @@
8183
driver.switch_to.window("result")
8284
wait.until { driver.title == "We Arrive Here" }
8385

84-
reset_driver!
8586
end
8687

8788
it "should use the original window if the block closes the popup" do
@@ -97,7 +98,74 @@
9798

9899
driver.current_url.should include("xhtmlTest.html")
99100
driver.title.should == "XHTML Test Page"
100-
reset_driver!
101+
end
102+
103+
it "should close current window when more than two windows exist" do
104+
driver.navigate.to url_for("xhtmlTest.html")
105+
driver.find_element(:link, "Create a new anonymous window").click
106+
driver.find_element(:link, "Open new window").click
107+
108+
expect(driver.window_handles.size).to eq 3
109+
110+
driver.switch_to.window(driver.window_handle) {driver.close}
111+
expect(driver.window_handles.size).to eq 2
112+
end
113+
114+
it "should close another window when more than two windows exist" do
115+
driver.navigate.to url_for("xhtmlTest.html")
116+
driver.find_element(:link, "Create a new anonymous window").click
117+
driver.find_element(:link, "Open new window").click
118+
119+
expect(driver.window_handles.size).to eq 3
120+
121+
window_to_close = driver.window_handles.last
122+
123+
driver.switch_to.window(window_to_close) {driver.close}
124+
expect(driver.window_handles.size).to eq 2
125+
end
126+
127+
it "should iterate over open windows when current window is not closed" do
128+
driver.navigate.to url_for("xhtmlTest.html")
129+
driver.find_element(:link, "Create a new anonymous window").click
130+
driver.find_element(:link, "Open new window").click
131+
132+
new_window = driver.window_handles.find do |wh|
133+
driver.switch_to.window(wh) { driver.title == "We Arrive Here" }
134+
end
135+
136+
driver.switch_to.window(new_window)
137+
driver.title.should == "We Arrive Here"
138+
end
139+
140+
it "should iterate over open windows when current window is closed" do
141+
driver.navigate.to url_for("xhtmlTest.html")
142+
driver.find_element(:link, "Create a new anonymous window").click
143+
driver.find_element(:link, "Open new window").click
144+
145+
driver.close
146+
147+
new_window = driver.window_handles.find do |wh|
148+
driver.switch_to.window(wh) { driver.title == "We Arrive Here" }
149+
end
150+
151+
driver.switch_to.window(new_window)
152+
driver.title.should == "We Arrive Here"
153+
end
154+
155+
it "should switch to a window and execute a block when current window is closed" do
156+
driver.navigate.to url_for("xhtmlTest.html")
157+
driver.find_element(:link, "Open new window").click
158+
159+
driver.switch_to.window("result")
160+
wait.until { driver.title == "We Arrive Here" }
161+
162+
driver.close
163+
164+
driver.switch_to.window(driver.window_handles.first) do
165+
wait.until { driver.title == "XHTML Test Page" }
166+
end
167+
168+
driver.title.should == "XHTML Test Page"
101169
end
102170
end
103171

0 commit comments

Comments
 (0)