I'm trying to execute a very basic test in Playwright: log in to a web and then go to an specific page once logged. The problem is that if I execute for a second time the test, I get an error from the web since the browser remains logged, with an example:
const { test, expect } = require('@playwright/test');
const {LoginPage} = require("../pages/LoginPage");
test('Login', async ({page}) => {
const loginPage = new LoginPage(page);
await loginPage.userLogged(user, password);
await page.goto(`${process.env.web_url}/users`);
});
when I execute the test in the UI the first time it works, but if I run a second time it doesn't succeed since the session remains logged, so the login step fails. In Cypress I have the same test, but it works with each execution, the browser is cleaned. The workaround so far I have is the one below, visiting the logout endpoint, this way I end any previous session:
test('Login', async ({page}) => {
await page.goto(${process.env.webapp_url}/logout`);
const loginPage = new LoginPage(page);
await loginPage.userLogged(user, password);
await page.goto(${process.env.web_url}/users);
});
I know that the problem may be with the concepts of page, browser and context. I have tried several ways but without success. For example this:
let page;
test.beforeEach(async ({ browser }) => {
page = await browser.newPage();
});
test('Login', async () => {
const loginPage = new LoginPage(page);
await loginPage.userLogged(user, password);
await page.goto(${process.env.webapp_url}/users);
});
test('Roles', async () => {
await page.goto(${process.env.webapp_url}/roles);
});
test.afterEach(async ({ browser }) => {
await browser.close();
});
when I execute this code in the UI, I would have expected the Roles test to fail since I'm not doing the logging step, but it succeeds since previously I ran another test that logged me in to the web.
I expected a similar behavior like Cypress, where after each execution the browser was cleared and with a new execution, the browser would be brand new, no session stored. I have tried working with context, browser and pages concepts without success.
The problem is that you are not isolating your
pageobject between runs. Using global scope is bad practice in Playwright.To clean up your code:
test()block. If you have logic that needs to be repeated for each test, create reusabletest.step()(steps docs).page(and thus, it'scontext). Instead, share thestorageState(storageState docs) after login. You can create itbeforeAll, then import it into your test usingbrowser.newContext({storageState}).page(and thuscontext) between tests, encapsulate all of the tests with atest.describe()block. Tests in these blocks run sequentially, and if a later test fails, it will rerun all previous tests before running again. This will keep your runs consistent.I hope that helps!