Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Playwright: ReferenceError: browserContext is not defined

I’m creating a new browserContextin 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading