Rayrun
← Back to videos

How to handle multiple tabs in Page Object Model | Playwright Tutorial - Part 87

In this video, we'll learn how to handle the multiple tabs or target_blank in Page Object Model.

In this video, the instructor explains how to handle multiple windows using the page object model in Playwright. He demonstrates this using a real-world scenario in which users need to interact with an e-commerce website (Flipkart), search for a product, and add it to the cart.

To achieve this, the instructor has created a test script with three classes (HomePage, SearchResultPage, and ResultPage) that includes basic elements and actions like closing the login pop-up, searching for a product, and clicking the first search result.

However, when running the test initially, it fails to find the "Add to Cart" button on the new tab that opens after clicking the first search result. This is because Playwright did not switch to the new tab automatically.

To handle this, the instructor first modifies the 'clickFirstResult' function:

await Promise.all([
  this.page.waitForEvent("popup"),
  this.page.click(".search-result")
]);

The 'waitForEvent("popup")' line waits for a new tab to open. The destructuring method can be used to retrieve the new tab, and the 'return newTab;' line is added after clicking the first search result.

The instructor then shows how to pass the new tab to the constructor of a page object:

return Then(async () => {
  const newTab = await searchResultspage.clickFirstResult();
  return await new ResultPage(newTab).addToCart();
});

This modification successfully adds the product to the cart, and the test passes as expected. The instructor concludes the tutorial by inviting viewers to ask any questions in the comments section if they have doubts regarding the concepts covered in the video.

Related Videos

#16 - ARIA and how Playwright is making use of it to identify UI controls?
#16 - ARIA and how Playwright is making use of it to identify UI controls?
Create feature, steps & config | Playwright & Cucumber - Typescript | Part 1
Create feature, steps & config | Playwright & Cucumber - Typescript | Part 1
Playwright Tutorial: Handling Multiple Tabs using Page Object Model
Playwright Tutorial: Handling Multiple Tabs using Page Object Model
Playwright - Turn Page Object Model Pages into fixtures
Playwright - Turn Page Object Model Pages into fixtures
TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.