-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: run auto fixtures when new browser context created #34585
Comments
@yonatanai I don't think this will be included in Playwright. I'd recommend to structure your code differently, so that you run your interceptors on a new context manually. Something along these lines: async function attachInterceptors(context) {
await context.route(...);
// ...
}
// Call this function manually instead of `browser.newContext()`
async function newContext(browser) {
const context = await browser.newContext();
await attachInterceptors(context);
return context;
}
const test = base.extends({
// Override "context" fixture instead of adding an auto fixture.
context: async ({ context }, use) => {
await attachInterceptors(context);
await use(context);
},
}); Let me know whether this helps. |
@dgozman, thanks for the response! I try to understand if we are missing something because it seems like a common use case, and we didn't find any examples or good solutions for this problem. |
@yonatanai I think it would help to make async function attachInterceptors(context: BrowserContext) {
await context.route(...);
// ...
}
const test = base.extend<{ newContext: () => Promise<BrowserContext> }>({
newContext: async ({ browser }, use) => {
const contexts: BrowserContext[] = [];
await use(async () => {
const context = await browser.newContext();
contexts.push(context);
await attachInterceptors(context);
return context;
});
await Promise.all(contexts.map(context => context.close()));
},
context: async ({ context }, use) => {
await attachInterceptors(context);
await use(context);
},
});
test('example', async ({ newContext }) => {
const context1 = await newContext();
const context2 = await newContext();
// ...
}); |
🚀 Feature Request
run auto fixtures on every new context created, or creating an event on the browser level
on('new-context', (context) => { })
Example
or
Motivation
We use auto fixtures to set up common interceptors and attach cookies to our tests.
Some of the interceptors and cookies get the data from Fixtures-options.
We encounter a problem: if we create a new browser context in our tests, the auto fixtures will not apply to this new context. It would be nice to run them on a newly created context or even to get an event that a new context created, and then we will run the callback to apply the logic of the auto fixtures on the newly created context.
Overriding the browser fixture could work, but we can't get the Fixtures-options in the override, so it does not work.
The text was updated successfully, but these errors were encountered: