I'm creating a new browserContext
in the beforeAll
hook to clear the cookies
, sessionStorage
& localStorage
. This is working fine & the test is passing without issues. However, I am getting the error: ReferenceError: browserContext is not defined
...
This is my spec file:
// tests/specs/login.spec.js
const { test, expect } = require('@playwright/test');
require('dotenv').config();
test.describe('Automates: CO-6367', () => {
let browserContext, page;
test.beforeEach(async ({ browser }) => {
browserContext = await browser.newContext();
page = await browserContext.newPage();
await browserContext.addInitScript(() => {
browserContext.clearCookies();
localStorage.clear();
sessionStorage.clear();
});
await page.goto(`${process.env.BASE_URL}/login`);
});
test.afterEach(async () => {
await browserContext.close();
});
test('User logs into BO via URL with correct credentials', async () => {
await page.fill('#email', process.env.BO_LOGIN_EMAIL);
await page.fill('#password', process.env.BO_LOGIN_PASSWORD);
await page.click('button[type="submit"]');
await page.waitForURL('**/dashboards/main');
const dashboardHeading = await page.locator('role=heading[name="Dashboard"]');
await expect(dashboardHeading).toBeVisible();
});
});
I'm not sure what's the issue here. I have added a screenshot for better context:
I see additional line inside addInitScript which maybe cause of the issue.
browserContext.clearCookies()
Lets try without this.
await browserContext.addInitScript(() => {
localStorage.clear();
sessionStorage.clear();
});
The reason is the browser does not have access to the code running in node's scope.