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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
node without a box

### Changed
- Commands without a result return `nil` instead of `true`, `self` or Chrome's raw response: `set_viewport`, `resize`,
`stop`, `close`, `activate`, `execute`, `remove_script_on_new_document`, `dialog.accept`/`dismiss`, `network.intercept`,
`clear` and `emulate`, `headers.add`/`set`/`clear`, `cookies.set`/`remove`/`clear`/`load`, `downloads.set_behavior`,
`console.clear`, `browser.close`/`reset`, `context.dispose`, `contexts.dispose` and `worker.close`. `node.remove`,
`select_file` and `execute` return the node, like the other node commands
- `node.click(offset:)` and `hover(offset:)` measure `x` and `y` from the center by default, pass `position: :top` for
the top-left corner
- `browser.contexts.each` and the `Enumerable` methods yield contexts instead of `[id, context]` pairs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ want to keep a value in the browser and pass it back in later, and
`Ferrum::Node#evaluate` to run a script with `this` bound to an element:

```ruby
page.execute("window.scrollBy(0, 100)") # => true
page.execute("window.scrollBy(0, 100)")

list = page.evaluate_handle("document.querySelectorAll('li')")
page.evaluate("Array.from(nodes).map(n => n.textContent)", nodes: list)
Expand Down
32 changes: 16 additions & 16 deletions docs/11-cookies.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ browser.cookies["NID"] # => <Ferrum::Cookies::Cookie:0x0000558624b67a88 @attribu
browser.cookies["id", domain: "www.example.com", path: "/app"]
```

#### set(value) : `true`
#### set(value)

Sets a cookie, for the `:base_url` host unless `:domain` or `:url` is given.

Expand All @@ -62,17 +62,17 @@ Sets a cookie, for the `:base_url` host unless `:domain` or `:url` is given.
* :secure `Boolean`

```ruby
browser.cookies.set(name: "stealth", value: "omg", domain: "google.com") # => true
browser.cookies.set(name: "stealth", value: "omg", domain: "google.com")
```

* value `Cookie`

```ruby
nid_cookie = browser.cookies["NID"] # => <Ferrum::Cookies::Cookie:0x0000558624b67a88>
browser.cookies.set(nid_cookie) # => true
browser.cookies.set(nid_cookie)
```

#### remove(\*\*options) : `true`
#### remove(\*\*options)

Removes the cookies with the given name, on the exact domain, the `:base_url` host by default, or the ones sent to the
URL.
Expand All @@ -84,16 +84,16 @@ URL.
* :path `String`

```ruby
browser.cookies.remove(name: "stealth", domain: "google.com") # => true
browser.cookies.remove(name: "sso_token", url: "https://sso.example.com") # => true
browser.cookies.remove(name: "stealth", domain: "google.com")
browser.cookies.remove(name: "sso_token", url: "https://sso.example.com")
```

#### clear : `true`
#### clear

Removes all cookies in the context.

```ruby
browser.cookies.clear # => true
browser.cookies.clear
```

#### store(path) : `Integer`
Expand All @@ -104,12 +104,12 @@ Stores all cookies in the context in a file, `cookies.yml` by default.
browser.cookies.store # => 15657
```

#### load(path) : `true`
#### load(path)

Loads the cookies from a file, `cookies.yml` by default, and sets them in the context.

```ruby
browser.cookies.load # => true
browser.cookies.load
```

## Page cookies
Expand All @@ -134,27 +134,27 @@ The page's first cookie with the given name, narrowed down like the context's.
page.cookies["NID"] # => <Ferrum::Cookies::Cookie:0x0000558624b67a88>
```

#### set(value) : `Boolean`
#### set(value)

Sets a cookie, takes the same options as the context's `set`. Without `:domain` or `:url` it's set for the page's host,
or the `:base_url` host before the page has loaded anything.

```ruby
page.cookies.set(name: "stealth", value: "omg") # => true
page.cookies.set(name: "stealth", value: "omg")
```

#### remove(\*\*options) : `true`
#### remove(\*\*options)

Removes the cookies with the given name, on the page's host unless `:domain` or `:url` is given.

```ruby
page.cookies.remove(name: "stealth") # => true
page.cookies.remove(name: "stealth")
```

#### clear : `true`
#### clear

Removes the page's cookies, the ones `each` yields. Cookies on other domains stay.

```ruby
page.cookies.clear # => true
page.cookies.clear
```
6 changes: 3 additions & 3 deletions docs/12-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sidebar_position: 12

Get all headers

#### set(headers) : `Boolean`
#### set(headers)

Set given headers. Eventually clear all headers and set given ones.

Expand All @@ -22,12 +22,12 @@ back the browser's user agent or language once the header that changed it is gon
two headers are only sent with requests, like any other, and `navigator` and the client hints stay as they are. Use
`network.emulate(user_agent:, accept_language:)` to override both the header and what the page reports.

#### add(headers) : `Boolean`
#### add(headers)

Adds given headers to already set ones.

* headers `Hash` key-value pairs for example `"Referer" => "http://example.com"`

#### clear : `Boolean`
#### clear

Clear all headers, and the user agent or language they set.
8 changes: 4 additions & 4 deletions docs/13-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ list = page.evaluate_handle("document.querySelectorAll('li')")
page.evaluate("Array.from(nodes).map(n => n.textContent)", nodes: list)
```

#### execute(expression, \*args, timeout: nil, \*\*named) : `Boolean`
#### execute(expression, \*args, timeout: nil, \*\*named)

Run JavaScript for its side effects. The script is used as a function body, so statements need no wrapping.

```ruby
page.execute("window.scrollBy(0, 100)") # => true
page.execute("window.scrollBy(0, 100)")
```

`Ferrum::Node` has the same three methods, with `this` bound to the element. Arrow functions don't get `this`, so use a
Expand Down Expand Up @@ -117,12 +117,12 @@ page.evaluate("window.secret") # => nil
world.evaluate("document.title") # => same as page.evaluate("document.title")
```

#### remove_script_on_new_document(identifier) : `Boolean`
#### remove_script_on_new_document(identifier)

Stops running a script added with `evaluate_on_new_document` in new documents.

```ruby
page.remove_script_on_new_document(id) # => true
page.remove_script_on_new_document(id)
```

#### add_extension(extension)
Expand Down
6 changes: 3 additions & 3 deletions docs/18-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ page.at_css("#footer").scroll.in_viewport? # => true
page.at_css("#row").in_viewport?(of: page.at_css("#table")) # => true
```

#### select_file(value) : `Hash`
#### select_file(value) : `Node`

Sets files on a file input.

Expand Down Expand Up @@ -285,7 +285,7 @@ page.at_xpath("//*[select]").select(["1", "2"])

#### evaluate(expression, \*positional, \*\*keywords) : `Object`
#### evaluate_handle(expression, \*positional, \*\*keywords) : `RemoteObject` | `Node` | `Object`
#### execute(expression, \*positional, \*\*keywords) : `true`
#### execute(expression, \*positional, \*\*keywords) : `Node`

Run JavaScript with `this` bound to the node. `evaluate` returns the result, `evaluate_handle` returns a handle that
stays in the browser instead of a serialized value (a `Node` for a node, the value itself for a primitive), `execute`
Expand Down Expand Up @@ -329,7 +329,7 @@ page.at_css("input").pinpoint(x: 5, y: 5) # => [131.7, 406.4]
page.at_css("input").pinpoint(x: 5, y: 5, position: :top) # => [55.2, 395.9]
```

#### remove : `Hash`
#### remove : `Node`

Removes the node from the DOM.

Expand Down
2 changes: 1 addition & 1 deletion docs/21-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ worker.evaluate("1 + 1") # => 2
Subscribe to the worker's own network interception events, same as `page.on(:request)` -- see the `intercept` method
in [Network](/docs/ferrum/network), or to its new console messages.

#### close : `Boolean`
#### close

Closes the underlying CDP target and the worker's own connection.

Expand Down
2 changes: 1 addition & 1 deletion docs/7-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ page.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://g
page.network.offline = false
```

#### emulate(\*\*options) : `Network`
#### emulate(\*\*options)

Overrides the user agent, the accept language, the platform and the client hints the page reports, in the request
headers and in `navigator`. The options you don't pass keep their previous values, `nil` brings one back to the browser's default.
Expand Down
6 changes: 6 additions & 0 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ def create_page(new_context: false, proxy: nil)
#
# Closes browser tabs opened by the `Browser` instance.
#
# @return [nil]
#
# @raise [TimeoutError]
#
# @example
Expand All @@ -214,6 +216,7 @@ def create_page(new_context: false, proxy: nil)
#
def reset
contexts.reset
nil
end

#
Expand Down Expand Up @@ -258,8 +261,11 @@ def crash
#
# You should clean up resources/connections in ruby world manually, it's only a CDP command.
#
# @return [nil]
#
def close
command("Browser.close")
nil
end

#
Expand Down
4 changes: 2 additions & 2 deletions lib/ferrum/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def empty?
#
# Forgets the collected messages.
#
# @return [self]
# @return [nil]
#
def clear
@mutex.synchronize { @messages.clear }
self
nil
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def close_targets_connection
# Disposes this browser context and all of its targets. The browser's
# implicit context cannot be disposed, see {#implicit?}.
#
# @return [Boolean, nil]
# @return [nil]
#
# @raise [TimeoutError]
def dispose
Expand Down
5 changes: 3 additions & 2 deletions lib/ferrum/contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def create(**options)
#
# @param [String] context_id
#
# @return [Boolean, nil]
# @return [nil]
#
# @raise [TimeoutError, BrowserError]
def dispose(context_id)
Expand All @@ -129,7 +129,8 @@ def dispose(context_id)
ensure
@contexts.delete(context_id)
end
true

nil
end

# Closes the WebSocket connection of every target in every context,
Expand Down
16 changes: 8 additions & 8 deletions lib/ferrum/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def each
# @option options [String] :samesite
# Specifies whether the cookie is `SameSite`, also accepted as `:same_site`.
#
# @return [true]
# @return [nil]
#
# @raise [BrowserError] if neither `:domain`, `:url` nor `:base_url` is set.
#
Expand All @@ -95,7 +95,7 @@ def set(options)
cookie[:domain] ||= default_domain unless cookie[:url]

write(cookie)
true
nil
end

#
Expand All @@ -116,7 +116,7 @@ def set(options)
# @option options [String] :path
# The exact path of the cookie.
#
# @return [true]
# @return [nil]
#
# @raise [TimeoutError]
#
Expand All @@ -135,13 +135,13 @@ def remove(name:, **options)
end
cookies.each { |cookie| write(cookie.attributes.merge("expires" => 1)) }

true
nil
end

#
# Removes all cookies in the context.
#
# @return [true]
# @return [nil]
#
# @raise [TimeoutError]
#
Expand All @@ -150,7 +150,7 @@ def remove(name:, **options)
#
def clear
@client.command("Storage.clearCookies", timeout: @client.timeout, browserContextId: @context_id)
true
nil
end

#
Expand All @@ -170,7 +170,7 @@ def store(path = "cookies.yml")
#
# Loads all cookies from the file and sets them in the context.
#
# @return [true]
# @return [nil]
#
# @raise [TimeoutError]
#
Expand All @@ -180,7 +180,7 @@ def store(path = "cookies.yml")
def load(path = "cookies.yml")
cookies = YAML.load_file(path)
cookies.each { |c| set(c) }
true
nil
end

private
Expand Down
6 changes: 6 additions & 0 deletions lib/ferrum/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def handled?
#
# @param [String, nil] prompt_text
#
# @return [nil]
#
# @raise [TimeoutError]
#
# @example
Expand All @@ -50,11 +52,14 @@ def accept(prompt_text = nil)
options.merge!(promptText: response) if response
@handled = true
@page.command("Page.handleJavaScriptDialog", slowmoable: true, **options)
nil
end

#
# Dismiss dialog.
#
# @return [nil]
#
# @raise [TimeoutError]
#
# @example
Expand All @@ -71,6 +76,7 @@ def accept(prompt_text = nil)
def dismiss
@handled = true
@page.command("Page.handleJavaScriptDialog", slowmoable: true, accept: false)
nil
end

#
Expand Down
Loading
Loading