Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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 = {}

Expand All @@ -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
Expand Down
19 changes: 14 additions & 5 deletions lib/ferrum/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions sig/ferrum/browser.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions sig/ferrum/context.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions spec/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading