Playwright Browser Context

An independent browser session, for example, an incognito mode, can be called a browser context. In this article, we will be seeing what a browser fixture and context are and how it can be done in playwright.

const {test, expect} = require('@playwright/test');

  test('testing', async ({browser})=> 
{

//

});
  • A browser is a fixture that must be imported into the async function within the curly bracket to use the browser object and play around with it.
const {test, expect} = require('@playwright/test');

  test('testing', async ({browser})=> 
{

const context = await browser.newContext();


});
  • newContext() creates new incognito browser context.
  • It doesn't write any browser data nor contains any cookies related to the previous test.
  • We can call it starting fresh.
const {test, expect} = require('@playwright/test');

  test('testing', async ({browser})=> 
{

const context = await browser.newContext();
const page = await context.newPage();


});
  • newContext() opens just a new browser and not a page. We have to do it one by one.
  • newPage() opens up new page after opening browser.
const {test, expect} = require('@playwright/test');

  test('testing', async ({browser})=> 
{

const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://programsbuzz.com');


});
  • Now using goto(), we can navigate different sites and continue our automation.
const {test, expect} = require('@playwright/test');

  test('testing', async ({browser})=> 
{

const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://programsbuzz.com');
await context.close();

});
  • Using close(), we can close the context once it is no longer needed.
Thu, 06/16/2022 - 13:14

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags