I'm trying to make tests for a webapplication with Playwright test in JavaScript, but I would like to make them modular, to avoid repeating code. Let's say two tests need to fill a form. Instead of writing code to fill the form in both tests, is it possible to include a file in these two tests that contains filling the form?
import { test, expect } from '@playwright/test';
test.describe("", () => {
//create a new page
test.beforeAll( async ({ browser }) => {
let page = await browser.newPage();
await page.goto('https://www.example.com/');
})
test('Do stuff', async () => {
import{test} from 'login.spec.mjs';
import{test} from 'fillform.spec.mjs';
})
login.spec.mjs
test('Login', async () => {
// Click input[name="username"]
await page.click('input[name="username"]');
//...
})
How do we call these functions, which are all named "test()"? Is this a good way to approach testing?