javascript - Playwright - beforeEach for all the files in the suite, or mock responses to all tests - Stack Overflow

I've seen i can mock a response in Playwright using the page.routeawait page.route('apibre

I've seen i can mock a response in Playwright using the page.route

await page.route('/api/breeds/list/all', async route => {
  const json = {
    message: { 'test_breed': [] }
  };
  await route.fulfill({ json });
});

That's awesome, but can i do it to all tests without need to call it in each file? Is there a mock configuration in the playwright.config or something similar?

I've seen i can mock a response in Playwright using the page.route

await page.route('https://dog.ceo/api/breeds/list/all', async route => {
  const json = {
    message: { 'test_breed': [] }
  };
  await route.fulfill({ json });
});

That's awesome, but can i do it to all tests without need to call it in each file? Is there a mock configuration in the playwright.config or something similar?

Share Improve this question edited Dec 22, 2022 at 5:10 No Idea For Name asked Dec 18, 2022 at 8:21 No Idea For NameNo Idea For Name 11.6k10 gold badges48 silver badges73 bronze badges 2
  • What is your use case? What are you trying to achieve by doing this? – Vishal Aggarwal Commented Dec 21, 2022 at 22:21
  • @VishalAggarwal i have a interceptor(s) that i want to run before each test – No Idea For Name Commented Dec 22, 2022 at 4:51
Add a ment  | 

3 Answers 3

Reset to default 3

U can use fixture for it.
Let test intercepting https://httpbin/uuid what normally returns {"uuid": "03840957-0386-498b-bd29-5c9cdbd84ed9"}

In file fixtures.ts:

    import { test as base } from "@playwright/test";
    
    export const test = base.extend({
      page: async ({ baseURL, page }, use) => {
        // We have a few cases where we need our app to know it's running in Playwright.
        // This is inspired by Cypress that auto-injects window.Cypress.
        await page.addInitScript(() => {
          (window as any).Playwright = true;
        });
    
        // Here we can enable logging of all requests, which is useful to see sometimes.
        // We also block some unnecessary third-party requests which speeds up the tests.
        await page.route(`https://httpbin/uuid`, async (route) => {
          const json = {
            message: 'my stuff 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745548367a4632446.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信