Skip to content

Commit cfab900

Browse files
authored
[rb] track the current window in the BiDi bridge so navigation skips the window handle round trip (#18374)
1 parent 14c12e8 commit cfab900

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

‎rb/lib/selenium/webdriver/remote/bidi_bridge.rb‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def create_session(capabilities)
4040
@bidi = Selenium::WebDriver::BiDi.new(url: validated_socket_url)
4141
# Reuse the BiDi object's socket as the connection until the bridge owns it directly.
4242
@connection = @bidi.ws
43+
@current_window = window_handle
4344
rescue StandardError
4445
quit
4546
raise
@@ -57,23 +58,31 @@ def uninstall_web_extension(id)
5758
end
5859

5960
def get(url)
60-
browsing_context.navigate(context: window_handle, url: url, wait: readiness_state)
61+
browsing_context.navigate(context: @current_window, url: url, wait: readiness_state)
6162
nil
63+
rescue Error::NoSuchFrameError => e
64+
raise Error::NoSuchWindowError, e.message
6265
end
6366

6467
def go_back
65-
browsing_context.traverse_history(context: window_handle, delta: -1)
68+
browsing_context.traverse_history(context: @current_window, delta: -1)
6669
nil
70+
rescue Error::NoSuchFrameError => e
71+
raise Error::NoSuchWindowError, e.message
6772
end
6873

6974
def go_forward
70-
browsing_context.traverse_history(context: window_handle, delta: 1)
75+
browsing_context.traverse_history(context: @current_window, delta: 1)
7176
nil
77+
rescue Error::NoSuchFrameError => e
78+
raise Error::NoSuchWindowError, e.message
7279
end
7380

7481
def refresh
75-
browsing_context.reload(context: window_handle, wait: readiness_state)
82+
browsing_context.reload(context: @current_window, wait: readiness_state)
7683
nil
84+
rescue Error::NoSuchFrameError => e
85+
raise Error::NoSuchWindowError, e.message
7786
end
7887

7988
def quit
@@ -88,6 +97,10 @@ def close
8897
execute(:close_window).tap { |handles| bidi.close if handles.empty? }
8998
end
9099

100+
def switch_to_window(name)
101+
super.tap { @current_window = name }
102+
end
103+
91104
private
92105

93106
def validated_socket_url

‎rb/sig/lib/selenium/webdriver/remote/bidi_bridge.rbs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module Selenium
2828

2929
@web_extension: BiDi::Protocol::WebExtension
3030

31+
@current_window: String
32+
3133
attr_reader bidi: BiDi
3234

3335
attr_reader connection: untyped
@@ -50,6 +52,8 @@ module Selenium
5052

5153
def close: () -> void
5254

55+
def switch_to_window: (String name) -> void
56+
5357
private
5458

5559
def validated_socket_url: () -> String

‎rb/spec/integration/selenium/webdriver/navigation_spec.rb‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ module WebDriver
5858

5959
expect(driver.title).to eq('Testing Javascript')
6060
end
61+
62+
context 'with more than one window' do
63+
let!(:original_window) { driver.window_handle }
64+
65+
after do
66+
driver.switch_to.window(original_window)
67+
(driver.window_handles - [original_window]).each do |handle|
68+
driver.switch_to.window(handle) { driver.close }
69+
end
70+
end
71+
72+
it 'raises when the current window is closed' do
73+
driver.switch_to.new_window(:tab)
74+
driver.close
75+
76+
expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::NoSuchWindowError)
77+
expect { driver.navigate.back }.to raise_error(Error::NoSuchWindowError)
78+
expect { driver.navigate.forward }.to raise_error(Error::NoSuchWindowError)
79+
expect { driver.navigate.refresh }.to raise_error(Error::NoSuchWindowError)
80+
end
81+
end
6182
end
6283
end # WebDriver
6384
end # Selenium

‎rb/spec/unit/selenium/webdriver/remote/bidi_bridge_spec.rb‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ def stub_new_session(web_socket_url)
5454
expect(http).to have_received(:request).at_least(:twice)
5555
end
5656
end
57+
58+
describe 'navigation' do
59+
let(:connection) { instance_double(WebSocketConnection) }
60+
let(:window_url) { URI('http://localhost/session/foo/window') }
61+
62+
before do
63+
stub_new_session('ws://localhost/session/foo')
64+
allow(http).to receive(:request).with(:get, window_url, any_args).and_return('value' => 'window-1')
65+
allow(http).to receive(:request).with(:post, window_url, any_args).and_return('value' => nil)
66+
allow(BiDi).to receive(:new).and_return(instance_double(BiDi, ws: connection))
67+
navigate_result = {'navigation' => nil, 'url' => 'about:blank'}
68+
allow(connection).to receive(:send_cmd).and_return('result' => navigate_result)
69+
bridge.create_session(Capabilities.new)
70+
end
71+
72+
it 'navigates the window it switched to' do
73+
bridge.switch_to_window('window-2')
74+
bridge.get('about:blank')
75+
76+
expect(connection).to have_received(:send_cmd)
77+
.with(method: 'browsingContext.navigate', params: hash_including('context' => 'window-2'))
78+
end
79+
80+
it 'requests the window handle only when the session starts' do
81+
bridge.get('about:blank')
82+
bridge.refresh
83+
84+
expect(http).to have_received(:request).with(:get, window_url, any_args).once
85+
end
86+
end
5787
end
5888
end # Remote
5989
end # WebDriver

0 commit comments

Comments
 (0)