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
Expand Up @@ -3,6 +3,7 @@
### Added
- `Node#style` reads computed styles in one call, Capybara no longer falls back to its own script after
`NotImplementedError`
- `open_new_window(:window)` opens a window instead of a tab, an unknown kind raises `ArgumentError`
- `browser.go_to` takes the keywords of Ferrum's `go_to`, like `referrer:` and `timeout:`, it accepted only the URL
- `:url_blocklist` and `:url_allowlist` driver options as aliases for `:url_blacklist` and `:url_whitelist` [#330]
- `clear:` set option like Selenium's: `:none` appends, `:backspace` deletes with Backspace, an Array of keys is pressed
Expand Down
1 change: 1 addition & 0 deletions lib/capybara/cuprite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require "capybara/cuprite/browser"
require "capybara/cuprite/page"
require "capybara/cuprite/options"
require "capybara/cuprite/context"
require "capybara/cuprite/devices"
require "capybara/cuprite/node"
require "capybara/cuprite/errors"
Expand Down
13 changes: 11 additions & 2 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ def window_handles
targets.values.select(&:page?).map(&:id)
end

def open_new_window
attach_page(default_context.create_target.id)
# Opens a blank page without switching to it.
#
# @param kind [:tab, :window] a tab in the current window, or a window of its own
# @return [Page]
# @raise [ArgumentError] for any other kind
def open_new_window(kind = :tab)
unless %i[tab window].include?(kind)
raise ArgumentError, "open_new_window takes :tab or :window, got #{kind.inspect}"
end

attach_page(default_context.create_target(new_window: kind == :window).id)
end

def switch_to_window(target_id)
Expand Down
23 changes: 23 additions & 0 deletions lib/capybara/cuprite/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Ferrum
class Context
module NewWindow
# Creates a new target in this context, as Ferrum's `create_target` does once rubycdp/ferrum#733 is released.
#
# @param new_window [Boolean] opens the target in a window of its own instead of a tab
# @return [Ferrum::Target]
# @raise [Ferrum::NoSuchTargetError, Ferrum::TimeoutError]
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", **options)
@targets.await(response["targetId"], deadline) || raise(NoSuchTargetError)
end
end

prepend NewWindow unless instance_method(:create_target).parameters.include?(%i[key new_window])
end
end
6 changes: 4 additions & 2 deletions lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ def switch_to_frame(locator)
browser.page.switch_to_frame(handle)
end

def open_new_window
browser.open_new_window
# @param kind [:tab, :window] what Capybara's `open_new_window` asks for
# @return [Page]
def open_new_window(kind = :tab)
browser.open_new_window(kind)
end

def no_such_window_error
Expand Down
2 changes: 1 addition & 1 deletion sig/capybara/cuprite/browser.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Capybara

def window_handles: () -> Array[String]

def open_new_window: () -> Page
def open_new_window: (?(:tab | :window) kind) -> Page

def switch_to_window: (String target_id) -> nil

Expand Down
7 changes: 7 additions & 0 deletions sig/capybara/cuprite/context.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Ferrum
class Context
module NewWindow
def create_target: (?new_window: bool) -> Target
end
end
end
2 changes: 1 addition & 1 deletion sig/capybara/cuprite/driver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Capybara

def switch_to_frame: (Capybara::Node::Element | Node | :parent | :top locator) -> void

def open_new_window: () -> Page
def open_new_window: (?(:tab | :window) kind) -> Page

def no_such_window_error: () -> singleton(Ferrum::NoSuchPageError)

Expand Down
20 changes: 20 additions & 0 deletions spec/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,26 @@ def no_matches_message(description)
end

describe "#open_new_window" do
def window_id_of(window)
@session.within_window(window) { @driver.browser.page.window_id }
end

it "opens a tab in the current window by default" do
@session.visit "/"

expect(window_id_of(@session.open_new_window)).to eq(window_id_of(@session.current_window))
end

it "opens a window of its own for :window" do
@session.visit "/"

expect(window_id_of(@session.open_new_window(:window))).not_to eq(window_id_of(@session.current_window))
end

it "raises ArgumentError for a kind other than :tab and :window" do
expect { @driver.open_new_window(:popup) }.to raise_error(ArgumentError, /:tab or :window/)
end

it "opens a window that inherits the size" do
@session.visit "/"
@session.current_window.resize_to(1200, 800)
Expand Down
Loading