Skip to content

Commit 3968265

Browse files
committed
Create browser module, added user context related methods
1 parent 9af3d6e commit 3968265

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-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 get_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
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 get_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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
2.times { browser.create_user_context }
40+
expect(browser.get_user_contexts['userContexts'].count).to eq 3
41+
end
42+
end
43+
44+
it 'removes an user context' do
45+
reset_driver!(web_socket_url: true) do |driver|
46+
browser = described_class.new(driver.bidi)
47+
user_context = browser.create_user_context
48+
expect(browser.get_user_contexts['userContexts'].count).to eq 2
49+
browser.remove_user_context(user_context['userContext'])
50+
expect(browser.get_user_contexts['userContexts'].count).to eq 1
51+
end
52+
end
53+
54+
it 'throws an error when removing the default user context' do
55+
reset_driver!(web_socket_url: true) do |driver|
56+
browser = described_class.new(driver.bidi)
57+
expect {
58+
browser.remove_user_context('default')
59+
}.to raise_error(Error::WebDriverError, /user context cannot be removed/)
60+
end
61+
end
62+
63+
it 'throws an error when removing a non-existent user context' do
64+
reset_driver!(web_socket_url: true) do |driver|
65+
browser = described_class.new(driver.bidi)
66+
expect {
67+
browser.remove_user_context('fake_context')
68+
}.to raise_error(Error::WebDriverError, /Failed to find context with id/)
69+
end
70+
end
71+
end
72+
end
73+
end
74+
end

0 commit comments

Comments
 (0)