Skip to content
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

Open
QAPranav opened this issue Apr 26, 2024 · 3 comments
Open

Question: sample code how to use Hooks, Before, BeforeAll #149

QAPranav opened this issue Apr 26, 2024 · 3 comments
Labels
question Further information is requested

Comments

@QAPranav
Copy link

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

@QAPranav QAPranav added the question Further information is requested label Apr 26, 2024
@vitalets vitalets changed the title Question: Hi @vitalets you have done amazing job, just one question is there a proper document of any sample code how to use Hooks, Before, BeforeAll thank you Question: sample code how to use Hooks, Before, BeforeAll Apr 30, 2024
@vitalets
Copy link
Owner

Hi @QAPranav !
Are there something missing in the Hooks docs?

@QAPranav
Copy link
Author

QAPranav commented Apr 30, 2024

Hi @vitalets thank you for responding, I tried to use the beforeAll with fixture base on document, which is not allowing
here is my code

This is Global.ts

import { 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 Fixtures

import { 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.ts

import { 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
Where I get error
Property 'globalPage' does not exist on type 'PlaywrightWorkerArgs & PlaywrightWorkerOptions & BddFixturesWorker & { $workerInfo: WorkerInfo; }' this is type error I am getting

Even I tried using this keyword in hooks which is still not allowing me and playwright page fixture is also not invoking here
It would be nice to have page fixture
BeforeAll(async function ({ page }) {
await page.goto('')
});
please review this thank you for your help

@vitalets
Copy link
Owner

Property 'globalPage' does not exist on type 'PlaywrightWorkerArgs & PlaywrightWorkerOptions & BddFixturesWorker & { $workerInfo: WorkerInfo; }

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants