I have a sign-in form I am testing using Playwright that has one of two possible states:
- If the user already exists, it will only ask for their password and present a "Sign In" button.
- If it is a new user, it will ask for their "First & last name" and password and present a "Save" button.
Due to some logic of our authentication system, it's not practical for it to be entirely deterministic so I need to ensure my test handles each of those cases successfully.
One such solution is [1]:
if (await page.getByRole('button', { name: 'Sign In' }).count() > 0) {
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign In' }).click();
} else {
await page.getByLabel('First & last name').fill('Bob Alice');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Save' }).click();
}
However, this requires the page to wait for my timeout before continuing. I could reduce the timeout for the conditional check [2], but this adds more brittleness to the test.
How can I use conditional logic without forced timeouts to ensure one of two code paths in Playwright is executed?