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.

Instance Method Summary collapse

Methods inherited from PlaywrightApi

#==, #initialize, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#abort(errorCode: nil) ⇒ Object

Aborts the route’s request.



7
8
9
# File 'lib/playwright_api/route.rb', line 7

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.

“‘js await page.route(’*/‘, (route, request) => {

// Override headers
const headers = {
  ...request.headers(),
  foo: 'bar', // set "foo" header
  origin: undefined, // remove "origin" header
};
route.continue({headers});

}); “‘

“‘python async async def handle(route, request):

# override headers
headers = {
    **request.headers,
    "foo": "bar" # set "foo" header
    "origin": None # remove "origin" header
}
await route.continue(headers=headers)

} await page.route(“*/”, handle) “‘

“‘python sync def handle(route, request):

# override headers
headers = {
    **request.headers,
    "foo": "bar" # set "foo" header
    "origin": None # remove "origin" header
}
route.continue(headers=headers)

} page.route(“*/”, handle) “‘



51
52
53
# File 'lib/playwright_api/route.rb', line 51

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

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

Fulfills route’s request with given response.

An example of fulfilling all requests with 404 responses:

“‘js await page.route(’*/‘, route => {

route.fulfill({
  status: 404,
  contentType: 'text/plain',
  body: 'Not Found!'
});

}); “‘

“‘python async await page.route(“*/”, lambda route: route.fulfill(

status=404,
content_type="text/plain",
body="not found!"))

“‘

“‘python sync page.route(“*/”, lambda route: route.fulfill(

status=404,
content_type="text/plain",
body="not found!"))

“‘

An example of serving static file:

“‘js await page.route(’**/xhr_endpoint’, route => route.fulfill({ path: ‘mock_data.json’ })); “‘

“‘python async await page.route(“**/xhr_endpoint”, lambda route: route.fulfill(path=“mock_data.json”)) “`

“‘python sync page.route(“**/xhr_endpoint”, lambda route: route.fulfill(path=“mock_data.json”)) “`



98
99
100
101
102
103
104
105
# File 'lib/playwright_api/route.rb', line 98

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

#off(event, callback) ⇒ Object

– inherited from EventEmitter –



114
115
116
# File 'lib/playwright_api/route.rb', line 114

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

#on(event, callback) ⇒ Object

– inherited from EventEmitter –



126
127
128
# File 'lib/playwright_api/route.rb', line 126

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

#once(event, callback) ⇒ Object

– inherited from EventEmitter –



120
121
122
# File 'lib/playwright_api/route.rb', line 120

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

#requestObject

A request to be routed.



108
109
110
# File 'lib/playwright_api/route.rb', line 108

def request
  wrap_impl(@impl.request)
end