Closed frassinier closed 3 years ago
You want to create a new eyes
fixture instead. Some docs here:
// myTest.ts
import { test as baseTest } from '@playwright/test';
export { expect } from '@playwright/test';
export const test = baseTest.extend<{
eyes: Eyes;
}>({
eyes: async ({}, use) => {
const eyes = new Eyes();
await use(eyes);
await eyes.close();
},
});
// test.spec.ts
import { test, expect } from './myTest';
test("use eyes", async ({ eyes, page }) => {
// Use eyes here.
});
If you want to reuse the runner among those, you need a worker fixture with the runner. That worker fixture will be created once per worker and will be shared among tests that worker runs. See https://playwright.dev/docs/test-fixtures#worker-fixtures for more details:
// myTest.ts
const test = baseTest.extend<{
eyes: Eyes;
}, {
eyesRunner: ClassicRunner
}>({
eyesRunner: [async ({}, use) => {
// Set up once per worker.
await use(new ClassicRunner());
}, { scope: 'worker'}],
eyes: async ({ eyesRunner }, use) => {
const eyes = new Eyes(eyesRunner);
await use(eyes);
await eyes.close();
},
});
// test.spec.ts
import { test, expect } from './myTest';
test("use eyes", async ({ eyes, page }) => {
// Use eyes here.
});
Thank you, Pavel! 💯 Exactly what I need! The second snippet works like a charm!
@pavelfeldman This information appears to be what I'm seeking for my issue however it is unclear whether the fixture runs this code only once and then passes the value to each test (desired), or if it runs it for every test and adds it to the context of the test. Which is it?
I only need such code to run once before all tests, globalSetup style, but then need the resulting variable to be available within in each test. Fixtures don't seem to be what is needed based on my understanding but I'm likely wrong. Would you please help clarify?
As is mentioned by the original poster I want to run const eyes = new Eyes(runner)
only once and have the eyes
variable be accessible within each test.
Thanks.
@frassinier could you please reopen the question due to the last comment?
I have the exact same problem as @fredboyle. What's the best solution here?
Looked into this more and I think you can just use the method defined here: https://playwright.dev/docs/test-advanced#global-setup-and-teardown
Once your object is defined in the global-setup, you can set it as an environment variable with process.env.FOO
. Then in your test you can make it available with const { FOO } = process.env;
Hey!
Can I pass an object from
globalSetup
fn to all my tests?Imagine that I would like to use a visual regression testing SaaS tool (Applitools for example),
And then I would love to be able to write a test like that
Or maybe I'm completely wrong about how to achieve it?