Skip to content

Commit b2e71da

Browse files
committed
docs: sync ferrum and cuprite docs
1 parent 812ec89 commit b2e71da

8 files changed

Lines changed: 181 additions & 14 deletions

File tree

‎docs/cuprite/2-customization.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ Cuprite-specific options are:
2727
[URL filtering](/docs/cuprite/url-filtering).
2828
* `:url_whitelist` (Array) - Regexes to match against requested URLs that are allowed, see
2929
[URL filtering](/docs/cuprite/url-filtering).
30+
* `:emulate` (Symbol) - Device to emulate, one of those listed in Chrome DevTools' device toolbar, such as
31+
`:iphone_16`, `:pixel_9`, `:samsung_galaxy_a55` or `:ipad_mini`, see `Capybara::Cuprite::Devices::LIST`. It sets
32+
the device's `:window_size`, `:scale_factor` and `:mobile`, options passed explicitly take precedence, and its
33+
user agent, unless a `User-Agent` is set with [headers](/docs/cuprite/headers).
34+
* `:mobile` (Boolean) - Emulate a mobile device: honor `<meta name="viewport">` and enable touch events, `false`
35+
by default. Kept across window resizes.
36+
* `:scale_factor` (Numeric) - Device scale factor (`window.devicePixelRatio`) to emulate, `0` (browser default)
37+
by default.
3038

3139
Cuprite changes a few Ferrum defaults:
3240

3341
* `:save_path` - defaults to `Capybara.save_path` if it's set.
3442
* `:pending_connection_errors` - `true` by default.
3543
* `:browser_options` - `"remote-allow-origins" => "*"` is always added, so that
36-
[debug](/docs/cuprite/debugging) can open DevTools.
44+
[debug](/docs/cuprite/debugging) can open DevTools, and so is `"disable-web-security" => nil`, so that tests
45+
can reach into cross-origin frames.
3746

3847
## Examples
3948

@@ -49,6 +58,15 @@ Capybara::Cuprite::Driver.new(app, timeout: 15, process_timeout: 30)
4958

5059
# Connect to a remote browser
5160
Capybara::Cuprite::Driver.new(app, url: "http://chrome:3333")
61+
62+
# Leave the window size to Chrome, e.g. to start maximized in headful mode
63+
Capybara::Cuprite::Driver.new(app, headless: false, window_size: nil, browser_options: { "start-maximized" => nil })
64+
65+
# Emulate an iPhone 14
66+
Capybara::Cuprite::Driver.new(app, emulate: :iphone_14)
67+
68+
# Emulate a Pixel 9 with a custom scale factor
69+
Capybara::Cuprite::Driver.new(app, emulate: :pixel_9, scale_factor: 1)
5270
```
5371

5472
## Environment variables

‎docs/cuprite/6-node.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ automatically.
7979
* :scroll `Boolean` scroll elements into view, `true` by default
8080
* :html5 `Boolean` force HTML5 or legacy mouse drag
8181
* :drop_modifiers `Symbol` | `Array` modifier keys held on drop, e.g. `:shift`, `:ctrl`, `:alt`, `:meta`
82+
* :x, :y `Integer` drop at this offset instead of the center of `other`, both are required
83+
* :offset `:center` | `:top` where the offset is measured from, the center or the top left corner of `other`.
84+
Follows `Capybara.w3c_click_offset` by default, like `click`
8285

8386
```ruby
8487
find("#item").drag_to(find("#basket"), steps: 10, drop_modifiers: :shift)
88+
find("#item").drag_to(find("#list li", text: "Milk"), x: 0, y: 10) # drop on the lower half
8589
```
8690

8791
#### drag_by(x, y, \*\*options)

‎docs/cuprite/9-windows-frames.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ Returns inner width and height of the window.
5454
page.current_window.size # => [1024, 768]
5555
```
5656

57-
The window size is reset to `:window_size` at the end of every test.
57+
The window size is reset to `:window_size` at the end of every test. With `window_size: nil` Cuprite doesn't
58+
set the size, so Chrome's own one, or `start-maximized`, applies.
5859

5960
## Frames
6061

‎docs/ferrum/1-introduction.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ gem "ferrum"
3838

3939
## Docker
4040

41-
See [Docker](/docs/ferrum/docker).
41+
To skip installing Chrome, run it in a container with our images, `ghcr.io/rubycdp/chrome` or
42+
`ghcr.io/rubycdp/chromium`, and connect with `Ferrum::Browser.new(url: "http://localhost:3000")`. See
43+
[Docker](/docs/ferrum/docker) for that and for running Chrome in your own container.
4244

4345
## Quick Start
4446

‎docs/ferrum/15-frames.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Parent frame if this one is nested in another one.
5555

5656
#### frame_element : `Node | nil`
5757

58-
Returns the element in which the window is embedded.
58+
Returns the element in which the window is embedded. It's `nil` for the main
59+
frame and for a frame from a different origin than its parent, since the
60+
same-origin policy hides `window.frameElement` from a cross-origin frame.
5961

6062
#### execution_id : `Integer`
6163

‎docs/ferrum/22-docker.md‎

Lines changed: 148 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,148 @@ sidebar_position: 22
44

55
# Docker
66

7-
Chrome runs a bit differently in a container, in ways that are not obvious and that surface as
8-
slow leaks rather than errors. This page collects everything Ferrum-specific about it.
7+
There are two ways to use Chrome with Docker:
8+
9+
- **Our images**, `ghcr.io/rubycdp/chrome` and `ghcr.io/rubycdp/chromium`: Chrome runs in its own container and Ferrum
10+
connects to it. Use these unless you need Chrome inside your app's container. Process reaping, crashpad, fonts and
11+
flags are taken care of.
12+
- **Chrome in your own container**, next to your Ruby process. Chrome runs a bit differently in a container, in ways
13+
that are not obvious and that surface as slow leaks rather than errors, and [Running Chrome yourself](#running-chrome-yourself)
14+
covers all of it.
15+
16+
## Our images (recommended)
17+
18+
Two images, both for amd64 and arm64: `chromium` ships Debian's Chromium, `chrome` ships Google Chrome. Each
19+
connection gets its own Chrome with a fresh profile, which is stopped as soon as the connection closes. Nothing else
20+
talks to your browser: the driver inside passes CDP through unchanged.
21+
22+
Tags are `latest`, the major version (`155`) and the full version (`155.0.8059.84`). New browser releases are picked up
23+
daily. Older tags stay available, so you can pin a version.
24+
25+
### Quick start
26+
27+
```shell
28+
docker run --rm -p 3000:3000 ghcr.io/rubycdp/chromium
29+
```
30+
31+
```ruby
32+
browser = Ferrum::Browser.new(url: "http://localhost:3000")
33+
browser.go_to("https://example.com")
34+
browser.quit
35+
```
36+
37+
`ws_url: "ws://localhost:3000"` works too.
38+
39+
### Compose and Cuprite
40+
41+
```yaml
42+
services:
43+
chrome:
44+
image: ghcr.io/rubycdp/chromium
45+
environment:
46+
CONCURRENT: 4
47+
volumes:
48+
- .:/app
49+
50+
app:
51+
build: .
52+
volumes:
53+
- .:/app
54+
environment:
55+
CHROME_URL: http://chrome:3000
56+
depends_on:
57+
chrome:
58+
condition: service_healthy
59+
```
60+
61+
```ruby
62+
Capybara.register_driver(:cuprite) do |app|
63+
Capybara::Cuprite::Driver.new(app, url: ENV.fetch("CHROME_URL"))
64+
end
65+
66+
Capybara.server_host = "0.0.0.0"
67+
Capybara.server_port = 3001
68+
Capybara.app_host = "http://app:3001"
69+
```
70+
71+
Chrome has to reach your app, so bind the server to `0.0.0.0` and point `app_host` at the app's name on the compose
72+
network. When the app runs on your machine instead, use `host.docker.internal`, which needs
73+
`extra_hosts: ["host.docker.internal:host-gateway"]` on Linux.
74+
75+
Cuprite keeps one connection per test process and calls `reset` between tests, so every parallel worker gets its own
76+
Chrome. The image has a healthcheck on `/health`.
77+
78+
### Options per connection
79+
80+
Ferrum's `window_size:` and `browser_options:` only apply to a Chrome that Ferrum starts itself. For a remote one, put
81+
them in the connection URL as `launch`, JSON or base64:
82+
83+
```ruby
84+
launch = {
85+
defaultViewport: { width: 1920, height: 1080 },
86+
args: ["--accept-lang=de-DE"]
87+
}
88+
Ferrum::Browser.new(url: "http://localhost:3000?launch=#{CGI.escape(launch.to_json)}")
89+
```
90+
91+
- `args`: extra Chrome flags. A flag with the same name as a default replaces it.
92+
- `defaultViewport`: the window size, 1024x768 by default like Ferrum's.
93+
- `ignoreDefaultArgs`: `true`, or a list of default flags to drop.
94+
- `headless: false`: Chrome gets a window on its own virtual display.
95+
96+
`timeout=<ms>` in the same URL ends the session after that long. `docker run --rm ghcr.io/rubycdp/chromium --help`
97+
lists everything, including the default flags, which are Ferrum's.
98+
99+
### Settings
100+
101+
- `CONCURRENT`: how many sessions run at once, the number of CPUs by default. Set it to at least your number of test
102+
workers.
103+
- `QUEUED`: how many connections wait for a free slot, 10 by default. Past that they get `429`.
104+
- `TIMEOUT`: a session timeout in milliseconds for every connection. There is none by default.
105+
- `EXTERNAL`: the public URL when the container sits behind a proxy, so the WebSocket address it hands out is
106+
reachable.
107+
- `PORT` and `HOST`: where it listens, `0.0.0.0:3000` by default.
108+
109+
### Files
110+
111+
Chrome resolves file paths inside its own container. Mount the directories that uploads come from and downloads go to
112+
at the same absolute path in both containers, and `attach_file`, `select_file` and `save_path` work unchanged. Run the
113+
container as your user so downloaded files belong to you:
114+
115+
```shell
116+
docker run --rm -p 3000:3000 --user "$(id -u):$(id -g)" -v "$PWD:$PWD" ghcr.io/rubycdp/chromium
117+
```
118+
119+
Screenshots and PDFs need no mount, they come back over CDP.
120+
121+
### Watching a test
122+
123+
Open [http://localhost:3000](http://localhost:3000) while tests run. The dashboard lists running sessions and their
124+
pages, and each page links to DevTools with a live view of it. Pages go away when the test ends, so pause the test
125+
(`binding.irb`, a breakpoint) to look at one.
126+
127+
### Security
128+
129+
:::warning
130+
There is no authentication, and anyone who can connect can pass Chrome flags that run commands in the container. Never
131+
publish the port beyond localhost or a private network.
132+
:::
133+
134+
### Coming from browserless
135+
136+
Usually you swap the image and connect to `/`. The differences that matter:
137+
138+
- There's no session timeout by default, browserless kills sessions after 30 seconds.
139+
- There are no `/chrome` or `/chromium` paths, no tokens, and no REST APIs (`/screenshot`, `/pdf`, `/function`).
140+
Ferrum does all of that over CDP.
141+
- There's no `/workspace`, files go through mounts as described above.
142+
- Nothing resets your viewport or download folder, and `reset` really isolates tests.
143+
144+
The full comparison and the reasons behind it are in the [design notes](https://github.com/rubycdp/ferrum/blob/main/docker/DESIGN.md).
145+
146+
## Running Chrome yourself
147+
148+
### In your app's container
9149

10150
:::note
11151
When running in docker as root
@@ -18,7 +158,7 @@ Ferrum::Browser.new(dockerize: true)
18158
Essentially it just sets CLI flags for a browser to make it start. On CI, you can just set `FERRUM_CHROME_DOCKERIZE=true`
19159
environment variable, and it will be passed to all browser instances.
20160

21-
## An init is required
161+
### An init is required
22162

23163
:::warning
24164
If your Ruby process is pid 1, run the container with an init: `docker run --init`, `init: true` in Compose, or
@@ -63,7 +203,7 @@ its full kill timeout.
63203

64204
An init fixes both, and is worth having regardless: every other process your image spawns has the same problem.
65205

66-
## Kubernetes
206+
### Kubernetes
67207

68208
There is no `init: true` in a pod spec, so you have two options.
69209

@@ -83,7 +223,7 @@ also stops being pid 1, so anyone doing `kill -HUP 1` now signals `pause`.
83223
`initContainers` are not an init system.
84224
:::
85225

86-
## What Ferrum cleans up, and what it does not
226+
### What Ferrum cleans up, and what it does not
87227

88228
Ferrum spawns exactly one process: the browser. On `#quit` it signals the browser's process group, escalating from
89229
`TERM` to `KILL`, and waits on the browser itself — the one process it is the parent of, and therefore the only one
@@ -98,9 +238,9 @@ library cannot take that liberty, which is why the answer here is an init rather
98238
happens, so it is not even in the process group Ferrum signals. See [the crashpad handler](/docs/ferrum/customization#the-crashpad-handler),
99239
including why the flag that appears to disable it must not be used.
100240

101-
## Running Chrome in a separate container
241+
### Another remote browser service
102242

103-
If Chrome lives in its own container and Ferrum connects over a websocket, Ferrum did not spawn that browser and
243+
Any service that speaks CDP works the same way, with `url:` or `ws_url:`. Ferrum did not spawn that browser and
104244
couldn't kill it:
105245

106246
```ruby
@@ -116,7 +256,7 @@ browser.close
116256
browser.quit
117257
```
118258

119-
## Diagnosing
259+
### Diagnosing
120260

121261
Check whether the processes piling up are dead or alive:
122262

‎projects/ferrum‎

Submodule ferrum updated 57 files

0 commit comments

Comments
 (0)