From 8b75eea8d4c6491d8ec30d388b11dad8b9d17fdb Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Wed, 25 Nov 2026 11:07:16 +0300 Subject: [PATCH] feat(context): open a window instead of a tab with new_window: Target.createTarget opens a tab in the window Chrome last opened for the context unless newWindow is set. create_target and create_page of a context and of the browser take new_window: to pass it, so Capybara drivers can implement open_new_window(:window). --- CHANGELOG.md | 1 + lib/ferrum/browser.rb | 9 ++++++--- lib/ferrum/context.rb | 19 ++++++++++++++----- sig/ferrum/browser.rbs | 4 ++-- sig/ferrum/context.rbs | 4 ++-- spec/browser_spec.rb | 6 ++++++ spec/context_spec.rb | 20 ++++++++++++++++++++ 7 files changed, 51 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39d0b74c..0441b3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.21.0...main) ## ### Added +- `new_window:` for `create_target` and `create_page` of a context and of the browser opens a window instead of a tab - The gem ships its RBS signatures from `sig/`, so type checkers pick them up without local stubs [#731] ### Changed diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 1c6bdd73..94226c20 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -147,6 +147,9 @@ def initialize(options = nil) # @param [Boolean] new_context # Whether to create a page in a new context or not. # + # @param [Boolean] new_window + # Whether to open the page in a window of its own instead of a tab. + # # @param [Hash] proxy # Whether to use proxy for a page. The page will be created in a new context if so. # @@ -155,7 +158,7 @@ def initialize(options = nil) # # @raise [TimeoutError] # - def create_page(new_context: false, proxy: nil) + def create_page(new_context: false, new_window: false, proxy: nil) page = if new_context || proxy params = {} @@ -166,9 +169,9 @@ def create_page(new_context: false, proxy: nil) end context = contexts.create(**params) - context.create_page(proxy: proxy) + context.create_page(new_window: new_window, proxy: proxy) else - default_context.create_page + default_context.create_page(new_window: new_window) end block_given? ? yield(page) : page diff --git a/lib/ferrum/context.rb b/lib/ferrum/context.rb index 246c62c8..a874a84a 100644 --- a/lib/ferrum/context.rb +++ b/lib/ferrum/context.rb @@ -165,28 +165,37 @@ def await_page!(**options, &) # Creates a new target in this context and eagerly connects to it as a # {Page}, forwarding `options` to {Target#build_page}. # + # @param [Boolean] new_window + # Opens the page in a window of its own instead of a tab, see {#create_target}. + # # @param [Hash] options # # @return [Page] # # @raise [TimeoutError] - def create_page(**options) - target = create_target + def create_page(new_window: false, **options) + target = create_target(new_window: new_window) target.page = target.build_page(**options) end # Creates a new target in this context via `Target.createTarget` and # blocks until it's been registered (see {#add_target}). # + # @param [Boolean] new_window + # Opens the target in a window of its own. Otherwise it's a tab in the + # window Chrome last opened for this context. + # # @return [Target] # # @raise [NoSuchTargetError] # # @raise [TimeoutError] - def create_target - options = { browserContextId: @id } unless implicit? + def create_target(new_window: false) + options = {} + options[:browserContextId] = @id unless implicit? + options[:newWindow] = true if new_window deadline = Deadline.wrap(@client.timeout) - response = @client.command("Target.createTarget", timeout: deadline, url: "about:blank", **Hash(options)) + response = @client.command("Target.createTarget", timeout: deadline, url: "about:blank", **options) @targets.await(response["targetId"], deadline) || raise(NoSuchTargetError) end diff --git a/sig/ferrum/browser.rbs b/sig/ferrum/browser.rbs index 0b70b46c..0f64c73e 100644 --- a/sig/ferrum/browser.rbs +++ b/sig/ferrum/browser.rbs @@ -26,8 +26,8 @@ module Ferrum def default_user_agent=: (String) -> String - def create_page: (?new_context: bool, ?proxy: Hash[Symbol, (String | ::Integer)]?) -> Page - | [T] (?new_context: bool, ?proxy: Hash[Symbol, (String | ::Integer)]?) { (Page) -> T } -> T + def create_page: (?new_context: bool, ?new_window: bool, ?proxy: Hash[Symbol, (String | ::Integer)]?) -> Page + | [T] (?new_context: bool, ?new_window: bool, ?proxy: Hash[Symbol, (String | ::Integer)]?) { (Page) -> T } -> T def extensions: () -> ::Array[String] diff --git a/sig/ferrum/context.rbs b/sig/ferrum/context.rbs index db0c323f..28ef4b0d 100644 --- a/sig/ferrum/context.rbs +++ b/sig/ferrum/context.rbs @@ -34,9 +34,9 @@ module Ferrum def await_page!: (?opener: Page?, ?timeout: (::Numeric | Deadline)?) { () -> void } -> Page - def create_page: (**untyped options) -> Page + def create_page: (?new_window: bool, **untyped options) -> Page - def create_target: () -> Target + def create_target: (?new_window: bool) -> Target def add_target: (params: Hash[String, untyped], ?session_id: String?, ?attached: bool) -> Target diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 464bf02b..652c44f9 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -1055,6 +1055,12 @@ def closed_by_peer?(socket) expect(browser.targets.size).to eq(0) end + it "opens a page in a window of its own with new_window: true" do + page = browser.create_page + + expect(browser.create_page(new_window: true).window_id).not_to eq(page.window_id) + end + it "creates a page in a new context without a block" do expect(browser.contexts.size).to eq(0) diff --git a/spec/context_spec.rb b/spec/context_spec.rb index 05f63a94..9701cf0a 100644 --- a/spec/context_spec.rb +++ b/spec/context_spec.rb @@ -287,6 +287,26 @@ end end + describe "#create_target" do + def window_id(target) + browser.client.command("Browser.getWindowForTarget", timeout: 5, targetId: target.id)["windowId"] + end + + it "opens a tab in the window of the context" do + context = browser.contexts.create + first = context.create_target + + expect(window_id(context.create_target)).to eq(window_id(first)) + end + + it "opens a window of its own with new_window: true" do + context = browser.contexts.create + first = context.create_target + + expect(window_id(context.create_target(new_window: true))).not_to eq(window_id(first)) + end + end + describe "#attach_target" do it "reuses the session of an already attached target", :launch do browser = Ferrum::Browser.new(base_url: base_url,