playwright-ruby-client 0.6.0 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/documentation/README.md +33 -0
- data/documentation/babel.config.js +3 -0
- data/documentation/docs/api/accessibility.md +7 -0
- data/documentation/docs/api/browser.md +188 -0
- data/documentation/docs/api/browser_context.md +397 -0
- data/documentation/docs/api/browser_type.md +158 -0
- data/documentation/docs/api/cdp_session.md +7 -0
- data/documentation/docs/api/console_message.md +41 -0
- data/documentation/docs/api/dialog.md +71 -0
- data/documentation/docs/api/element_handle.md +618 -0
- data/documentation/docs/api/experimental/_category_.yml +3 -0
- data/documentation/docs/api/experimental/android.md +26 -0
- data/documentation/docs/api/experimental/android_device.md +92 -0
- data/documentation/docs/api/experimental/android_input.md +38 -0
- data/documentation/docs/api/experimental/android_socket.md +7 -0
- data/documentation/docs/api/experimental/android_web_view.md +7 -0
- data/documentation/docs/api/file_chooser.md +50 -0
- data/documentation/docs/api/frame.md +866 -0
- data/documentation/docs/api/js_handle.md +113 -0
- data/documentation/docs/api/keyboard.md +157 -0
- data/documentation/docs/api/mouse.md +69 -0
- data/documentation/docs/api/page.md +1402 -0
- data/documentation/docs/api/playwright.md +63 -0
- data/documentation/docs/api/request.md +188 -0
- data/documentation/docs/api/response.md +97 -0
- data/documentation/docs/api/route.md +79 -0
- data/documentation/docs/api/selectors.md +23 -0
- data/documentation/docs/api/touchscreen.md +8 -0
- data/documentation/docs/api/tracing.md +47 -0
- data/documentation/docs/api/web_socket.md +7 -0
- data/documentation/docs/api/worker.md +24 -0
- data/documentation/docs/article/api_coverage.mdx +11 -0
- data/documentation/docs/article/getting_started.md +152 -0
- data/documentation/docs/article/guides/_category_.yml +3 -0
- data/documentation/docs/article/guides/download_playwright_driver.md +49 -0
- data/documentation/docs/article/guides/launch_browser.md +119 -0
- data/documentation/docs/article/guides/rails_integration.md +205 -0
- data/documentation/docs/article/guides/recording_video.md +79 -0
- data/{docs → documentation/docs/include}/api_coverage.md +4 -3
- data/documentation/docusaurus.config.js +107 -0
- data/documentation/package.json +39 -0
- data/documentation/sidebars.js +15 -0
- data/documentation/src/components/HomepageFeatures.js +61 -0
- data/documentation/src/components/HomepageFeatures.module.css +13 -0
- data/documentation/src/css/custom.css +44 -0
- data/documentation/src/pages/index.js +50 -0
- data/documentation/src/pages/index.module.css +41 -0
- data/documentation/src/pages/markdown-page.md +7 -0
- data/documentation/static/.nojekyll +0 -0
- data/documentation/static/img/playwright-logo.svg +9 -0
- data/documentation/static/img/undraw_dropdown_menu.svg +1 -0
- data/documentation/static/img/undraw_web_development.svg +1 -0
- data/documentation/static/img/undraw_windows.svg +1 -0
- data/documentation/yarn.lock +8785 -0
- data/lib/playwright/channel_owners/binding_call.rb +33 -0
- data/lib/playwright/channel_owners/browser.rb +15 -27
- data/lib/playwright/channel_owners/browser_context.rb +15 -7
- data/lib/playwright/channel_owners/browser_type.rb +23 -8
- data/lib/playwright/channel_owners/element_handle.rb +2 -10
- data/lib/playwright/channel_owners/frame.rb +6 -28
- data/lib/playwright/channel_owners/js_handle.rb +2 -10
- data/lib/playwright/channel_owners/page.rb +18 -8
- data/lib/playwright/channel_owners/worker.rb +4 -0
- data/lib/playwright/input_files.rb +0 -8
- data/lib/playwright/javascript.rb +0 -10
- data/lib/playwright/javascript/expression.rb +2 -7
- data/lib/playwright/playwright_api.rb +16 -1
- data/lib/playwright/tracing_impl.rb +9 -9
- data/lib/playwright/version.rb +1 -1
- data/lib/playwright_api/accessibility.rb +7 -89
- data/lib/playwright_api/android.rb +10 -66
- data/lib/playwright_api/android_device.rb +10 -9
- data/lib/playwright_api/browser.rb +21 -172
- data/lib/playwright_api/browser_context.rb +54 -617
- data/lib/playwright_api/browser_type.rb +20 -108
- data/lib/playwright_api/cdp_session.rb +2 -25
- data/lib/playwright_api/console_message.rb +6 -6
- data/lib/playwright_api/dialog.rb +11 -92
- data/lib/playwright_api/element_handle.rb +60 -362
- data/lib/playwright_api/file_chooser.rb +0 -28
- data/lib/playwright_api/frame.rb +74 -713
- data/lib/playwright_api/js_handle.rb +16 -90
- data/lib/playwright_api/keyboard.rb +21 -213
- data/lib/playwright_api/mouse.rb +1 -45
- data/lib/playwright_api/page.rb +181 -1647
- data/lib/playwright_api/playwright.rb +14 -117
- data/lib/playwright_api/request.rb +15 -121
- data/lib/playwright_api/response.rb +7 -7
- data/lib/playwright_api/route.rb +8 -105
- data/lib/playwright_api/selectors.rb +6 -97
- data/lib/playwright_api/tracing.rb +7 -73
- data/lib/playwright_api/web_socket.rb +1 -1
- data/lib/playwright_api/worker.rb +28 -42
- metadata +57 -4
- data/lib/playwright/javascript/function.rb +0 -67
@@ -0,0 +1,8 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Touchscreen
|
6
|
+
|
7
|
+
The Touchscreen class operates in main-frame CSS pixels relative to the top-left corner of the viewport. Methods on the
|
8
|
+
touchscreen can only be used in browser contexts that have been initialized with `hasTouch` set to true.
|
@@ -0,0 +1,47 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Tracing
|
6
|
+
|
7
|
+
API for collecting and saving Playwright traces. Playwright traces can be opened using the Playwright CLI after
|
8
|
+
Playwright script runs.
|
9
|
+
|
10
|
+
Start with specifying the folder traces will be stored in:
|
11
|
+
|
12
|
+
```python sync title=example_a767dfb400d98aef50f2767b94171d23474ea1ac1cf9b4d75d412936208e652d.py
|
13
|
+
browser = chromium.launch()
|
14
|
+
context = browser.new_context()
|
15
|
+
context.tracing.start(screenshots=True, snapshots=True)
|
16
|
+
page.goto("https://playwright.dev")
|
17
|
+
context.tracing.stop(path = "trace.zip")
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
## start
|
24
|
+
|
25
|
+
```
|
26
|
+
def start(name: nil, screenshots: nil, snapshots: nil)
|
27
|
+
```
|
28
|
+
|
29
|
+
Start tracing.
|
30
|
+
|
31
|
+
```python sync title=example_e611abc8b1066118d0c87eae1bbbb08df655f36d50a94402fc56b8713150997b.py
|
32
|
+
context.tracing.start(name="trace", screenshots=True, snapshots=True)
|
33
|
+
page.goto("https://playwright.dev")
|
34
|
+
context.tracing.stop()
|
35
|
+
context.tracing.stop(path = "trace.zip")
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
## stop
|
42
|
+
|
43
|
+
```
|
44
|
+
def stop(path: nil)
|
45
|
+
```
|
46
|
+
|
47
|
+
Stop tracing.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Worker
|
6
|
+
|
7
|
+
The Worker class represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API). `worker`
|
8
|
+
event is emitted on the page object to signal a worker creation. `close` event is emitted on the worker object when the
|
9
|
+
worker is gone.
|
10
|
+
|
11
|
+
```py title=example_29716fdd4471a97923a64eebeee96330ab508226a496ae8fd13f12eb07d55ee6.py
|
12
|
+
def handle_worker(worker):
|
13
|
+
print("worker created: " + worker.url)
|
14
|
+
worker.on("close", lambda: print("worker destroyed: " + worker.url))
|
15
|
+
|
16
|
+
page.on('worker', handle_worker)
|
17
|
+
|
18
|
+
print("current workers:")
|
19
|
+
for worker in page.workers:
|
20
|
+
print(" " + worker.url)
|
21
|
+
|
22
|
+
```
|
23
|
+
|
24
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
---
|
2
|
+
position: 20
|
3
|
+
---
|
4
|
+
|
5
|
+
# API Coverages
|
6
|
+
|
7
|
+
`playwright-ruby-client` doesn't cover all of the Playwright APIs. The APIs listed below without strikethrough are available in `playwright-ruby-client`.
|
8
|
+
|
9
|
+
import ApiCoverage from '../include/api_coverage.md'
|
10
|
+
|
11
|
+
<ApiCoverage />
|
@@ -0,0 +1,152 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 0
|
3
|
+
---
|
4
|
+
|
5
|
+
# Getting started
|
6
|
+
|
7
|
+
`playwright-ruby-client` doesn't include Playwright driver nor its downloader. **We have to install Playwright in advance**
|
8
|
+
|
9
|
+
```shell
|
10
|
+
$ npx playwright install
|
11
|
+
```
|
12
|
+
|
13
|
+
and then set `playwright_cli_executable_path: "npx playwright"` into `Playwright.create`.
|
14
|
+
|
15
|
+
## Enjoy with examples
|
16
|
+
|
17
|
+
### Capture a site
|
18
|
+
|
19
|
+
Navigate pages with `page.goto(url)` and save the screenshot with `page.screenshot(path: './image.png')`.
|
20
|
+
|
21
|
+
```rb {6-7}
|
22
|
+
require 'playwright'
|
23
|
+
|
24
|
+
Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
|
25
|
+
playwright.chromium.launch(headless: false) do |browser|
|
26
|
+
page = browser.new_page
|
27
|
+
page.goto('https://github.com/YusukeIwaki')
|
28
|
+
page.screenshot(path: './YusukeIwaki.png')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+

|
34
|
+
|
35
|
+
### Simple scraping
|
36
|
+
|
37
|
+
Extract data from a site.
|
38
|
+
|
39
|
+
* Grab DOM elements with `page.query_selector(loc)` and `page.query_selector_all(loc)`
|
40
|
+
* Extract data with `elem.evaluate(js)` or `page.eval_on_selector(js)`
|
41
|
+
* Wait for navigation with `page.expect_navigation do ... end`
|
42
|
+
|
43
|
+
```rb {12-14,17-21}
|
44
|
+
require 'playwright'
|
45
|
+
|
46
|
+
Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
|
47
|
+
playwright.chromium.launch(headless: false) do |browser|
|
48
|
+
page = browser.new_page
|
49
|
+
page.goto('https://github.com/')
|
50
|
+
|
51
|
+
form = page.query_selector("form.js-site-search-form")
|
52
|
+
search_input = form.query_selector("input.header-search-input")
|
53
|
+
search_input.click
|
54
|
+
page.keyboard.type("playwright")
|
55
|
+
page.expect_navigation {
|
56
|
+
page.keyboard.press("Enter")
|
57
|
+
}
|
58
|
+
|
59
|
+
list = page.query_selector("ul.repo-list")
|
60
|
+
items = list.query_selector_all("div.f4")
|
61
|
+
items.each do |item|
|
62
|
+
title = item.eval_on_selector("a", "a => a.innerText")
|
63
|
+
puts("==> #{title}")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
```
|
70
|
+
$ bundle exec ruby main.rb
|
71
|
+
==> microsoft/playwright
|
72
|
+
==> microsoft/playwright-python
|
73
|
+
==> microsoft/playwright-cli
|
74
|
+
==> checkly/headless-recorder
|
75
|
+
==> microsoft/playwright-sharp
|
76
|
+
==> playwright-community/jest-playwright
|
77
|
+
==> microsoft/playwright-test
|
78
|
+
==> mxschmitt/playwright-go
|
79
|
+
==> microsoft/playwright-java
|
80
|
+
==> MarketSquare/robotframework-browser
|
81
|
+
```
|
82
|
+
|
83
|
+
### Android browser automation
|
84
|
+
|
85
|
+
As an experimental feature, we can automate Chrome for Android.
|
86
|
+
|
87
|
+
```rb
|
88
|
+
require 'playwright'
|
89
|
+
|
90
|
+
Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
|
91
|
+
devices = playwright.android.devices
|
92
|
+
unless devices.empty?
|
93
|
+
device = devices.last
|
94
|
+
begin
|
95
|
+
puts "Model: #{device.model}"
|
96
|
+
puts "Serial: #{device.serial}"
|
97
|
+
puts device.shell('ls /system')
|
98
|
+
|
99
|
+
device.launch_browser do |context|
|
100
|
+
page = context.pages.first
|
101
|
+
page.goto('https://github.com/YusukeIwaki')
|
102
|
+
page.click('header button')
|
103
|
+
page.click('input[name="q"]')
|
104
|
+
page.keyboard.type('puppeteer')
|
105
|
+
page.expect_navigation {
|
106
|
+
page.keyboard.press('Enter')
|
107
|
+
}
|
108
|
+
page.screenshot(path: 'YusukeIwaki.android.png')
|
109
|
+
end
|
110
|
+
ensure
|
111
|
+
device.close
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+

|
118
|
+
|
119
|
+
### Android native automation
|
120
|
+
|
121
|
+
We have to download android-driver for Playwright in advance.
|
122
|
+
|
123
|
+
```shell
|
124
|
+
$ wget https://github.com/microsoft/playwright/raw/master/bin/android-driver-target.apk -O /path/to/playwright-driver/package/bin/android-driver-target.apk
|
125
|
+
$ wget https://github.com/microsoft/playwright/raw/master/bin/android-driver.apk -O /path/to/playwright-driver/package/bin/android-driver.apk
|
126
|
+
```
|
127
|
+
|
128
|
+
(If you downloaded Playwright via npm, replace /path/to/playwright-driver/package/ with ./node_modules/playwright/ above.)
|
129
|
+
|
130
|
+
```rb
|
131
|
+
require 'playwright'
|
132
|
+
|
133
|
+
Playwright.create(playwright_cli_executable_path: ENV['PLAYWRIGHT_CLI_EXECUTABLE_PATH']) do |playwright|
|
134
|
+
devices = playwright.android.devices
|
135
|
+
unless devices.empty?
|
136
|
+
device = devices.last
|
137
|
+
begin
|
138
|
+
device.shell('input keyevent POWER')
|
139
|
+
device.shell('input keyevent POWER')
|
140
|
+
device.shell('input keyevent 82')
|
141
|
+
sleep 1
|
142
|
+
device.shell('cmd statusbar expand-notifications')
|
143
|
+
|
144
|
+
# pp device.tree
|
145
|
+
# pp device.info(res: 'com.android.systemui:id/clock')
|
146
|
+
device.tap_on(res: 'com.android.systemui:id/clock')
|
147
|
+
ensure
|
148
|
+
device.close
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
```
|
@@ -0,0 +1,49 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
---
|
4
|
+
|
5
|
+
# Download Playwright driver
|
6
|
+
|
7
|
+
`playwright-ruby-client` doesn't include Playwright driver nor its downloader. **We have to install Playwright in advance**, and set the proper CLI path like `Playwright.create(playwright_cli_executable_path: /path/to/playwright)`
|
8
|
+
|
9
|
+
Choose any of the three ways as you prefer to download the driver:
|
10
|
+
|
11
|
+
* `npx`: suitable for playground use, and not suitable for continuous usage.
|
12
|
+
* `npm install`: the best choice for most use cases, with existing Node.js environment.
|
13
|
+
* Direct download: maybe a good choice for Docker :whale: integration.
|
14
|
+
|
15
|
+
## Using `npx`
|
16
|
+
|
17
|
+
```shell
|
18
|
+
$ npx playwright install
|
19
|
+
```
|
20
|
+
|
21
|
+
and then set `playwright_cli_executable_path: "npx playwright"` at `Playwright.create`.
|
22
|
+
|
23
|
+
|
24
|
+
## Using `npm install`
|
25
|
+
|
26
|
+
Actually `npx playwright` is a bit slow. We can also use `npm install` to setup.
|
27
|
+
|
28
|
+
Instead of `npx playwright install`:
|
29
|
+
|
30
|
+
```shell
|
31
|
+
$ export PLAYWRIGHT_CLI_VERSION=$(bundle exec ruby -e 'puts Playwright::COMPATIBLE_PLAYWRIGHT_VERSION.strip')
|
32
|
+
$ npm install playwright@$PLAYWRIGHT_CLI_VERSION || npm install playwright@next
|
33
|
+
$ ./node_modules/.bin/playwright install
|
34
|
+
```
|
35
|
+
|
36
|
+
and then set `playwright_cli_executable_path: './node_modules/.bin/playwright'`
|
37
|
+
|
38
|
+
|
39
|
+
## Directly download driver without Node.js installation.
|
40
|
+
|
41
|
+
Instead of npm, you can also directly download playwright driver from playwright.azureedge.net. (The URL can be easily detected from [here](https://github.com/microsoft/playwright-python/blob/cfc1030a69d1e934cac579687a680eac53d4b9ee/setup.py#L75))
|
42
|
+
|
43
|
+
|
44
|
+
```shell
|
45
|
+
$ export PLAYWRIGHT_CLI_VERSION=$(bundle exec ruby -e 'puts Playwright::COMPATIBLE_PLAYWRIGHT_VERSION.strip')
|
46
|
+
$ wget https://playwright.azureedge.net/builds/driver/playwright-$PLAYWRIGHT_CLI_VERSION-linux.zip
|
47
|
+
```
|
48
|
+
|
49
|
+
and then extract it, and set `playwright_cli_executable_path: '/path/to/playwright-1.11.0-linux/playwright.sh'`
|
@@ -0,0 +1,119 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
---
|
4
|
+
|
5
|
+
# Launch Browser
|
6
|
+
|
7
|
+

|
8
|
+
|
9
|
+
## Create Playwright session
|
10
|
+
|
11
|
+
In oder to launch browser, it is required to create Playwright session.
|
12
|
+
|
13
|
+
In previous examples,
|
14
|
+
|
15
|
+
```rb
|
16
|
+
Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
|
17
|
+
# Play with `playwright` here
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
this is the exact procedure for creating Playwright session. Choose either of method for creating the session.
|
22
|
+
|
23
|
+
### Define scoped Playwright session with block
|
24
|
+
|
25
|
+
```rb
|
26
|
+
Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
|
27
|
+
# Play with `playwright` here
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
As is described repetedly, this is the recommended way for creating Playwright session. Even when any exception happens, Playwright session is safely ended on leaving the block.
|
32
|
+
|
33
|
+
Internally `playwright run-driver` session is alive during the block.
|
34
|
+
|
35
|
+
### Define start/end of the Playwright session separately without block.
|
36
|
+
|
37
|
+
Sometimes we have to define separated start/end definition. `playwright-ruby-client` also allows it.
|
38
|
+
|
39
|
+
```rb
|
40
|
+
class SomeClass
|
41
|
+
|
42
|
+
def start_playwright
|
43
|
+
# Start Playwright driver (runs `playwright run-driver` internally)
|
44
|
+
@playwright_exec = Playwright.create(playwright_cli_executable_path: 'npx playwright')
|
45
|
+
end
|
46
|
+
|
47
|
+
def stop_playwright!
|
48
|
+
# Stop Playwright driver
|
49
|
+
@playwright_exec.stop
|
50
|
+
end
|
51
|
+
|
52
|
+
def play_with_playwright(&blk)
|
53
|
+
# Acquire Playwright instance with PlaywrightExecution#playwright
|
54
|
+
playwright = @playwright_exec.playwright
|
55
|
+
|
56
|
+
browser = playwright.chromium.launch
|
57
|
+
|
58
|
+
begin
|
59
|
+
blk.call(browser)
|
60
|
+
ensure
|
61
|
+
browser.close
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
Note that we have to make sure to call `PlaywrightExecution#stop` (`stop_playwright!` in this case).
|
68
|
+
|
69
|
+
## Create browser instance
|
70
|
+
|
71
|
+
Playwright allows launching several browsers.
|
72
|
+
|
73
|
+
* `playwright.chromium.launch` for launching Chrome, Microsoft Edge, Chromium
|
74
|
+
* `playwright.firefox.launch` for launching (modified version of) Firefox
|
75
|
+
* `playwright.webkit.launch` for launching (modified version of) WebKit/Safari
|
76
|
+
|
77
|
+
It is recommended in most cases to launch browser with block, which automatically closes the launched browser on end.
|
78
|
+
|
79
|
+
```rb
|
80
|
+
# scoped browser block
|
81
|
+
playwright.chromium.launch do |browser|
|
82
|
+
# Play with `browser`
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
Of course, you can manually call `Browser#close` when the browser is launched without block.
|
87
|
+
|
88
|
+
```rb
|
89
|
+
# separated start/end
|
90
|
+
browser = playwright.chromium.launch
|
91
|
+
|
92
|
+
begin
|
93
|
+
# Play with `@browser`
|
94
|
+
ensure
|
95
|
+
browser.close
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
## Open new window and new tab
|
100
|
+
|
101
|
+
Use `Browser#new_context` to prepare a new browser window and use `BrowserContext#new_page` to create a new tab.
|
102
|
+
Also we can use `Browser#new_page` to create a new window and new tab at once.
|
103
|
+
|
104
|
+
```rb
|
105
|
+
Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
|
106
|
+
playwright.chromium.launch(headless: false) do |browser| # Chromium task icon appers in.
|
107
|
+
context = browser.new_context # Prepare new window.
|
108
|
+
page = context.new_page # Open new window and new tab here. (about:blank)
|
109
|
+
page.goto('https://example.com') # Navigate to a site.
|
110
|
+
|
111
|
+
page2 = context.new_page # Open another tab here.
|
112
|
+
page2.goto('https://example.com') # Navigate to a site.
|
113
|
+
|
114
|
+
another_context = browser.new_context # Prepare another window here.
|
115
|
+
another_page = another_context.new_page # Open a new window and new tab.
|
116
|
+
another_page.goto('https://example.com/') # Navigate to a site.
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
@@ -0,0 +1,205 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 3
|
3
|
+
---
|
4
|
+
|
5
|
+
# Integration into Ruby on Rails application
|
6
|
+
|
7
|
+
`playwright-ruby-client` is a client library just for browser automation. `capybara-playwright-driver` provides the [Capybara](https://github.com/teamcapybara/capybara) driver based on playwright-ruby-client and makes it easy to integrate into Ruby on Rails applications.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add the line below into Gemfile:
|
12
|
+
|
13
|
+
```rb
|
14
|
+
gem 'capybara-playwright-driver'
|
15
|
+
```
|
16
|
+
|
17
|
+
and then `bundle install`.
|
18
|
+
|
19
|
+
Note that capybara-playwright-driver does not depend on Selenium. But `selenium-webdriver` is also required [on Rails 5.x, 6.0](https://github.com/rails/rails/pull/39179)
|
20
|
+
|
21
|
+
## Register and configure Capybara driver
|
22
|
+
|
23
|
+
```rb
|
24
|
+
Capybara.register_driver(:playwright) do |app|
|
25
|
+
Capybara::Playwright::Driver.new(app,
|
26
|
+
browser_type: :chromium, # :chromium (default) or :firefox, :webkit
|
27
|
+
headless: false, # true for headless mode (default), false for headful mode.
|
28
|
+
)
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
### Update timeout
|
33
|
+
|
34
|
+
Capybara sets the default value of timeout to *2 seconds*. Generally it is too short to wait for HTTP responses.
|
35
|
+
|
36
|
+
It is recommended to set the timeout to 15-30 seconds for Playwright driver.
|
37
|
+
|
38
|
+
```rb
|
39
|
+
Capybara.default_max_wait_time = 15
|
40
|
+
```
|
41
|
+
|
42
|
+
### (Optional) Update default driver
|
43
|
+
|
44
|
+
By default, Capybara driver is set to `:rack_test`, which works only with non-JS contents. If your Rails application has many JavaScript contents, it is recommended to change the default driver to `:playwrite`.
|
45
|
+
|
46
|
+
```rb
|
47
|
+
Capybara.default_driver = :playwright
|
48
|
+
Capybara.javascript_driver = :playwright
|
49
|
+
```
|
50
|
+
|
51
|
+
It is not mandatry. Without changing the default driver, you can still use Playwright driver by specifying `Capybara.current_driver = :playwright` (or `driven_by :playwright` in system spec) explicitly.
|
52
|
+
|
53
|
+
### (reference) Available driver options
|
54
|
+
|
55
|
+
These parameters can be passed into `Capybara::Playwright::Driver.new`
|
56
|
+
|
57
|
+
* `playwright_cli_executable_path`
|
58
|
+
* Refer [this article](./download_playwright_driver) to understand what to specify.
|
59
|
+
* `browser_type`
|
60
|
+
* `:chromium` (default), `:firefox`, or `:webkit`
|
61
|
+
* Parameters for [Playwright::BrowserType#launch](/docs/api/browser_type#launch)
|
62
|
+
* args
|
63
|
+
* channel
|
64
|
+
* `chrome`, `msedge`, `chrome-beta`, `chrome-dev`, `chrome-canary`, `msedge-beta`, `msedge-dev` Browser distribution channel. Read more about using [Google Chrome & Microsoft Edge](https://playwright.dev/docs/browsers#google-chrome--microsoft-edge)
|
65
|
+
* devtools
|
66
|
+
* downloadsPath
|
67
|
+
* env
|
68
|
+
* executablePath
|
69
|
+
* firefoxUserPrefs
|
70
|
+
* headless
|
71
|
+
* ignoreDefaultArgs
|
72
|
+
* proxy
|
73
|
+
* slowMo
|
74
|
+
* timeout
|
75
|
+
* Parameters for [Playwright::Browser#new_context](/docs/api/browser#new_context)
|
76
|
+
* bypassCSP
|
77
|
+
* colorScheme
|
78
|
+
* deviceScaleFactor
|
79
|
+
* extraHTTPHeaders
|
80
|
+
* geolocation
|
81
|
+
* hasTouch
|
82
|
+
* httpCredentials
|
83
|
+
* ignoreHTTPSErrors
|
84
|
+
* isMobile
|
85
|
+
* javaScriptEnabled
|
86
|
+
* locale
|
87
|
+
* noViewport
|
88
|
+
* offline
|
89
|
+
* permissions
|
90
|
+
* proxy
|
91
|
+
* record_har_omit_content
|
92
|
+
* record_har_path
|
93
|
+
* record_video_dir
|
94
|
+
* record_video_size
|
95
|
+
* screen
|
96
|
+
* storageState
|
97
|
+
* timezoneId
|
98
|
+
* userAgent
|
99
|
+
* viewport
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
driver_opts = {
|
103
|
+
# `playwright` command path.
|
104
|
+
playwright_cli_executable_path: './node_modules/.bin/playwright',
|
105
|
+
|
106
|
+
# Use firefox for testing.
|
107
|
+
browser_type: :firefox,
|
108
|
+
|
109
|
+
# Headful mode.
|
110
|
+
headless: false,
|
111
|
+
|
112
|
+
# Slower operation
|
113
|
+
slowMo: 50, # integer. (50-100 would be good for most cases)
|
114
|
+
}
|
115
|
+
|
116
|
+
Capybara::Playwright::Driver.new(app, driver_opts)
|
117
|
+
```
|
118
|
+
|
119
|
+
|
120
|
+
## Available functions and Limitations
|
121
|
+
|
122
|
+
### Capybara DSL
|
123
|
+
|
124
|
+
Most of the methods of `Capybara::Session` and `Capybara::Node::Element` are available. However the following method is not yet implemented.
|
125
|
+
|
126
|
+
* `Capybara::Node::Element#drop`
|
127
|
+
|
128
|
+
### Playwright-native scripting
|
129
|
+
|
130
|
+
We can also describe Playwright-native automation script using `with_playwright_page` and `with_playwright_element_handle`.
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
# With Capybara DSL
|
134
|
+
find('a[data-item-type="global_search"]').click
|
135
|
+
|
136
|
+
# With Playwright-native Page
|
137
|
+
Capybara.current_session.driver.with_playwright_page do |page|
|
138
|
+
# `page` is an instance of Playwright::Page.
|
139
|
+
page.click('a[data-item-type="global_search"]')
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
all('.list-item').each do |li|
|
145
|
+
# With Capybara::Node::Element method
|
146
|
+
puts li.all('a').first.text
|
147
|
+
|
148
|
+
# With Playwright-native ElementHandle
|
149
|
+
puts li.with_playwright_element_handle do |handle|
|
150
|
+
# `handle` is an instance of Playwright::ElementHandle
|
151
|
+
handle.query_selector('a').text_content
|
152
|
+
end
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
Generally, Capybara DSL seems simple, but Playwright-native scripting are more precise and efficient. Also `waitForNavigation`, `waitForSelector`, and many other Playwright functions are available with Playwright-native scripting.
|
157
|
+
|
158
|
+
### Screen recording
|
159
|
+
|
160
|
+
NO NEED to keep sitting in front of screen during test. Just record what happened with video.
|
161
|
+
|
162
|
+
For example, we can store the video for [Allure report](https://github.com/allure-framework/allure-ruby) as below:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
before do |example|
|
166
|
+
Capybara.current_session.driver.on_save_screenrecord do |video_path|
|
167
|
+
Allure.add_attachment(
|
168
|
+
name: "screenrecord - #{example.description}",
|
169
|
+
source: File.read(video_path),
|
170
|
+
type: Allure::ContentType::WEBM,
|
171
|
+
test_case: true,
|
172
|
+
)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+

|
178
|
+
|
179
|
+
For more details, refer [Recording video](./recording_video.md#using-screen-recording-from-capybara-driver)
|
180
|
+
|
181
|
+
|
182
|
+
### Screenshot just before teardown
|
183
|
+
|
184
|
+
In addition to `Capybara::Session#save_screenshot`, capybara-playwright-driver have another method for storing last screen state just before teardown.
|
185
|
+
|
186
|
+
For example, we can attach the screenshot for [Allure report](https://github.com/allure-framework/allure-ruby) as below:
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
before do |example|
|
190
|
+
Capybara.current_session.driver.on_save_raw_screenshot_before_reset do |raw_screenshot|
|
191
|
+
Allure.add_attachment(
|
192
|
+
name: "screenshot - #{example.description}",
|
193
|
+
source: raw_screenshot,
|
194
|
+
type: Allure::ContentType::PNG,
|
195
|
+
test_case: true,
|
196
|
+
)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
### Limitations
|
202
|
+
|
203
|
+
* Playwright doesn't allow clicking invisible DOM elements or moving elements. `click` sometimes doesn't work as Selenium does. See the detail in https://playwright.dev/docs/actionability/
|
204
|
+
* `current_window.maximize` and `current_window.fullscreen` work only on headful (non-headless) mode, as selenium driver does.
|
205
|
+
* `Capybara::Node::Element#drag_to` does not accept `html5` parameter.
|