How to Open Multiple Browser Sessions in Playwright?
Bakkappa N
YouTuber @ Testers Talk | Content Creator | Playwright Cypress Selenium SDET Rest Assured API Postman | Automation Testing | QA Test Lead
Playwright offers the concept of BrowserContexts to manage multiple independent browser sessions within a single browser instance. This is invaluable for scenarios involving multiple users, parallel testing, or isolating browser data.
Step1: Create browser instance using below command
const browser = await chromium.launch();
Step2; Creates a new browser context. It won't share cookies/cache with other browser contexts.
const context1 = await browser.newContext();
const context2 = await browser.newContext();
Step3: Creates a new page in the browser context.
const page1 = await context1.newPage();
const page2 = await context2.newPage();
Step4: First browser actions
await page1.goto('https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=-US8rBdfw1kxRBnt');
await page1.locator('#contents > ytd-playlist-video-renderer:nth-child(1)').click()
await page1.waitForTimeout(5000);
Step5: Second browser actions
await page2.goto('https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=-US8rBdfw1kxRBnt');
Step6: Close both browsers
await browser.close();
Step7: Finally, this is how test looks like.
import { test, chromium } from '@playwright/test';
test('Open multiple browser sessions', async () => {
const browser = await chromium.launch();
const context1 = await browser.newContext();
const context2 = await browser.newContext();
const page1 = await context1.newPage();
const page2 = await context2.newPage();
await page1.goto('https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=-US8rBdfw1kxRBnt');
await page1.locator('#contents > ytd-playlist-video-renderer:nth-child(1)').click()
await page1.waitForTimeout(5000);
await page2.goto('https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=-US8rBdfw1kxRBnt');
await browser.close();
});
Step8: Run the tests
npx playwright test
Similarly, we can create 'N' numbers of browsers and perform actions by using respective page objects.
Reference: https://playwright.dev/docs
TO RECEIVE DAILY UPDATES, SUBSCRIBE CHANNEL - https://www.youtube.com/c/BakkappaN_SoftwareTesting_Videos?sub_confirmation=1
===== Playwright Automation Full Courses =====
-> Playwright Full Course - https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=65JRAAG-SkkfphDm
-> Playwright API Testing Crash Course - https://youtube.com/playlist?list=PLUeDIlio4THF3rnYZ63qkbHwMTXiG67vz&si=hNd_ji_kXVmIAr_R
领英推荐
-> Playwright with Azure DeveOps Pipeline - https://youtube.com/playlist?list=PLUeDIlio4THG8irTXJn-Z02nYzmi6dUOF&si=3b4G5tvemlPy0--j
-> Playwright with CRM application testing - https://youtu.be/WwovRRp0f4o?si=w0rzJlUI9BhfP4KS
===== Playwright GitHub Repositories =====
-> Playwright Full Course - https://github.com/BakkappaN/PlaywrightTutorialFullCourse
-> Playwright API Testing Crash Course - https://github.com/BakkappaN/PlaywrightAPITestingTutorial
-> Playwright with Azure DeveOps Pipeline - https://github.com/BakkappaN/PlaywrightAzureDevopsPipeline
-> Playwright with CRM application testing -https://github.com/BakkappaN/MicrosoftD365CRMPlaywrightFramework
-> Playwright with JavaScript Framework [UI + API] - https://github.com/BakkappaN/PlaywrightBaseAutomationFramework
-> Playwright with TypeScript Framework [UI + API]- https://github.com/BakkappaN/Playwright-TypeScript-Framework
Learn more about Playwright:
-> Playwright API Testing Crash Course
-> Playwright with Azure DevOps Complete guide
-> Testing Microsoft CRM application using Playwright
#playwright #apitesting #qa #softwaretesting #crm #javascript #typescript #ado #azuredeveops #framework #fullcourse #codegen #test #e2e #testing #automation #testerstalk #bakkappan #git #github #testautomation #selenium #cypress #microsoftplaywright #microsoft #opensource
SUBSCRIBE CHANNEL TO GET LATEST SOFTWARE TESTING UPDATES - https://www.youtube.com/c/BakkappaN_SoftwareTesting_Videos?sub_confirmation=1