Skip to content

Commit 332988c

Browse files
authored
[rb] Close BiDi session on closing the last top-level browsing context
1 parent 1e2a4c4 commit 332988c

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def callbacks
3737
end
3838

3939
def session
40-
Session.new(self)
40+
@session ||= Session.new(self)
4141
end
4242

4343
def send_cmd(method, **params)

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def for(browser, opts = {})
7171
def initialize(bridge: nil, listener: nil, **opts)
7272
@service_manager = nil
7373
@devtools = nil
74+
@bidi = nil
7475
bridge ||= create_bridge(**opts)
7576
add_extensions(bridge.browser)
7677
@bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge
@@ -174,14 +175,18 @@ def quit
174175
ensure
175176
@service_manager&.stop
176177
@devtools&.close
178+
@bidi&.close
177179
end
178180

179181
#
180182
# Close the current window, or the browser if no windows are left.
181183
#
182184

183185
def close
184-
bridge.close
186+
# If no top-level browsing contexts are open after calling close,
187+
# it indicates that the WebDriver session is closed.
188+
# If the WebDriver session is closed, the BiDi session also needs to be closed.
189+
bridge.close.tap { |handles| @bidi&.close if handles&.empty? }
185190
end
186191

187192
#

rb/spec/integration/selenium/webdriver/bidi/browsing_context_spec.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
module Selenium
2323
module WebDriver
2424
class BiDi
25-
describe BrowsingContext, exclusive: {browser: %i[chrome firefox]} do
25+
describe BrowsingContext, only: {browser: %i[chrome edge firefox]} do
2626
before { reset_driver!(web_socket_url: true) }
2727
after { quit_driver }
2828

@@ -32,50 +32,47 @@ class BiDi
3232
expect(browsing_context.id).to eq(id)
3333
end
3434

35-
it 'can create a window without a reference context' do
35+
it 'can create a window' do
3636
browsing_context = described_class.new(driver: driver, type: :window)
3737
expect(browsing_context.id).not_to be_nil
3838
end
3939

40-
it 'can create a window with a reference context', except: {browser: :chrome} do
40+
it 'can create a window with a reference context', except: {browser: %i[chrome edge]} do
4141
browsing_context = described_class.new(driver: driver, type: :window,
4242
reference_context: driver.window_handle)
4343
expect(browsing_context.id).not_to be_nil
4444
end
4545

46-
it 'can create a tab without a reference context' do
46+
it 'can create a tab' do
4747
browsing_context = described_class.new(driver: driver, type: :tab)
4848
expect(browsing_context.id).not_to be_nil
4949
end
5050

51-
it 'can create a tab with a reference context', except: {browser: :chrome} do
51+
it 'can create a tab with a reference context', except: {browser: %i[chrome edge]} do
5252
browsing_context = described_class.new(driver: driver, type: :tab, reference_context: driver.window_handle)
5353
expect(browsing_context.id).not_to be_nil
5454
end
5555

56-
it 'can navigate to a url without a readiness state',
57-
except: {browser: :chrome, reason: 'navigation_id is not nil'} do
56+
it 'can navigate to a url' do
5857
browsing_context = described_class.new(driver: driver, type: :tab)
5958

6059
info = browsing_context.navigate url: url_for('/bidi/logEntryAdded.html')
6160

6261
expect(browsing_context.id).not_to be_nil
63-
expect(info.navigation_id).to be_nil
6462
expect(info.url).to include('/bidi/logEntryAdded.html')
6563
end
6664

67-
it 'can navigate to a url with readiness state', except: {browser: :chrome} do
65+
it 'can navigate to a url with readiness state' do
6866
browsing_context = described_class.new(driver: driver, type: :tab)
6967

7068
info = browsing_context.navigate url: url_for('/bidi/logEntryAdded.html'),
7169
readiness_state: :complete
7270

7371
expect(browsing_context.id).not_to be_nil
74-
expect(info.navigation_id).to be_nil
7572
expect(info.url).to include('/bidi/logEntryAdded.html')
7673
end
7774

78-
it 'can get tree with a child', except: {browser: :chrome} do
75+
it 'can get tree with a child', except: {browser: %i[chrome edge]} do
7976
browsing_context_id = driver.window_handle
8077
parent_window = described_class.new(driver: driver, browsing_context_id: browsing_context_id)
8178
parent_window.navigate(url: url_for('iframes.html'),
@@ -87,7 +84,7 @@ class BiDi
8784
expect(context_info.children[0]['url']).to include('formPage.html')
8885
end
8986

90-
it 'can get tree with depth', except: {browser: :chrome, reason: 'not yet implemented'} do
87+
it 'can get tree with depth', except: {browser: %i[chrome edge], reason: 'not yet implemented'} do
9188
browsing_context_id = driver.window_handle
9289
parent_window = described_class.new(driver: driver, browsing_context_id: browsing_context_id)
9390
parent_window.navigate(url: url_for('iframes.html'),

rb/spec/integration/selenium/webdriver/bidi_spec.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ module Selenium
2323
module WebDriver
2424
describe BiDi, only: {browser: %i[chrome edge firefox]} do
2525
before { reset_driver!(web_socket_url: true) }
26-
after { quit_driver }
26+
27+
after do
28+
quit_driver
29+
rescue Selenium::WebDriver::Error::InvalidSessionIdError
30+
# do nothing
31+
end
2732

2833
it 'gets session status' do
2934
status = driver.bidi.session.status
@@ -52,6 +57,32 @@ module WebDriver
5257
level: BiDi::LogInspector::LOG_LEVEL[:ERROR]
5358
)
5459
end
60+
61+
it 'does not close BiDi session if at least one window is opened' do
62+
status = driver.bidi.session.status
63+
expect(status.ready).to be false
64+
expect(status.message).to be_a String
65+
66+
driver.switch_to.new_window(:window)
67+
driver.switch_to.new_window(:tab)
68+
driver.switch_to.new_window(:tab)
69+
70+
driver.close
71+
72+
status_after_closing = driver.bidi.session.status
73+
expect(status_after_closing.ready).to be false
74+
expect(status_after_closing.message).to be_a String
75+
end
76+
77+
it 'closes BiDi session if last window is closed' do
78+
status = driver.bidi.session.status
79+
expect(status.ready).to be false
80+
expect(status.message).to be_a String
81+
82+
driver.close
83+
84+
expect { driver.bidi.session.status }.to raise_error(IOError)
85+
end
5586
end
5687
end
5788
end

0 commit comments

Comments
 (0)