Rayrun

How do I use the wildcard '*' in the 'page.route()' function of Playwright to intercept all requests from a specific origin?

Answer

You can intercept all requests from a specific origin in Playwright using the wildcard '*' in the page.route() function. Here's how you can do it:

await page.route('https://ray.run/*', route => {
  // Your interception logic goes here
});

In this code, any request made to URLs starting with "https://ray.run/" will be intercepted. You can then define your own logic inside the route handler function to handle these intercepted requests.

For example, if you want to block all requests from that origin, you can use route.abort():

await page.route('https://ray.run/*', route => {
  route.abort();
});

Alternatively, if you want to modify or fulfill these intercepted requests, you can use route.fulfill() and provide a custom response:

await page.route('https://ray.run/*', route => {
  const customResponse = {
    status: 200,
    contentType: 'text/html',
    body: 'Intercepted response',
  };
  
  route.fulfill(customResponse);
});

In this case, any request made to URLs starting with "https://ray.run/" will receive a custom response with status code 200, content type 'text/html', and body containing 'Intercepted response'.

Remember, when defining routes using page.route(), they take precedence over routes defined at browser context level using browserContext.route(). If there are conflicting routes for a specific request URL, the one defined at page level will be used.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].