diff --git a/rb/lib/selenium/webdriver/bidi.rb b/rb/lib/selenium/webdriver/bidi.rb index 4c8ca37b4f8d2..25c7393a88f2f 100644 --- a/rb/lib/selenium/webdriver/bidi.rb +++ b/rb/lib/selenium/webdriver/bidi.rb @@ -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' diff --git a/rb/lib/selenium/webdriver/bidi/browser.rb b/rb/lib/selenium/webdriver/bidi/browser.rb new file mode 100644 index 0000000000000..e5d64e1fdbd9f --- /dev/null +++ b/rb/lib/selenium/webdriver/bidi/browser.rb @@ -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 diff --git a/rb/lib/selenium/webdriver/common/manager.rb b/rb/lib/selenium/webdriver/common/manager.rb index 99181d7700f67..2f4fdeb90c5ed 100644 --- a/rb/lib/selenium/webdriver/common/manager.rb +++ b/rb/lib/selenium/webdriver/common/manager.rb @@ -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 diff --git a/rb/sig/lib/selenium/webdriver/bidi/browser.rbs b/rb/sig/lib/selenium/webdriver/bidi/browser.rbs new file mode 100644 index 0000000000000..fe6f06548c9ac --- /dev/null +++ b/rb/sig/lib/selenium/webdriver/bidi/browser.rbs @@ -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 diff --git a/rb/spec/integration/selenium/webdriver/bidi/browser_spec.rb b/rb/spec/integration/selenium/webdriver/bidi/browser_spec.rb new file mode 100644 index 0000000000000..1482009eda942 --- /dev/null +++ b/rb/spec/integration/selenium/webdriver/bidi/browser_spec.rb @@ -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