Skip to content

Commit 148487d

Browse files
authored
[rb][BiDi] Create browser module, added user context related methods (#15371)
* Create browser module, added user context related methods * Fix formatting issues * Fix delete cookie test * Fix tests for firefox browser contexts
1 parent 359c309 commit 148487d

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BiDi
2323
autoload :Session, 'selenium/webdriver/bidi/session'
2424
autoload :LogInspector, 'selenium/webdriver/bidi/log_inspector'
2525
autoload :LogHandler, 'selenium/webdriver/bidi/log_handler'
26+
autoload :Browser, 'selenium/webdriver/bidi/browser'
2627
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
2728
autoload :Struct, 'selenium/webdriver/bidi/struct'
2829
autoload :Network, 'selenium/webdriver/bidi/network'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class BiDi
23+
class Browser
24+
def initialize(bidi)
25+
@bidi = bidi
26+
end
27+
28+
def create_user_context
29+
@bidi.send_cmd('browser.createUserContext')
30+
end
31+
32+
def user_contexts
33+
@bidi.send_cmd('browser.getUserContexts')
34+
end
35+
36+
def remove_user_context(user_context)
37+
@bidi.send_cmd('browser.removeUserContext', userContext: user_context)
38+
end
39+
end
40+
end # BiDi
41+
end # WebDriver
42+
end # Selenium

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def cookie_named(name)
7979
#
8080

8181
def delete_cookie(name)
82+
raise ArgumentError, 'Cookie name cannot be null or empty' if name.nil? || name.to_s.strip.empty?
83+
8284
@bridge.delete_cookie name
8385
end
8486

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Selenium
2+
module WebDriver
3+
class BiDi
4+
class Browser
5+
@bidi: BiDi
6+
7+
def initialize: (BiDi bidi) -> void
8+
9+
def create_user_context: () -> Hash[String, String]
10+
11+
def user_contexts: () -> Array[Hash[String, String]]
12+
13+
def remove_user_context: (String user_context) -> Hash[nil, nil]
14+
end
15+
end
16+
end
17+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
require_relative '../spec_helper'
21+
22+
module Selenium
23+
module WebDriver
24+
class BiDi
25+
describe Browser, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
26+
only: {browser: %i[chrome edge firefox]} do
27+
it 'creates an user context' do
28+
reset_driver!(web_socket_url: true) do |driver|
29+
browser = described_class.new(driver.bidi)
30+
user_context = browser.create_user_context
31+
expect(user_context).not_to be_nil
32+
expect(user_context['userContext']).to be_a String
33+
end
34+
end
35+
36+
it 'gets user contexts' do
37+
reset_driver!(web_socket_url: true) do |driver|
38+
browser = described_class.new(driver.bidi)
39+
created_context_id = browser.create_user_context['userContext']
40+
all_context_ids = browser.user_contexts['userContexts'].map { |c| c['userContext'] }
41+
42+
expect(all_context_ids).to include(created_context_id)
43+
end
44+
end
45+
46+
it 'removes an user context' do
47+
reset_driver!(web_socket_url: true) do |driver|
48+
browser = described_class.new(driver.bidi)
49+
context_id_to_remove = browser.create_user_context['userContext']
50+
browser.remove_user_context(context_id_to_remove)
51+
all_ids_after_removal = browser.user_contexts['userContexts'].map { |c| c['userContext'] }
52+
53+
expect(all_ids_after_removal).not_to include(context_id_to_remove)
54+
end
55+
end
56+
57+
it 'throws an error when removing the default user context' do
58+
reset_driver!(web_socket_url: true) do |driver|
59+
browser = described_class.new(driver.bidi)
60+
expect {
61+
browser.remove_user_context('default')
62+
}.to raise_error(Error::WebDriverError, /user context cannot be removed/)
63+
end
64+
end
65+
66+
it 'throws an error when removing a non-existent user context' do
67+
reset_driver!(web_socket_url: true) do |driver|
68+
browser = described_class.new(driver.bidi)
69+
expect {
70+
browser.remove_user_context('fake_context')
71+
}.to raise_error(Error::WebDriverError)
72+
end
73+
end
74+
end
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)