Skip to content

Commit 812ec89

Browse files
committed
docs: sync cuprite and vessel docs
1 parent 145383c commit 812ec89

32 files changed

Lines changed: 1907 additions & 4 deletions

‎docs/cuprite/1-introduction.md‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,86 @@ sidebar_position: 1
44

55
# Introduction
66

7-
Hello there
7+
Cuprite is a pure Ruby driver for [Capybara](https://github.com/teamcapybara/capybara).
8+
9+
<img align="right"
10+
width="160" height="160"
11+
alt="Cuprite logo"
12+
src="https://raw.githubusercontent.com/rubycdp/cuprite/main/logo.svg?sanitize=true" />
13+
14+
It allows you to run Capybara tests on a headless Chrome or Chromium with _no_
15+
Selenium/WebDriver/ChromeDriver dependency. Under the hood it uses
16+
[Ferrum](/docs/ferrum/introduction), a high-level API to the browser over the
17+
[CDP protocol](https://chromedevtools.github.io/devtools-protocol/).
18+
19+
<br clear="right" />
20+
21+
## Installation
22+
23+
Download Chrome or Chromium from the official source for
24+
[Chrome](https://www.google.com/chrome/) or [Chromium](https://www.chromium.org/getting-involved/download-chromium).
25+
The binary should be in the `PATH` or `BROWSER_PATH`, or you can pass it with the
26+
`:browser_path` option, see [Customization](/docs/cuprite/customization).
27+
28+
Add this to your `Gemfile` and run `bundle install`.
29+
30+
```ruby
31+
group :test do
32+
gem "cuprite"
33+
end
34+
```
35+
36+
## Quick Start
37+
38+
In your test setup add:
39+
40+
```ruby
41+
require "capybara/cuprite"
42+
43+
Capybara.javascript_driver = :cuprite
44+
Capybara.register_driver(:cuprite) do |app|
45+
Capybara::Cuprite::Driver.new(app, window_size: [1200, 800])
46+
end
47+
```
48+
49+
Requiring `capybara/cuprite` already registers a `:cuprite` driver with default
50+
options, so you only need to register it yourself if you want to customize it.
51+
52+
Then write your tests as usual:
53+
54+
```ruby
55+
it "signs in", js: true do
56+
visit "/login"
57+
58+
fill_in "Email", with: "user@example.com"
59+
fill_in "Password", with: "secret"
60+
click_button "Sign in"
61+
62+
expect(page).to have_content("Welcome")
63+
end
64+
```
65+
66+
## Docker
67+
68+
See [Docker](/docs/cuprite/docker).
69+
70+
## Ferrum under the hood
71+
72+
Since Cuprite is built on top of Ferrum there are many useful methods you can
73+
call even using this driver. `page.driver.browser` is a `Ferrum::Browser`
74+
instance:
75+
76+
```ruby
77+
browser = page.driver.browser
78+
browser.mouse.move(x: 123, y: 456).down.up
79+
browser.network.emulate_network_conditions(offline: true)
80+
```
81+
82+
See [Ferrum documentation](/docs/ferrum/introduction) for the full API.
83+
84+
## Migrating from Selenium
85+
86+
Check your code for `page.driver.browser.manage` calls, there's no `manage` in
87+
Cuprite. Use driver methods instead, for example
88+
[cookies](/docs/cuprite/cookies) or
89+
[window resizing](/docs/cuprite/windows-frames).

‎docs/cuprite/10-network.md‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# Network
6+
7+
#### network_traffic(type = nil) : `Array<Ferrum::Network::Exchange>`
8+
9+
Returns network traffic (loaded resources) on the current page. Each
10+
[exchange](/docs/ferrum/network) contains request and response details.
11+
12+
* type `Symbol`
13+
* `nil` - all requests that weren't blocked (default)
14+
* `:all` - all requests
15+
* `:blocked` - only requests blocked by [URL filtering](/docs/cuprite/url-filtering)
16+
17+
```ruby
18+
traffic = page.driver.network_traffic
19+
exchange = traffic.first
20+
exchange.request.url # => "http://127.0.0.1:3000/"
21+
exchange.response.status # => 200
22+
23+
page.driver.network_traffic(:blocked)
24+
```
25+
26+
Network traffic is not cleared when you visit a new page.
27+
28+
#### clear_network_traffic
29+
30+
Clears collected network traffic.
31+
32+
```ruby
33+
page.driver.clear_network_traffic
34+
```
35+
36+
#### wait_for_network_idle(\*\*options) : `Boolean`
37+
38+
Waits until there are no active connections. Returns `true` on success and
39+
`false` if there are still connections after timeout. Accepts the same options
40+
as Ferrum's [wait_for_idle](/docs/ferrum/network):
41+
42+
* options `Hash`
43+
* :connections `Integer` how many connections are allowed for network to be idling, `0` by default
44+
* :duration `Float` sleep for given amount of time and check again, `0.05` by default
45+
* :timeout `Float` during what time we try to check idle, `browser.timeout` by default
46+
47+
```ruby
48+
click_button "Save"
49+
page.driver.wait_for_network_idle
50+
```
51+
52+
#### wait_for_reload(timeout = 1)
53+
54+
Unlike `wait_for_network_idle` waits until the whole page is reloaded. Useful
55+
when you know an action, like choosing an autocomplete suggestion, reloads the
56+
page. Sleep makes you wait longer or less than needed, and network idle can
57+
return before the page even started to reload.
58+
59+
```ruby
60+
find(".suggestion").click
61+
page.driver.wait_for_reload
62+
```
63+
64+
#### status_code : `Integer`
65+
66+
Returns status code of the main page response.
67+
68+
```ruby
69+
page.status_code # => 200
70+
```
71+
72+
#### response_headers : `Hash`
73+
74+
Returns headers of the main page response.
75+
76+
```ruby
77+
page.response_headers["Content-Type"] # => "text/html; charset=utf-8"
78+
```
79+
80+
#### clear_memory_cache
81+
82+
Clears the browser cache.
83+
84+
```ruby
85+
page.driver.clear_memory_cache
86+
```

‎docs/cuprite/11-headers.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
sidebar_position: 11
3+
---
4+
5+
# Headers
6+
7+
Manipulate HTTP request headers. Headers apply to all subsequent HTTP requests,
8+
including assets, AJAX, etc. They are cleared at the end of every test.
9+
10+
#### headers : `Hash`
11+
12+
Returns current headers.
13+
14+
```ruby
15+
page.driver.headers # => {}
16+
```
17+
18+
#### headers=(headers)
19+
20+
Sets headers, overwriting already set ones.
21+
22+
```ruby
23+
page.driver.headers = { "User-Agent" => "Cuprite" }
24+
```
25+
26+
#### add_headers(headers)
27+
28+
Adds headers to already set ones.
29+
30+
```ruby
31+
page.driver.add_headers("Referer" => "https://example.com")
32+
page.driver.headers # => { "User-Agent" => "Cuprite", "Referer" => "https://example.com" }
33+
```
34+
35+
#### add_header(name, value, permanent: true)
36+
37+
Adds a single header. With `permanent: false` a `Referer` header is applied to
38+
the next navigation only.
39+
40+
```ruby
41+
page.driver.add_header("Referer", "https://example.com", permanent: false)
42+
```

‎docs/cuprite/12-cookies.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
sidebar_position: 12
3+
---
4+
5+
# Cookies
6+
7+
#### cookies : `Hash<String, Ferrum::Cookies::Cookie>`
8+
9+
Returns cookies accessible to the current page. The keys are cookie names, the
10+
values are cookie objects with `name`, `value`, `domain`, `path`, `size`,
11+
`secure?`, `httponly?`, `session?`, `samesite` and `expires` methods.
12+
13+
```ruby
14+
page.driver.cookies["session_id"].value # => "abc"
15+
```
16+
17+
#### set_cookie(name, value, options = {})
18+
19+
Sets a cookie. Domain defaults to the current page's host, or to
20+
`Capybara.app_host` (or `127.0.0.1`) if no page was visited yet.
21+
22+
* options `Hash`
23+
* :domain `String`
24+
* :path `String`
25+
* :secure `Boolean`
26+
* :httponly `Boolean`
27+
* :samesite `String`
28+
* :expires `Time`
29+
30+
```ruby
31+
page.driver.set_cookie("stealth", "omg", path: "/", expires: Time.now + 3600)
32+
```
33+
34+
#### remove_cookie(name, \*\*options)
35+
36+
Removes a cookie. Domain defaults to the current page's host, you can pass
37+
`:domain` or `:url`.
38+
39+
```ruby
40+
page.driver.remove_cookie("stealth")
41+
```
42+
43+
#### clear_cookies
44+
45+
Removes all cookies.
46+
47+
```ruby
48+
page.driver.clear_cookies
49+
```

‎docs/cuprite/13-screenshots.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
sidebar_position: 13
3+
---
4+
5+
# Screenshots
6+
7+
#### save_screenshot(path, options = {})
8+
9+
Capybara's `save_screenshot` saves an image or a PDF if the path ends with
10+
`.pdf`. Also aliased as `render`. Accepts all options of Ferrum's
11+
[screenshot and pdf](/docs/ferrum/screenshots) methods.
12+
13+
```ruby
14+
page.save_screenshot("page.png", full: true)
15+
page.save_screenshot("button.png", selector: "#submit")
16+
page.save_screenshot("page.jpg", quality: 60)
17+
page.save_screenshot("page.pdf", landscape: true)
18+
```
19+
20+
Screenshots are saved to `Capybara.save_path` when a relative path is given.
21+
22+
#### render_base64(format = :png, options = {}) : `String`
23+
24+
Returns an image or a PDF as Base64.
25+
26+
```ruby
27+
page.driver.render_base64(:png, full: true)
28+
page.driver.render_base64(:jpeg, quality: 60)
29+
page.driver.render_base64(:png, format: :pdf)
30+
```
31+
32+
#### zoom_factor=(value)
33+
34+
Sets scale for subsequent screenshots and PDFs.
35+
36+
```ruby
37+
page.driver.zoom_factor = 2
38+
```
39+
40+
#### paper_size=(size)
41+
42+
Sets paper size in inches for subsequent PDFs.
43+
44+
```ruby
45+
page.driver.paper_size = { width: 8.5, height: 11 }
46+
page.save_screenshot("page.pdf")
47+
```
48+
49+
Zoom factor and paper size are reset at the end of every test.

‎docs/cuprite/14-authorization.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
sidebar_position: 14
3+
---
4+
5+
# Authorization
6+
7+
#### basic_authorize(user, password)
8+
9+
Sets credentials for HTTP basic authentication. Also aliased as `authorize`.
10+
11+
```ruby
12+
page.driver.basic_authorize("login", "password")
13+
visit "/admin"
14+
```

‎docs/cuprite/15-proxy.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
sidebar_position: 15
3+
---
4+
5+
# Proxy
6+
7+
#### set_proxy(host, port, user = nil, password = nil, bypass = nil)
8+
9+
Sets a proxy for the browser.
10+
11+
```ruby
12+
page.driver.set_proxy("127.0.0.1", 8080, "user", "password", ["localhost"])
13+
```
14+
15+
Or pass the `:proxy` option:
16+
17+
```ruby
18+
Capybara::Cuprite::Driver.new(
19+
app,
20+
proxy: { host: "127.0.0.1", port: 8080, user: "user", password: "password", bypass: ["localhost"] }
21+
)
22+
```
23+
24+
The proxy is passed to Chrome as a command-line flag, so it has to be set before
25+
the browser starts: either with the `:proxy` driver option or by calling
26+
`set_proxy` before the browser is first used. Calling it once the browser is
27+
running has no effect.
28+
29+
When connecting to a remote browser with the `:url` option (browserless,
30+
Selenoid, etc.), Cuprite doesn't launch Chrome and the proxy flag is never
31+
applied, so configure the proxy on the remote browser itself. The `user` and
32+
`password` are still used to answer the proxy's authentication challenge.
33+
34+
The `bypass` argument is a list of hosts that skip the proxy, so don't pass the
35+
host you want to go through it.
36+
37+
See Ferrum's [Proxy](/docs/ferrum/proxy) for more details.

0 commit comments

Comments
 (0)