Welcome to the next chapter of our Playwright journey. As we venture into managing multiple tabs, you'll discover how essential these skills are for testing complex web applications that often involve numerous tabs and windows. Previously, we explored handling asynchronous operations and ensuring seamless waiting strategies. Now, we're taking a step forward to understand how to navigate between multiple tabs efficiently.
In modern web applications, it is common for interactions to open new windows or tabs. This is particularly true for actions such as logging into an account or accessing customer support. In this lesson, you will learn how to manage multiple tabs using Playwright efficiently and in an automated manner.
Let's take a look at a practical Playwright example:
TypeScript1import { test, expect } from '@playwright/test'; 2 3test('manage multiple tabs', async ({ page, context }) => { 4 await page.goto('http://localhost:3000/home'); 5 await page.click('text=Sign In'); 6 await page.fill('#username', 'user1'); 7 await page.fill('#password', 'pass1'); 8 await page.click('button[type="submit"]'); 9 10 await expect(page).toHaveURL('http://localhost:3000/home'); 11 await expect(page.locator('text=Welcome to the BookStore')).toBeVisible(); 12 13 const [newPage] = await Promise.all([ 14 context.waitForEvent('page'), 15 page.click('text=👤 Account') 16 ]); 17 18 await newPage.waitForLoadState('domcontentloaded'); 19 expect(await newPage.url()).toBe('http://localhost:3000/account'); 20 await expect(newPage.locator('text=Your Account')).toBeVisible(); 21 22 await newPage.close(); 23 expect(await page.url()).toBe('http://localhost:3000/home'); 24});
In this test, we achieve the following:
-
Navigation and Authentication: You begin by navigating to the application home page, signing in, and verifying the successful login process.
-
Handling a New Tab:
Promise.all([...])
: This is used to run multiple asynchronous operations concurrently. It waits for all the promises inside to resolve.context.waitForEvent('page')
: This waits for a new page (or tab) to be opened. It's listening for the 'page' event, which is triggered when a new tab is created.page.click('text=👤 Account')
: This simulates a click on the "Account" link or button, which is expected to open a new tab.newPage.waitForLoadState('domcontentloaded')
: This waits until the new page's Document Object Model (DOM) is fully loaded and parsed, but it doesn't wait for stylesheets, images, and subframes to finish loading.
-
Verification and Closure: Once the new tab is verified, you close it using the command
newPage.close()
while ensuring the original tab remains functional, showcasing the ability to handle multiple contexts without losing track.
Mastering the ability to switch between multiple tabs and maintaining control is critical to ensuring the robustness of automated tests for web applications. Many websites today use new tabs to provide additional user interactions, like accessing accounts or viewing detailed information. Understanding these techniques ensures that your automation scripts accurately mimic real-world user behavior, leading to more comprehensive testing coverage.
Ready to dive into practice? Let's tackle some exercises that will empower you with these pivotal skills.