Class: Playwright::Route

Inherits:
PlaywrightApi show all
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

Methods inherited from PlaywrightApi

#initialize, unwrap, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#abort(errorCode: nil) ⇒ Object

Aborts the route’s request.



9
10
11
# File 'lib/playwright_api/route.rb', line 9

def abort(errorCode: nil)
  wrap_impl(@impl.abort(errorCode: unwrap_impl(errorCode)))
end

#continue(headers: nil, method: nil, postData: nil, url: nil) ⇒ Object

Continues route’s request with optional overrides.

“‘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) “‘



27
28
29
# File 'lib/playwright_api/route.rb', line 27

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

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_post(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) “‘



81
82
83
# File 'lib/playwright_api/route.rb', line 81

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

#fulfill(body: nil, contentType: nil, headers: nil, path: nil, response: nil, status: nil) ⇒ Object

Fulfills route’s request with given response.

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”)) “`



101
102
103
104
105
106
107
108
109
# File 'lib/playwright_api/route.rb', line 101

def fulfill(
      body: nil,
      contentType: nil,
      headers: nil,
      path: nil,
      response: nil,
      status: nil)
  wrap_impl(@impl.fulfill(body: unwrap_impl(body), contentType: unwrap_impl(contentType), headers: unwrap_impl(headers), path: unwrap_impl(path), response: unwrap_impl(response), status: unwrap_impl(status)))
end

#off(event, callback) ⇒ Object

– inherited from EventEmitter –



123
124
125
# File 'lib/playwright_api/route.rb', line 123

def off(event, callback)
  event_emitter_proxy.off(event, callback)
end

#on(event, callback) ⇒ Object

– inherited from EventEmitter –



135
136
137
# File 'lib/playwright_api/route.rb', line 135

def on(event, callback)
  event_emitter_proxy.on(event, callback)
end

#once(event, callback) ⇒ Object

– inherited from EventEmitter –



129
130
131
# File 'lib/playwright_api/route.rb', line 129

def once(event, callback)
  event_emitter_proxy.once(event, callback)
end

#redirect_navigation_request(url) ⇒ Object



117
118
119
# File 'lib/playwright_api/route.rb', line 117

def redirect_navigation_request(url)
  wrap_impl(@impl.redirect_navigation_request(unwrap_impl(url)))
end

#requestObject

A request to be routed.



112
113
114
# File 'lib/playwright_api/route.rb', line 112

def request
  wrap_impl(@impl.request)
end