Class: Playwright::Route
- Inherits:
-
PlaywrightApi
- Object
- PlaywrightApi
- Playwright::Route
- Defined in:
- lib/playwright_api/route.rb
Overview
Whenever a network route is set up with [‘method: Page.route`] or [`method: BrowserContext.route`], the `Route` object allows to handle the route.
Learn more about [networking](../network.md).
Instance Method Summary collapse
-
#abort(errorCode: nil) ⇒ Object
Aborts the route’s request.
-
#continue(headers: nil, method: nil, postData: nil, url: nil) ⇒ Object
Sends route’s request to the network with optional overrides.
-
#fallback(headers: nil, method: nil, postData: nil, url: nil) ⇒ Object
Continues route’s request with optional overrides.
-
#fetch(headers: nil, maxRedirects: nil, maxRetries: nil, method: nil, postData: nil, timeout: nil, url: nil) ⇒ Object
Performs the request and fetches result without fulfilling it, so that the response could be modified and then fulfilled.
-
#fulfill(body: nil, contentType: nil, headers: nil, json: nil, path: nil, response: nil, status: nil) ⇒ Object
Fulfills route’s request with given response.
-
#off(event, callback) ⇒ Object
– inherited from EventEmitter –.
-
#on(event, callback) ⇒ Object
– inherited from EventEmitter –.
-
#once(event, callback) ⇒ Object
– inherited from EventEmitter –.
- #redirect_navigation_request(url) ⇒ Object
-
#request ⇒ Object
A request to be routed.
Methods inherited from PlaywrightApi
Constructor Details
This class inherits a constructor from Playwright::PlaywrightApi
Instance Method Details
#abort(errorCode: nil) ⇒ Object
Aborts the route’s request.
11 12 13 |
# File 'lib/playwright_api/route.rb', line 11 def abort(errorCode: nil) wrap_impl(@impl.abort(errorCode: unwrap_impl(errorCode))) end |
#continue(headers: nil, method: nil, postData: nil, url: nil) ⇒ Object
Sends route’s request to the network with optional overrides.
Usage
“‘python sync def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "foo-value", # set "foo" header
"bar": None # remove "bar" header
}
route.continue_(headers=headers)
page.route(“*/”, handle) “‘
Details
Note that any overrides such as ‘url` or `headers` only apply to the request being routed. If this request results in a redirect, overrides will not be applied to the new redirected request. If you want to propagate a header through redirects, use the combination of [`method: Route.fetch`] and [`method: Route.fulfill`] instead.
- ‘method: Route.continue`
-
will immediately send the request to the network, other matching handlers won’t be invoked. Use [‘method: Route.fallback`] If you want next matching handler in the chain to be invoked.
38 39 40 |
# File 'lib/playwright_api/route.rb', line 38 def continue(headers: nil, method: nil, postData: nil, url: nil) wrap_impl(@impl.continue(headers: unwrap_impl(headers), method: unwrap_impl(method), postData: unwrap_impl(postData), url: unwrap_impl(url))) end |
#fallback(headers: nil, method: nil, postData: nil, url: nil) ⇒ Object
Continues route’s request with optional overrides. The method is similar to [‘method: Route.continue`] with the difference that other matching handlers will be invoked before sending the request.
Usage
When several routes match the given pattern, they run in the order opposite to their registration. That way the last registered route can always override all the previous ones. In the example below, request will be handled by the bottom-most handler first, then it’ll fall back to the previous one and in the end will be aborted by the first registered route.
“‘python sync page.route(“*/”, lambda route: route.abort()) # Runs last. page.route(“*/”, lambda route: route.fallback()) # Runs second. page.route(“*/”, lambda route: route.fallback()) # Runs first. “`
Registering multiple routes is useful when you want separate handlers to handle different kinds of requests, for example API calls vs page resources or GET requests vs POST requests as in the example below.
“‘python sync # Handle GET requests. def handle_get(route):
if route.request.method != "GET":
route.fallback()
return
# Handling GET only.
# ...
# Handle POST requests. def handle_post(route):
if route.request.method != "POST":
route.fallback()
return
# Handling POST only.
# ...
page.route(“*/”, handle_get) page.route(“*/”, handle_post) “‘
One can also modify request while falling back to the subsequent handler, that way intermediate route handler can modify url, method, headers and postData of the request.
“‘python sync def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "foo-value", # set "foo" header
"bar": None # remove "bar" header
}
route.fallback(headers=headers)
page.route(“*/”, handle) “‘
Use [‘method: Route.continue`] to immediately send the request to the network, other matching handlers won’t be invoked in that case.
100 101 102 |
# File 'lib/playwright_api/route.rb', line 100 def fallback(headers: nil, method: nil, postData: nil, url: nil) wrap_impl(@impl.fallback(headers: unwrap_impl(headers), method: unwrap_impl(method), postData: unwrap_impl(postData), url: unwrap_impl(url))) end |
#fetch(headers: nil, maxRedirects: nil, maxRetries: nil, method: nil, postData: nil, timeout: nil, url: nil) ⇒ Object
Performs the request and fetches result without fulfilling it, so that the response could be modified and then fulfilled.
Usage
“‘python sync def handle(route):
response = route.fetch()
json = response.json()
json["message"]["big_red_dog"] = []
route.fulfill(response=response, json=json)
page.route(“dog.ceo/api/breeds/list/all”, handle) “‘
Details
Note that ‘headers` option will apply to the fetched request as well as any redirects initiated by it. If you want to only apply `headers` to the original request, but not to redirects, look into [`method: Route.continue`] instead.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/playwright_api/route.rb', line 123 def fetch( headers: nil, maxRedirects: nil, maxRetries: nil, method: nil, postData: nil, timeout: nil, url: nil) wrap_impl(@impl.fetch(headers: unwrap_impl(headers), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), method: unwrap_impl(method), postData: unwrap_impl(postData), timeout: unwrap_impl(timeout), url: unwrap_impl(url))) end |
#fulfill(body: nil, contentType: nil, headers: nil, json: nil, path: nil, response: nil, status: nil) ⇒ Object
Fulfills route’s request with given response.
Usage
An example of fulfilling all requests with 404 responses:
“‘python sync page.route(“*/”, lambda route: route.fulfill(
status=404,
content_type="text/plain",
body="not found!"))
“‘
An example of serving static file:
“‘python sync page.route(“**/xhr_endpoint”, lambda route: route.fulfill(path=“mock_data.json”)) “`
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/playwright_api/route.rb', line 153 def fulfill( body: nil, contentType: nil, headers: nil, json: nil, path: nil, response: nil, status: nil) wrap_impl(@impl.fulfill(body: unwrap_impl(body), contentType: unwrap_impl(contentType), headers: unwrap_impl(headers), json: unwrap_impl(json), path: unwrap_impl(path), response: unwrap_impl(response), status: unwrap_impl(status))) end |
#off(event, callback) ⇒ Object
– inherited from EventEmitter –
189 190 191 |
# File 'lib/playwright_api/route.rb', line 189 def off(event, callback) event_emitter_proxy.off(event, callback) end |
#on(event, callback) ⇒ Object
– inherited from EventEmitter –
183 184 185 |
# File 'lib/playwright_api/route.rb', line 183 def on(event, callback) event_emitter_proxy.on(event, callback) end |
#once(event, callback) ⇒ Object
– inherited from EventEmitter –
177 178 179 |
# File 'lib/playwright_api/route.rb', line 177 def once(event, callback) event_emitter_proxy.once(event, callback) end |
#redirect_navigation_request(url) ⇒ Object
171 172 173 |
# File 'lib/playwright_api/route.rb', line 171 def (url) wrap_impl(@impl.(unwrap_impl(url))) end |
#request ⇒ Object
A request to be routed.
166 167 168 |
# File 'lib/playwright_api/route.rb', line 166 def request wrap_impl(@impl.request) end |