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.
Instance Method Summary collapse
- 
  
    
      #abort(errorCode: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Aborts the route’s request. 
- 
  
    
      #continue(headers: nil, method: nil, postData: nil, url: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Continues route’s request with optional overrides. 
- 
  
    
      #fulfill(body: nil, contentType: nil, headers: nil, path: 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 –. 
- 
  
    
      #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.
| 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});
}); “‘
“‘java page.route(“*/”, route ->
// Override headers
Map<String, String> headers = new HashMap<>(route.request().headers());
headers.put("foo", "bar"); // set "foo" header
headers.remove("origin"); // remove "origin" header
route.resume(new Route.ResumeOptions().setHeaders(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) “‘
| 61 62 63 | # File 'lib/playwright_api/route.rb', line 61 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!'
});
}); “‘
“‘java page.route(“*/”, route ->
route.fulfill(new Route.FulfillOptions()
  .setStatus(404)
  .setContentType("text/plain")
  .setBody("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’ })); “‘
“‘java page.route(“**/xhr_endpoint”, route -> route.fulfill(
new Route.FulfillOptions().setPath(Paths.get("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”)) “`
| 122 123 124 125 126 127 128 129 | # File 'lib/playwright_api/route.rb', line 122 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 –
| 150 151 152 | # File 'lib/playwright_api/route.rb', line 150 def off(event, callback) event_emitter_proxy.off(event, callback) end | 
#on(event, callback) ⇒ Object
– inherited from EventEmitter –
| 144 145 146 | # File 'lib/playwright_api/route.rb', line 144 def on(event, callback) event_emitter_proxy.on(event, callback) end | 
#once(event, callback) ⇒ Object
– inherited from EventEmitter –
| 138 139 140 | # File 'lib/playwright_api/route.rb', line 138 def once(event, callback) event_emitter_proxy.once(event, callback) end | 
#request ⇒ Object
A request to be routed.
| 132 133 134 | # File 'lib/playwright_api/route.rb', line 132 def request wrap_impl(@impl.request) end |