Skip to content

Create browser module, added user context related methods #15371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/bidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BiDi
autoload :Session, 'selenium/webdriver/bidi/session'
autoload :LogInspector, 'selenium/webdriver/bidi/log_inspector'
autoload :LogHandler, 'selenium/webdriver/bidi/log_handler'
autoload :Browser, 'selenium/webdriver/bidi/browser'
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
autoload :Struct, 'selenium/webdriver/bidi/struct'
autoload :Network, 'selenium/webdriver/bidi/network'
Expand Down
42 changes: 42 additions & 0 deletions rb/lib/selenium/webdriver/bidi/browser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
class BiDi
class Browser
def initialize(bidi)
@bidi = bidi
end

def create_user_context
@bidi.send_cmd('browser.createUserContext')
end

def user_contexts
@bidi.send_cmd('browser.getUserContexts')
end

def remove_user_context(user_context)
@bidi.send_cmd('browser.removeUserContext', userContext: user_context)
end
end
end # BiDi
end # WebDriver
end # Selenium
2 changes: 2 additions & 0 deletions rb/lib/selenium/webdriver/common/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def cookie_named(name)
#

def delete_cookie(name)
raise ArgumentError, 'Cookie name cannot be null or empty' if name.nil? || name.to_s.strip.empty?

@bridge.delete_cookie name
end

Expand Down
17 changes: 17 additions & 0 deletions rb/sig/lib/selenium/webdriver/bidi/browser.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Selenium
module WebDriver
class BiDi
class Browser
@bidi: BiDi

def initialize: (BiDi bidi) -> void

def create_user_context: () -> Hash[String, String]

def user_contexts: () -> Array[Hash[String, String]]

def remove_user_context: (String user_context) -> Hash[nil, nil]
end
end
end
end
74 changes: 74 additions & 0 deletions rb/spec/integration/selenium/webdriver/bidi/browser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

require_relative '../spec_helper'

module Selenium
module WebDriver
class BiDi
describe Browser, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
only: {browser: %i[chrome edge firefox]} do
it 'creates an user context' do
reset_driver!(web_socket_url: true) do |driver|
browser = described_class.new(driver.bidi)
user_context = browser.create_user_context
expect(user_context).not_to be_nil
expect(user_context['userContext']).to be_a String
end
end

it 'gets user contexts' do
reset_driver!(web_socket_url: true) do |driver|
browser = described_class.new(driver.bidi)
2.times { browser.create_user_context }
expect(browser.user_contexts['userContexts'].count).to eq 3
end
end

it 'removes an user context' do
reset_driver!(web_socket_url: true) do |driver|
browser = described_class.new(driver.bidi)
user_context = browser.create_user_context
expect(browser.user_contexts['userContexts'].count).to eq 2
browser.remove_user_context(user_context['userContext'])
expect(browser.user_contexts['userContexts'].count).to eq 1
end
end

it 'throws an error when removing the default user context' do
reset_driver!(web_socket_url: true) do |driver|
browser = described_class.new(driver.bidi)
expect {
browser.remove_user_context('default')
}.to raise_error(Error::WebDriverError, /user context cannot be removed/)
end
end

it 'throws an error when removing a non-existent user context' do
reset_driver!(web_socket_url: true) do |driver|
browser = described_class.new(driver.bidi)
expect {
browser.remove_user_context('fake_context')
}.to raise_error(Error::WebDriverError, /Failed to find context with id/)
end
end
end
end
end
end
Loading