admin管理员组文章数量:1429610
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?
- 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
3 Answers
Reset to default 3U 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
本文标签:
版权声明:本文标题:javascript - Playwright - beforeEach for all the files in the suite, or mock responses to all tests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745548367a2662812.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论