最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论