I have just completed my first two playwright tests, but I am not sure how to optimize them.
The scenario is the following:
I log in to the system with an administrative user.
I create a new user and save the generated username and password using the following code:
// copy username and password
const username = await page.locator(‘#username0’).innerText();
const password = await page.locator(‘#passwd0’).innerText();
expect(username).not.toBeNull();
expect(username).not.toBeUndefined();
expect(password).not.toBeNull();
expect(password).not.toBeUndefined();
I log out the admin user.
I log in the created user (using the const created above), accept the terms and conditions and answer 220 questions, saving a screenshot of every 20th question.
for (let x = 1; x <= 220; x++) {
if (x % 20 === 0) {
await storeScreenshot(popupPage, testInfo, ‘test Q’ + x);
}
await popupPage.waitForLoadState();
await popupPage.locator(user.selector).click();
if (x % 20 === 0) {
await storeScreenshot(popupPage, testInfo, ‘test Q’ + x + ‘ – selected’);
}
await popupPage.locator(‘#submitknap’).click();
}
I am very happy that this works, and it is even parameterized with a csv for testing different answer patterns.
If any step in this fails, the whole case fails.
Ideally the admin part is not necessary as a part of the test and is reusable, but creating a user IS something I want to have. I could create some static users and wipe them before running each test, but I like the dynamic approach of creating a user prior to logging it in. I also have some environments that are not easily reset.
I am in doubt as to how I can split this test scenario up. Should I have the admin part as a helper function called by the testcase, or as a smaller testcase prior to calling the main testcase?
Edit
For a little perspective this test takes 3 minutes to complete. a complete run of 6 parameters in three browsers takes 16 minutes.
Source: Read More