playwright-ruby-client 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- 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 +185 -0
- data/documentation/docs/api/browser_context.md +398 -0
- data/documentation/docs/api/browser_type.md +105 -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 +74 -0
- data/documentation/docs/api/element_handle.md +640 -0
- data/documentation/docs/api/experimental/_category_.yml +3 -0
- data/documentation/docs/api/experimental/android.md +25 -0
- data/documentation/docs/api/experimental/android_device.md +91 -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 +51 -0
- data/documentation/docs/api/frame.md +867 -0
- data/documentation/docs/api/js_handle.md +116 -0
- data/documentation/docs/api/keyboard.md +157 -0
- data/documentation/docs/api/mouse.md +69 -0
- data/documentation/docs/api/page.md +1469 -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 +80 -0
- data/documentation/docs/api/selectors.md +23 -0
- data/documentation/docs/api/touchscreen.md +8 -0
- data/documentation/docs/api/tracing.md +54 -0
- data/documentation/docs/api/web_socket.md +7 -0
- data/documentation/docs/api/worker.md +7 -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 +51 -0
- data/{docs → documentation/docs/include}/api_coverage.md +0 -0
- 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 +8805 -0
- data/lib/playwright/channel_owners/binding_call.rb +33 -0
- data/lib/playwright/channel_owners/browser_context.rb +2 -2
- 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 +10 -1
- 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/version.rb +1 -1
- data/lib/playwright_api/accessibility.rb +7 -89
- data/lib/playwright_api/android.rb +7 -64
- data/lib/playwright_api/android_device.rb +8 -8
- data/lib/playwright_api/browser.rb +15 -169
- data/lib/playwright_api/browser_context.rb +47 -609
- data/lib/playwright_api/browser_type.rb +13 -103
- 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 +155 -1635
- data/lib/playwright_api/playwright.rb +14 -117
- data/lib/playwright_api/request.rb +15 -121
- data/lib/playwright_api/response.rb +9 -9
- data/lib/playwright_api/route.rb +8 -105
- data/lib/playwright_api/selectors.rb +6 -97
- data/lib/playwright_api/tracing.rb +1 -61
- data/lib/playwright_api/web_socket.rb +1 -1
- data/lib/playwright_api/worker.rb +6 -42
- metadata +55 -4
- data/lib/playwright/javascript/function.rb +0 -67
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b365f47558ade3fcf3d803a070cbd61eb4eae6279f0d93c1641a53d9b50ec232
|
4
|
+
data.tar.gz: 353de7a190205c6d3d4542b3058afe24c0c634fe70811c15384b55d1cb464571
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec905a5051716af754993c11b5a484e5bf69a9b50cdb6681f583869d0cf70d23eb3265507d6739b6d006fc8419360dea8570f6fae6fc3ac8fdd6d44ae3547fd8
|
7
|
+
data.tar.gz: cf06e3351a40457108080dea85af6c8e417ffda3bec6671f9280ad42bc5fde7e58cf039016fe11387a7cb3e1347e1391f1440d23c2e54e512ca70e39ba90db38
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Website
|
2
|
+
|
3
|
+
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```console
|
8
|
+
yarn install
|
9
|
+
```
|
10
|
+
|
11
|
+
## Local Development
|
12
|
+
|
13
|
+
```console
|
14
|
+
yarn start
|
15
|
+
```
|
16
|
+
|
17
|
+
This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.
|
18
|
+
|
19
|
+
## Build
|
20
|
+
|
21
|
+
```console
|
22
|
+
yarn build
|
23
|
+
```
|
24
|
+
|
25
|
+
This command generates static content into the `build` directory and can be served using any static contents hosting service.
|
26
|
+
|
27
|
+
## Deployment
|
28
|
+
|
29
|
+
```console
|
30
|
+
GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
|
31
|
+
```
|
32
|
+
|
33
|
+
If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
|
@@ -0,0 +1,185 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Browser
|
6
|
+
|
7
|
+
- extends: [EventEmitter]
|
8
|
+
|
9
|
+
A Browser is created via [BrowserType#launch](./browser_type#launch). An example of using a [Browser](./browser) to create a [Page](./page):
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
firefox = playwright.firefox
|
13
|
+
browser = firefox.launch
|
14
|
+
begin
|
15
|
+
page = browser.new_page
|
16
|
+
page.goto("https://example.com")
|
17
|
+
ensure
|
18
|
+
browser.close
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
## close
|
25
|
+
|
26
|
+
```
|
27
|
+
def close
|
28
|
+
```
|
29
|
+
|
30
|
+
In case this browser is obtained using [BrowserType#launch](./browser_type#launch), closes the browser and all of its pages (if any
|
31
|
+
were opened).
|
32
|
+
|
33
|
+
In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the
|
34
|
+
browser server.
|
35
|
+
|
36
|
+
The [Browser](./browser) object itself is considered to be disposed and cannot be used anymore.
|
37
|
+
|
38
|
+
## contexts
|
39
|
+
|
40
|
+
```
|
41
|
+
def contexts
|
42
|
+
```
|
43
|
+
|
44
|
+
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
playwright.webkit.launch do |browser|
|
48
|
+
puts browser.contexts.count # => 0
|
49
|
+
context = browser.new_context
|
50
|
+
puts browser.contexts.count # => 1
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
## connected?
|
57
|
+
|
58
|
+
```
|
59
|
+
def connected?
|
60
|
+
```
|
61
|
+
|
62
|
+
Indicates that the browser is connected.
|
63
|
+
|
64
|
+
## new_context
|
65
|
+
|
66
|
+
```
|
67
|
+
def new_context(
|
68
|
+
acceptDownloads: nil,
|
69
|
+
bypassCSP: nil,
|
70
|
+
colorScheme: nil,
|
71
|
+
deviceScaleFactor: nil,
|
72
|
+
extraHTTPHeaders: nil,
|
73
|
+
geolocation: nil,
|
74
|
+
hasTouch: nil,
|
75
|
+
httpCredentials: nil,
|
76
|
+
ignoreHTTPSErrors: nil,
|
77
|
+
isMobile: nil,
|
78
|
+
javaScriptEnabled: nil,
|
79
|
+
locale: nil,
|
80
|
+
noViewport: nil,
|
81
|
+
offline: nil,
|
82
|
+
permissions: nil,
|
83
|
+
proxy: nil,
|
84
|
+
record_har_omit_content: nil,
|
85
|
+
record_har_path: nil,
|
86
|
+
record_video_dir: nil,
|
87
|
+
record_video_size: nil,
|
88
|
+
screen: nil,
|
89
|
+
storageState: nil,
|
90
|
+
timezoneId: nil,
|
91
|
+
userAgent: nil,
|
92
|
+
viewport: nil,
|
93
|
+
&block)
|
94
|
+
```
|
95
|
+
|
96
|
+
Creates a new browser context. It won't share cookies/cache with other browser contexts.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
playwright.firefox.launch do |browser| # or "chromium.launch" or "webkit.launch".
|
100
|
+
# create a new incognito browser context.
|
101
|
+
context = browser.new_context
|
102
|
+
|
103
|
+
# create a new page in a pristine context.
|
104
|
+
page = context.new_page()
|
105
|
+
page.goto("https://example.com")
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
## new_page
|
112
|
+
|
113
|
+
```
|
114
|
+
def new_page(
|
115
|
+
acceptDownloads: nil,
|
116
|
+
bypassCSP: nil,
|
117
|
+
colorScheme: nil,
|
118
|
+
deviceScaleFactor: nil,
|
119
|
+
extraHTTPHeaders: nil,
|
120
|
+
geolocation: nil,
|
121
|
+
hasTouch: nil,
|
122
|
+
httpCredentials: nil,
|
123
|
+
ignoreHTTPSErrors: nil,
|
124
|
+
isMobile: nil,
|
125
|
+
javaScriptEnabled: nil,
|
126
|
+
locale: nil,
|
127
|
+
noViewport: nil,
|
128
|
+
offline: nil,
|
129
|
+
permissions: nil,
|
130
|
+
proxy: nil,
|
131
|
+
record_har_omit_content: nil,
|
132
|
+
record_har_path: nil,
|
133
|
+
record_video_dir: nil,
|
134
|
+
record_video_size: nil,
|
135
|
+
screen: nil,
|
136
|
+
storageState: nil,
|
137
|
+
timezoneId: nil,
|
138
|
+
userAgent: nil,
|
139
|
+
viewport: nil)
|
140
|
+
```
|
141
|
+
|
142
|
+
Creates a new page in a new browser context. Closing this page will close the context as well.
|
143
|
+
|
144
|
+
This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and
|
145
|
+
testing frameworks should explicitly create [Browser#new_context](./browser#new_context) followed by the
|
146
|
+
[BrowserContext#new_page](./browser_context#new_page) to control their exact life times.
|
147
|
+
|
148
|
+
## start_tracing
|
149
|
+
|
150
|
+
```
|
151
|
+
def start_tracing(page: nil, categories: nil, path: nil, screenshots: nil)
|
152
|
+
```
|
153
|
+
|
154
|
+
> NOTE: Tracing is only supported on Chromium-based browsers.
|
155
|
+
|
156
|
+
You can use [Browser#start_tracing](./browser#start_tracing) and [Browser#stop_tracing](./browser#stop_tracing) to create a trace file that can be
|
157
|
+
opened in Chrome DevTools performance panel.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
browser.start_tracing(page: page, path: "trace.json")
|
161
|
+
begin
|
162
|
+
page.goto("https://www.google.com")
|
163
|
+
ensure
|
164
|
+
browser.stop_tracing
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
|
169
|
+
## stop_tracing
|
170
|
+
|
171
|
+
```
|
172
|
+
def stop_tracing
|
173
|
+
```
|
174
|
+
|
175
|
+
> NOTE: Tracing is only supported on Chromium-based browsers.
|
176
|
+
|
177
|
+
Returns the buffer with trace data.
|
178
|
+
|
179
|
+
## version
|
180
|
+
|
181
|
+
```
|
182
|
+
def version
|
183
|
+
```
|
184
|
+
|
185
|
+
Returns the browser version.
|
@@ -0,0 +1,398 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# BrowserContext
|
6
|
+
|
7
|
+
- extends: [EventEmitter]
|
8
|
+
|
9
|
+
BrowserContexts provide a way to operate multiple independent browser sessions.
|
10
|
+
|
11
|
+
If a page opens another page, e.g. with a `window.open` call, the popup will belong to the parent page's browser
|
12
|
+
context.
|
13
|
+
|
14
|
+
Playwright allows creation of "incognito" browser contexts with [Browser#new_context](./browser#new_context) method. "Incognito" browser
|
15
|
+
contexts don't write any browsing data to disk.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
# create a new incognito browser context
|
19
|
+
context = browser.new_context
|
20
|
+
|
21
|
+
# create a new page inside context.
|
22
|
+
page = context.new_page
|
23
|
+
page.goto("https://example.com")
|
24
|
+
|
25
|
+
# dispose context once it is no longer needed.
|
26
|
+
context.close()
|
27
|
+
```
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
## add_cookies
|
32
|
+
|
33
|
+
```
|
34
|
+
def add_cookies(cookies)
|
35
|
+
```
|
36
|
+
|
37
|
+
Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be
|
38
|
+
obtained via [BrowserContext#cookies](./browser_context#cookies).
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
browser_context.add_cookies([cookie_object1, cookie_object2])
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
## add_init_script
|
47
|
+
|
48
|
+
```
|
49
|
+
def add_init_script(path: nil, script: nil)
|
50
|
+
```
|
51
|
+
|
52
|
+
Adds a script which would be evaluated in one of the following scenarios:
|
53
|
+
- Whenever a page is created in the browser context or is navigated.
|
54
|
+
- Whenever a child frame is attached or navigated in any page in the browser context. In this case, the script is
|
55
|
+
evaluated in the context of the newly attached frame.
|
56
|
+
|
57
|
+
The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
|
58
|
+
the JavaScript environment, e.g. to seed `Math.random`.
|
59
|
+
|
60
|
+
An example of overriding `Math.random` before the page loads:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# in your playwright script, assuming the preload.js file is in same directory.
|
64
|
+
browser_context.add_init_script(path: "preload.js")
|
65
|
+
```
|
66
|
+
|
67
|
+
> NOTE: The order of evaluation of multiple scripts installed via [BrowserContext#add_init_script](./browser_context#add_init_script) and
|
68
|
+
[Page#add_init_script](./page#add_init_script) is not defined.
|
69
|
+
|
70
|
+
## browser
|
71
|
+
|
72
|
+
```
|
73
|
+
def browser
|
74
|
+
```
|
75
|
+
|
76
|
+
Returns the browser instance of the context. If it was launched as a persistent context null gets returned.
|
77
|
+
|
78
|
+
## clear_cookies
|
79
|
+
|
80
|
+
```
|
81
|
+
def clear_cookies
|
82
|
+
```
|
83
|
+
|
84
|
+
Clears context cookies.
|
85
|
+
|
86
|
+
## clear_permissions
|
87
|
+
|
88
|
+
```
|
89
|
+
def clear_permissions
|
90
|
+
```
|
91
|
+
|
92
|
+
Clears all permission overrides for the browser context.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
context = browser.new_context
|
96
|
+
context.grant_permissions(["clipboard-read"])
|
97
|
+
|
98
|
+
# do stuff ..
|
99
|
+
|
100
|
+
context.clear_permissions
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
## close
|
106
|
+
|
107
|
+
```
|
108
|
+
def close
|
109
|
+
```
|
110
|
+
|
111
|
+
Closes the browser context. All the pages that belong to the browser context will be closed.
|
112
|
+
|
113
|
+
> NOTE: The default browser context cannot be closed.
|
114
|
+
|
115
|
+
## cookies
|
116
|
+
|
117
|
+
```
|
118
|
+
def cookies(urls: nil)
|
119
|
+
```
|
120
|
+
|
121
|
+
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs
|
122
|
+
are returned.
|
123
|
+
|
124
|
+
## expose_binding
|
125
|
+
|
126
|
+
```
|
127
|
+
def expose_binding(name, callback, handle: nil)
|
128
|
+
```
|
129
|
+
|
130
|
+
The method adds a function called `name` on the `window` object of every frame in every page in the context. When
|
131
|
+
called, the function executes `callback` and returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which resolves to the return value of `callback`. If
|
132
|
+
the `callback` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), it will be awaited.
|
133
|
+
|
134
|
+
The first argument of the `callback` function contains information about the caller: `{ browserContext: BrowserContext,
|
135
|
+
page: Page, frame: Frame }`.
|
136
|
+
|
137
|
+
See [Page#expose_binding](./page#expose_binding) for page-only version.
|
138
|
+
|
139
|
+
An example of exposing page URL to all frames in all pages in the context:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
browser_context.expose_binding("pageURL", ->(source) { source[:page].url })
|
143
|
+
page = browser_context.new_page
|
144
|
+
|
145
|
+
page.content = <<~HTML
|
146
|
+
<script>
|
147
|
+
async function onClick() {
|
148
|
+
document.querySelector('div').textContent = await window.pageURL();
|
149
|
+
}
|
150
|
+
</script>
|
151
|
+
<button onclick="onClick()">Click me</button>
|
152
|
+
<div></div>
|
153
|
+
HTML
|
154
|
+
|
155
|
+
page.click("button")
|
156
|
+
```
|
157
|
+
|
158
|
+
An example of passing an element handle:
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
def print_text(source, element)
|
162
|
+
element.text_content
|
163
|
+
end
|
164
|
+
|
165
|
+
browser_context.expose_binding("clicked", method(:print_text), handle: true)
|
166
|
+
page = browser_context.new_page
|
167
|
+
|
168
|
+
page.content = <<~HTML
|
169
|
+
<script>
|
170
|
+
document.addEventListener('click', async (event) => {
|
171
|
+
alert(await window.clicked(event.target));
|
172
|
+
})
|
173
|
+
</script>
|
174
|
+
<div>Click me</div>
|
175
|
+
<div>Or click me</div>
|
176
|
+
HTML
|
177
|
+
|
178
|
+
page.click('div')
|
179
|
+
```
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
## expose_function
|
184
|
+
|
185
|
+
```
|
186
|
+
def expose_function(name, callback)
|
187
|
+
```
|
188
|
+
|
189
|
+
The method adds a function called `name` on the `window` object of every frame in every page in the context. When
|
190
|
+
called, the function executes `callback` and returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which resolves to the return value of `callback`.
|
191
|
+
|
192
|
+
If the `callback` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), it will be awaited.
|
193
|
+
|
194
|
+
See [Page#expose_function](./page#expose_function) for page-only version.
|
195
|
+
|
196
|
+
An example of adding an `sha256` function to all pages in the context:
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
require 'digest'
|
200
|
+
|
201
|
+
def sha256(text)
|
202
|
+
Digest::SHA256.hexdigest(text)
|
203
|
+
end
|
204
|
+
|
205
|
+
browser_context.expose_function("sha256", method(:sha256))
|
206
|
+
page = browser_context.new_page()
|
207
|
+
page.content = <<~HTML
|
208
|
+
<script>
|
209
|
+
async function onClick() {
|
210
|
+
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
|
211
|
+
}
|
212
|
+
</script>
|
213
|
+
<button onclick="onClick()">Click me</button>
|
214
|
+
<div></div>
|
215
|
+
HTML
|
216
|
+
page.click("button")
|
217
|
+
```
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
## grant_permissions
|
222
|
+
|
223
|
+
```
|
224
|
+
def grant_permissions(permissions, origin: nil)
|
225
|
+
```
|
226
|
+
|
227
|
+
Grants specified permissions to the browser context. Only grants corresponding permissions to the given origin if
|
228
|
+
specified.
|
229
|
+
|
230
|
+
## new_page
|
231
|
+
|
232
|
+
```
|
233
|
+
def new_page
|
234
|
+
```
|
235
|
+
|
236
|
+
Creates a new page in the browser context.
|
237
|
+
|
238
|
+
## pages
|
239
|
+
|
240
|
+
```
|
241
|
+
def pages
|
242
|
+
```
|
243
|
+
|
244
|
+
Returns all open pages in the context.
|
245
|
+
|
246
|
+
## route
|
247
|
+
|
248
|
+
```
|
249
|
+
def route(url, handler)
|
250
|
+
```
|
251
|
+
|
252
|
+
Routing provides the capability to modify network requests that are made by any page in the browser context. Once route
|
253
|
+
is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted.
|
254
|
+
|
255
|
+
An example of a naive handler that aborts all image requests:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
context = browser.new_context
|
259
|
+
page = context.new_page
|
260
|
+
context.route("**/*.{png,jpg,jpeg}", ->(route, request) { route.abort })
|
261
|
+
page.goto("https://example.com")
|
262
|
+
```
|
263
|
+
|
264
|
+
or the same snippet using a regex pattern instead:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
context = browser.new_context
|
268
|
+
page = context.new_page
|
269
|
+
context.route(/\.(png|jpg)$/, ->(route, request) { route.abort })
|
270
|
+
page.goto("https://example.com")
|
271
|
+
```
|
272
|
+
|
273
|
+
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some
|
274
|
+
post data, and leaving all other requests as is:
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
def handle_route(route, request)
|
278
|
+
if request.post_data["my-string"]
|
279
|
+
mocked_data = request.post_data.merge({ "my-string" => 'mocked-data'})
|
280
|
+
route.fulfill(postData: mocked_data)
|
281
|
+
else
|
282
|
+
route.continue
|
283
|
+
end
|
284
|
+
end
|
285
|
+
context.route("/api/**", method(:handle_route))
|
286
|
+
```
|
287
|
+
|
288
|
+
Page routes (set up with [Page#route](./page#route)) take precedence over browser context routes when request matches both
|
289
|
+
handlers.
|
290
|
+
|
291
|
+
To remove a route with its handler you can use [BrowserContext#unroute](./browser_context#unroute).
|
292
|
+
|
293
|
+
> NOTE: Enabling routing disables http cache.
|
294
|
+
|
295
|
+
## set_default_navigation_timeout
|
296
|
+
|
297
|
+
```
|
298
|
+
def set_default_navigation_timeout(timeout)
|
299
|
+
```
|
300
|
+
alias: `default_navigation_timeout=`
|
301
|
+
|
302
|
+
This setting will change the default maximum navigation time for the following methods and related shortcuts:
|
303
|
+
- [Page#go_back](./page#go_back)
|
304
|
+
- [Page#go_forward](./page#go_forward)
|
305
|
+
- [Page#goto](./page#goto)
|
306
|
+
- [Page#reload](./page#reload)
|
307
|
+
- [Page#set_content](./page#set_content)
|
308
|
+
- [Page#expect_navigation](./page#expect_navigation)
|
309
|
+
|
310
|
+
> NOTE: [Page#set_default_navigation_timeout](./page#set_default_navigation_timeout) and [Page#set_default_timeout](./page#set_default_timeout) take priority over
|
311
|
+
[BrowserContext#set_default_navigation_timeout](./browser_context#set_default_navigation_timeout).
|
312
|
+
|
313
|
+
## set_default_timeout
|
314
|
+
|
315
|
+
```
|
316
|
+
def set_default_timeout(timeout)
|
317
|
+
```
|
318
|
+
alias: `default_timeout=`
|
319
|
+
|
320
|
+
This setting will change the default maximum time for all the methods accepting `timeout` option.
|
321
|
+
|
322
|
+
> NOTE: [Page#set_default_navigation_timeout](./page#set_default_navigation_timeout), [Page#set_default_timeout](./page#set_default_timeout) and
|
323
|
+
[BrowserContext#set_default_navigation_timeout](./browser_context#set_default_navigation_timeout) take priority over [BrowserContext#set_default_timeout](./browser_context#set_default_timeout).
|
324
|
+
|
325
|
+
## set_extra_http_headers
|
326
|
+
|
327
|
+
```
|
328
|
+
def set_extra_http_headers(headers)
|
329
|
+
```
|
330
|
+
alias: `extra_http_headers=`
|
331
|
+
|
332
|
+
The extra HTTP headers will be sent with every request initiated by any page in the context. These headers are merged
|
333
|
+
with page-specific extra HTTP headers set with [Page#set_extra_http_headers](./page#set_extra_http_headers). If page overrides a particular
|
334
|
+
header, page-specific header value will be used instead of the browser context header value.
|
335
|
+
|
336
|
+
> NOTE: [BrowserContext#set_extra_http_headers](./browser_context#set_extra_http_headers) does not guarantee the order of headers in the outgoing requests.
|
337
|
+
|
338
|
+
## set_geolocation
|
339
|
+
|
340
|
+
```
|
341
|
+
def set_geolocation(geolocation)
|
342
|
+
```
|
343
|
+
alias: `geolocation=`
|
344
|
+
|
345
|
+
Sets the context's geolocation. Passing `null` or `undefined` emulates position unavailable.
|
346
|
+
|
347
|
+
```ruby
|
348
|
+
browser_context.geolocation = { latitude: 59.95, longitude: 30.31667 }
|
349
|
+
```
|
350
|
+
|
351
|
+
> NOTE: Consider using [BrowserContext#grant_permissions](./browser_context#grant_permissions) to grant permissions for the browser context pages to
|
352
|
+
read its geolocation.
|
353
|
+
|
354
|
+
## set_offline
|
355
|
+
|
356
|
+
```
|
357
|
+
def set_offline(offline)
|
358
|
+
```
|
359
|
+
alias: `offline=`
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
## unroute
|
364
|
+
|
365
|
+
```
|
366
|
+
def unroute(url, handler: nil)
|
367
|
+
```
|
368
|
+
|
369
|
+
Removes a route created with [BrowserContext#route](./browser_context#route). When `handler` is not specified, removes all routes for
|
370
|
+
the `url`.
|
371
|
+
|
372
|
+
## expect_event
|
373
|
+
|
374
|
+
```
|
375
|
+
def expect_event(event, predicate: nil, timeout: nil, &block)
|
376
|
+
```
|
377
|
+
|
378
|
+
Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy
|
379
|
+
value. Will throw an error if the context closes before the event is fired. Returns the event data value.
|
380
|
+
|
381
|
+
```ruby
|
382
|
+
new_page = browser_context.expect_event('page') do
|
383
|
+
page.click('button')
|
384
|
+
end
|
385
|
+
```
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
## expect_page
|
390
|
+
|
391
|
+
```
|
392
|
+
def expect_page(predicate: nil, timeout: nil)
|
393
|
+
```
|
394
|
+
|
395
|
+
Performs action and waits for a new [Page](./page) to be created in the context. If predicate is provided, it passes [Page](./page) value into the `predicate` and waits for `predicate.call(page)` to return a truthy value. Will throw an error if
|
396
|
+
the context closes before new [Page](./page) is created.
|
397
|
+
|
398
|
+
## tracing
|