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

#==, from_channel_owner, #initialize

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#abort(errorCode: nil) ⇒ Object

Aborts the route’s request.

Raises:

  • (NotImplementedError)


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

def abort(errorCode: nil)
  raise NotImplementedError.new('abort is not implemented yet.')
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) “‘

Raises:

  • (NotImplementedError)


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

def continue_(headers: nil, method: nil, postData: nil, url: nil)
  raise NotImplementedError.new('continue_ is not implemented yet.')
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”)) “`

Raises:

  • (NotImplementedError)


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)
  raise NotImplementedError.new('fulfill is not implemented yet.')
end

#requestObject

A request to be routed.

Raises:

  • (NotImplementedError)


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

def request
  raise NotImplementedError.new('request is not implemented yet.')
end