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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- `context.target(id)` returns the context's target with that id
- `timeout:` on `keyboard.type`, `node.type` and `mouse.move` bounds all the keys or steps together, the page `timeout`
by default, raise it for a long text
- `context.wait_for_new_page { ... }`, and `browser.wait_for_new_page` for the default context, runs the block and
returns the page it opens, in a tab or a window, or `nil` if none opens in time. `wait_for_new_page!` raises
`Ferrum::TimeoutError` instead. Pass `opener:` to only get the pages opened by one page
- `pages` of a context and of the browser takes a block that selects targets, e.g. `pages(&:opener_id)` for
the pages opened by another page
- `go_to(url, referrer:)` sets the referrer of that navigation only, the page's other requests don't carry it
- `cookies.set` accepts `http_only:` and `same_site:` next to `httponly:` and `samesite:`, like the cookie's readers
- `cookies[name, domain:, path:]` picks one of the cookies sharing a name
Expand Down Expand Up @@ -48,6 +53,9 @@
- `on` with a raw CDP event yields only the params, not the callback's index and the total number of callbacks

### Deprecated
- `target.window?`, will be removed in 0.22. Use `target.opener_id`, it is set for a target another one opened
- `windows` of a context and of the browser, will be removed in 0.22. Use `wait_for_new_page { ... }` for the page a click
opens, or `pages(&:opener_id)` for all of them
- `cookies.all`, will be removed in 0.22. Keyed by name, it kept one of the cookies sharing a name, use `to_a`, or
`map(&:name)` for the names
- `current_url` and `current_title` on pages and frames, will be removed in 0.22. Use `url` and `title`, or
Expand Down
38 changes: 38 additions & 0 deletions docs/3-navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,41 @@ Current window id
```ruby
browser.window_id # => 1
```

#### wait_for_new_page(\*\*options) { } : `Page` | `nil`

Runs the block and returns the page it opens, in a tab or a window, e.g. through `window.open` or a link with
`target="_blank"`. Returns `nil` if no page opens in time.

* options `Hash`
* :opener `Page` only return a page opened by this page, by default a page opened by any page of the context
* :timeout `Numeric` how long the block and the new page may take together, the browser's `timeout` by default

```ruby
opened = browser.wait_for_new_page { browser.at_css("a[target=_blank]").click }
opened.url # => "https://example.com/"
opened.close

page = browser.create_page
opened = browser.wait_for_new_page(opener: page) { page.at_css("a[target=_blank]").click }
```

#### wait_for_new_page!(\*\*options) { } : `Page`

Same as `wait_for_new_page`, but raises `Ferrum::TimeoutError` if no page opens in time.

The target of an opened page has the id of the page that opened it:

```ruby
browser.targets[opened.target_id].opener_id == page.target_id # => true
```

#### pages { |target| } : `Array<Page>`

Returns the pages of the default context. With a block, only the pages of the targets it returns truthy for.

```ruby
browser.pages # => [#<Ferrum::Page>, #<Ferrum::Page>]
browser.pages(&:opener_id) # the pages opened by another page
browser.pages { |t| t.opener_id == page.target_id } # the pages opened by `page`
```
4 changes: 2 additions & 2 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Browser
extend Forwardable

delegate %i[default_context] => :contexts
delegate %i[targets create_target page pages windows workers service_workers attach_target
cookies] => :default_context
delegate %i[targets create_target page pages windows wait_for_new_page wait_for_new_page! workers service_workers
attach_target cookies] => :default_context
delegate %i[go_to goto go back forward refresh reload stop wait_for_reload
wait_for_navigation wait_for_navigation!
at_css at_xpath css xpath wait_for_selector current_url current_title url title
Expand Down
97 changes: 90 additions & 7 deletions lib/ferrum/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Ferrum
#
class Context
POSITION = %i[first last].freeze
WINDOWS_DEPRECATED = "windows will be removed in Ferrum 0.22. Use wait_for_new_page { ... } for the page a click " \
"opens, or pages(&:opener_id) for all of them."

attr_reader :id

Expand All @@ -21,6 +23,8 @@ def initialize(client, contexts, id, implicit: false)
@client = client
@contexts = contexts
@targets = Concurrency::AwaitableMap.new
@page_waiters = []
@page_waiters_mutex = Mutex.new
end

# Whether this is the browser's implicit context, the one it puts its
Expand Down Expand Up @@ -66,11 +70,22 @@ def targets

# All page targets in this context, connected to as {Page}s.
#
# @yield [target]
# Only the pages of the targets the block returns truthy for are
# connected to and returned.
#
# @yieldparam [Target] target
#
# @return [Array<Page>]
#
# @raise [TimeoutError]
def pages
@targets.values.select(&:page?).map(&:page)
#
# @example The pages opened by another page
# browser.pages(&:opener_id)
def pages(&)
targets = @targets.values.select(&:page?)
targets = targets.select(&) if block_given?
targets.map(&:page)
end

# Dedicated and shared workers spawned by any page in this context,
Expand All @@ -93,18 +108,75 @@ def service_workers
@targets.values.select(&:service_worker?)
end

# When we call `page` method on target it triggers ruby to connect to given
# page by WebSocket, if there are many opened windows, but we need only one
# it makes more sense to get and connect to the needed one only which
# usually is the last one.
# The pages of this context opened by another page.
#
# @deprecated Will be removed in Ferrum 0.22. Use {#wait_for_new_page} for the page a click opens, or
# `pages(&:opener_id)` for all of them.
#
# @param [:first, :last, nil] pos
#
# @param [Integer] size
#
# @return [Array<Page>]
#
# @raise [ArgumentError]
#
# @raise [TimeoutError]
def windows(pos = nil, size = 1)
Utils::Deprecate.warn(WINDOWS_DEPRECATED, user_code: true)
raise ArgumentError if pos && !POSITION.include?(pos)

windows = @targets.values.select(&:window?)
windows = @targets.values.select(&:opener_id)
windows = windows.send(pos, size) if pos
windows.map(&:page)
end

# Runs the block and returns the page it opens, in a tab or a window,
# e.g. through `window.open` or a link with `target="_blank"`.
#
# @param [Page, nil] opener
# Only return a page opened by this page. By default a page opened by
# any page of the context is returned.
#
# @param [Numeric, Deadline] timeout
# How long the block and the new page may take together. Defaults to
# the browser's `timeout`.
#
# @yield The code that opens the page.
#
# @return [Page, nil]
# The first page opened since the call, `nil` if none opens in time.
#
# @example
# opened = browser.wait_for_new_page { browser.at_css("a[target=_blank]").click }
# opened.url # => "https://example.com/"
def wait_for_new_page(opener: nil, timeout: @client.timeout)
deadline = Deadline.wrap(timeout)
waiter = [opener&.target_id, Concurrency::Cell.new]
@page_waiters_mutex.synchronize { @page_waiters << waiter }
yield if block_given?
return unless waiter.last.wait(deadline)

waiter.last.value.page
ensure
@page_waiters_mutex.synchronize { @page_waiters.delete(waiter) }
end

# Same as {#wait_for_new_page}, but raises if no page opens in time.
#
# @param [Hash] options
# The options of {#wait_for_new_page}.
#
# @yield The code that opens the page.
#
# @return [Page]
#
# @raise [TimeoutError]
# If no page opens in time.
def wait_for_new_page!(**options, &)
wait_for_new_page(**options, &) || raise(TimeoutError)
end

# Creates a new target in this context and eagerly connects to it as a
# {Page}, forwarding `options` to {Target#build_page}.
#
Expand Down Expand Up @@ -153,6 +225,8 @@ def add_target(params:, session_id: nil, attached: false)
target.session_id = session_id if session_id && (attached || target.session_id.nil?)
@default_target ||= target
@targets[target.id] = target
announce_page(target) if target.equal?(new_target)
target
end

# Updates a known target's params, e.g. on `Target.targetInfoChanged`.
Expand Down Expand Up @@ -255,5 +329,14 @@ def target?(target_id)
def inspect
%(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
end

private

def announce_page(target)
return unless target.page? && target.opener_id

waiters = @page_waiters_mutex.synchronize { @page_waiters.select { |id, _| id.nil? || id == target.opener_id } }
waiters.each { |w| w.last.set(target) }
end
end
end
10 changes: 7 additions & 3 deletions lib/ferrum/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Ferrum
#
class Target
NEW_WINDOW_WAIT = ENV.fetch("FERRUM_NEW_WINDOW_WAIT", 0.3).to_f
WINDOW_DEPRECATED = "window? will be removed in Ferrum 0.22. Use opener_id, it is set for a target another one " \
"opened."

# You can create page yourself and assign it to target, used in cuprite
# where we enhance page class and build page ourselves.
Expand Down Expand Up @@ -159,11 +161,13 @@ def context_id
@params["browserContextId"] unless @implicit
end

# Whether this target is a window/tab, i.e. was opened via
# `window.open`/a link/etc. and thus has an {#opener_id}.
# Whether this target was opened by another one, i.e. has an {#opener_id}.
#
# @deprecated Will be removed in Ferrum 0.22. Use {#opener_id}.
#
# @return [Boolean]
def window?
Utils::Deprecate.warn(WINDOW_DEPRECATED, user_code: true)
!!opener_id
end

Expand Down Expand Up @@ -208,7 +212,7 @@ def service_worker?
# @return [void]
def maybe_sleep_if_new_window
# Dirty hack because new window doesn't have events at all
sleep(NEW_WINDOW_WAIT) if window?
sleep(NEW_WINDOW_WAIT) if opener_id
end

# Sends a CDP command through {#client}.
Expand Down
15 changes: 13 additions & 2 deletions sig/ferrum/context.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Ferrum
class Context
POSITION: ::Array[:first | :last]
WINDOWS_DEPRECATED: String

attr_reader id: String

Expand All @@ -11,6 +12,8 @@ module Ferrum
@client: Client
@contexts: Contexts
@targets: Concurrency::AwaitableMap[String, Target]
@page_waiters: ::Array[[String?, Concurrency::Cell]]
@page_waiters_mutex: ::Mutex
@default_target: Target?
@cookies: Cookies

Expand All @@ -24,13 +27,17 @@ module Ferrum

def page: () -> Page

def pages: () -> ::Array[Page]
def pages: () ?{ (Target) -> boolish } -> ::Array[Page]

def workers: () -> ::Array[Worker]

def service_workers: () -> ::Array[Target]

def windows: (?(:first | :last)?, ?::Integer) -> ::Array[Page]
%a{deprecated} def windows: (?(:first | :last)?, ?::Integer) -> ::Array[Page]

def wait_for_new_page: (?opener: Page?, ?timeout: (::Numeric | Deadline)) ?{ () -> void } -> Page?

def wait_for_new_page!: (?opener: Page?, ?timeout: (::Numeric | Deadline)) ?{ () -> void } -> Page

def create_page: (**untyped options) -> Page

Expand All @@ -55,5 +62,9 @@ module Ferrum
def target?: (String target_id) -> bool

def inspect: () -> ::String

private

def announce_page: (Target target) -> void
end
end
3 changes: 2 additions & 1 deletion sig/ferrum/target.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Ferrum
class Target
NEW_WINDOW_WAIT: ::Float
WINDOW_DEPRECATED: String

attr_writer page: Page?
attr_reader options: Browser::Options
Expand Down Expand Up @@ -47,7 +48,7 @@ module Ferrum

def context_id: () -> String?

def window?: () -> bool
%a{deprecated} def window?: () -> bool

def iframe?: () -> bool

Expand Down
31 changes: 8 additions & 23 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ def broken.puts(*) = raise(IOError, "closed stream")
extensions: [File.expand_path("support/geolocation.js", __dir__)])
browser.go_to

browser.execute(%(window.open("/requiring_custom_extension", "popup")))
popup, = browser.windows(:last)
popup = browser.wait_for_new_page { browser.execute(%(window.open("/requiring_custom_extension", "popup"))) }

expect(popup.at_css("#location").text).to eq("1,-1")
ensure
Expand Down Expand Up @@ -897,22 +896,13 @@ def proxy_tunnel(socket, request, browser_url)

expect(browser.targets.size).to eq(1)

browser.execute <<-JS
window.open("/simple", "popup1")
JS

sleep 0.1

browser.execute <<-JS
window.open("/simple", "popup2")
JS
popup1 = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup1"))) }
popup2 = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup2"))) }
popup1.resize(width: 100, height: 200)
popup2.resize(width: 200, height: 100)

popup1, popup2 = browser.windows(:last, 2)
popup1&.resize(width: 100, height: 200)
popup2&.resize(width: 200, height: 100)

expect(popup1&.viewport_size).to eq([100, 200])
expect(popup2&.viewport_size).to eq([200, 100])
expect(popup1.viewport_size).to eq([100, 200])
expect(popup2.viewport_size).to eq([200, 100])
end

it "resizes the viewport to the page size with fullscreen: true" do
Expand Down Expand Up @@ -949,15 +939,10 @@ def proxy_tunnel(socket, request, browser_url)

expect(browser.targets.size).to eq(2)

browser.execute <<~JS
window.open("/simple", "popup2")
JS

sleep 0.1
popup2 = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup2"))) }

expect(browser.targets.size).to eq(3)

popup2, = browser.windows(:last)
expect(popup2.body).to include("Test")
# Browser isn't dead, current page after executing JS closes connection
# and we don't have a chance to push response to the Queue. Since the
Expand Down
Loading
Loading