Skip to content

Commit d047b4d

Browse files
committed
[rb] move new window functionality into TargetLocator to match other bindings
1 parent a254c33 commit d047b4d

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

rb/lib/selenium/webdriver/common/manager.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ def logs
114114
end
115115

116116
#
117-
# Create a new top-level browsing context
118-
# https://w3c.github.io/webdriver/#new-window
119117
# @param type [Symbol] Supports two values: :tab and :window.
120-
# Use :tab if you'd like the new window to share an OS-level window
121-
# with the current browsing context.
122-
# Use :window otherwise
123118
# @return [String] The value of the window handle
124119
#
125120
def new_window(type = :tab)
121+
WebDriver.logger.deprecate('Manager#new_window', 'TargetLocator#new_window', id: :new_window) do
122+
'e.g., `driver.switch_to.new_window(:tab)`'
123+
end
126124
case type
127125
when :tab, :window
128126
result = @bridge.new_window(type)

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,34 @@ def parent_frame
4444
@bridge.switch_to_parent_frame
4545
end
4646

47+
#
48+
# Switch to a new top-level browsing context
49+
#
50+
# @param type either :tab or :window
51+
#
52+
53+
def new_window(type = :window)
54+
unless %i[window tab].include?(type)
55+
raise ArgumentError, "Valid types are :tab and :window, received: #{type.inspect}"
56+
end
57+
58+
handle = @bridge.new_window(type)['handle']
59+
60+
if block_given?
61+
execute_and_close = proc do
62+
yield(self)
63+
begin
64+
@bridge.close
65+
rescue Error::NoSuchWindowError
66+
# window already closed
67+
end
68+
end
69+
window(handle, &execute_and_close)
70+
else
71+
window(handle)
72+
end
73+
end
74+
4775
#
4876
# switch to the given window handle
4977
#

rb/spec/integration/selenium/webdriver/manager_spec.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,8 @@ module WebDriver
267267
describe 'new_window' do
268268
after { ensure_single_window }
269269

270-
types = %i[tab window]
271-
types.each do |type|
272-
it "should be able to open a new #{type}" do
273-
before_window_handles = driver.window_handles.length
274-
driver.manage.new_window(type)
275-
expect(driver.window_handles.length).to eq(before_window_handles + 1)
276-
end
277-
end
278-
279270
it "returns an exception if an invalid type is provided" do
280-
invalid_types = [:invalid, 'invalid', 'tab', 'window']
281-
invalid_types.each do |type|
282-
expect { driver.manage.new_window(type) }.to \
283-
raise_error(ArgumentError, "invalid argument for type. Got: '#{type.inspect}'. Try :tab or :window")
284-
end
271+
expect { driver.manage.new_window }.to have_deprecated(:new_window)
285272
end
286273
end
287274
end # Options

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,47 @@ module WebDriver
6868
quit_driver
6969
end
7070

71+
describe '#new_window' do
72+
it 'should switch to a new window' do
73+
original_window = driver.window_handle
74+
driver.switch_to.new_window(:window)
75+
76+
expect(driver.window_handles.size).to eq 2
77+
expect(driver.window_handle).not_to eq original_window
78+
end
79+
80+
it 'should switch to a new tab' do
81+
original_window = driver.window_handle
82+
driver.switch_to.new_window(:tab)
83+
84+
expect(driver.window_handles.size).to eq 2
85+
expect(driver.window_handle).not_to eq original_window
86+
end
87+
88+
it 'should raise exception when the new window type is not recognized' do
89+
expect {
90+
driver.switch_to.new_window(:unknown)
91+
}.to raise_error(ArgumentError)
92+
end
93+
94+
it 'should switch to the new window then close it when given a block' do
95+
original_window = driver.window_handle
96+
97+
driver.switch_to.new_window do
98+
expect(driver.window_handles.size).to eq 2
99+
end
100+
101+
expect(driver.window_handles.size).to eq 1
102+
expect(driver.window_handle).to eq original_window
103+
end
104+
105+
it 'should not error if switching to a new window with a block that closes window' do
106+
expect {
107+
driver.switch_to.new_window { driver.close }
108+
}.not_to raise_exception
109+
end
110+
end
111+
71112
it 'should switch to a window and back when given a block' do
72113
driver.navigate.to url_for('xhtmlTest.html')
73114

0 commit comments

Comments
 (0)