-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Question: sample code how to use Hooks, Before, BeforeAll #149
Comments
Hi @QAPranav ! |
Hi @vitalets thank you for responding, I tried to use the beforeAll with fixture base on document, which is not allowing This is Global.tsimport { Page } from '@playwright/test';
const authFile = 'cookies.json';
export class Global{
constructor(public page: Page){
this.page = page
}
async setup(){
await this.page.goto('');
await this.page.getByRole('button', { name: 'I accept cookies' }).click();
await this.page.context().storageState({ path: authFile });
}
} This is were I load my Fixturesimport { test as base } from 'playwright-bdd';
import { Page } from '@playwright/test';
import * as Pages from '../pages/index';
const createTestFunction =
<T extends new (page: Page) => InstanceType<T>>(PageClass: T) =>
(
{ page }: { page: Page },
use: (fixture: InstanceType<T>) => Promise<void>
) =>
use(new PageClass(page));
type MyFixtures = {
globalPage: Pages.Global
};
export const test = base.extend<MyFixtures>({
globalPage: createTestFunction(Pages.Global)
}); This is my hooks.tsimport { createBdd } from 'playwright-bdd';
import { test } from '../fixtures/pagefixtures';
const { BeforeAll, AfterAll } = createBdd(test);
BeforeAll(async function ({ globalPage }) {
await globalPage.setup();
}); This code I should aspect one cookies.json file generated on project level but it is not generating any cookies.json file Even I tried using this keyword in hooks which is still not allowing me and playwright page fixture is also not invoking here |
This is because BeforeAll runs once per worker, and fixture should be worker-scoped, not test-scoped. Fix can be like this: export const test = base.extend<{}, MyFixtures>({
globalPage: [createTestFunction(Pages.Global), { scope: 'worker' }]
}); Notice that in worker scoped fixtures there is no pre-created page (b/c each test has own page), so you should create page manually like it's shown in Playwright docs. |
Is there a proper document or any sample code how to use Hooks, Before, BeforeAll thank you
I tried this feature that not able to invoke beforeAll where I load all the cookies for the project, proper documents would be great
The text was updated successfully, but these errors were encountered: