Playwright allows to extend test function so we can inject our logic and use it in tests (source):
import { test as base } from '@playwright/test';
import { TodoPage } from './todo-page';
export type Options = { defaultItem: string };
export const test = base.extend<Options & { todoPage: TodoPage }>({
// Define an option and provide a default value.
// We can later override it in the config.
defaultItem: ['Do stuff', { option: true }],
// Define a fixture. Note that it can use built-in fixture "page"
// and a new option "defaultItem".
todoPage: async ({ page, defaultItem }, use) => {
const todoPage = new TodoPage(page);
await todoPage.goto();
await todoPage.addToDo(defaultItem);
await use(todoPage);
await todoPage.removeAll();
},
});
We can attach them automatically to all tests (source).
Example solution they used is spawning child process that will just run CLI (which is mostly not ideal, but doable and enough for now). Here we can see another approach (but also based on building CLI command).
Here is also some mentions about some workaround to programmatically run some subgroup of tests.
Playwright allows to extend
test
function so we can inject our logic and use it in tests (source):We can attach them automatically to all tests (source).
Unfortunately, I don't see native option to run Playwright programmatically: https://github.com/microsoft/playwright/issues/7275. They even mentioned running Playwright in AWS Lambda (source).
Example solution they used is spawning child process that will just run CLI (which is mostly not ideal, but doable and enough for now). Here we can see another approach (but also based on building CLI command).
Here is also some mentions about some workaround to programmatically run some subgroup of tests.