What is a Tab in Playwright?
- In browser automation, a tab is represented as a Page object in Playwright.
- Each new tab is essentially a new Page instance, but it still belongs to the same BrowserContext.
- This allows multiple tabs to run in parallel, isolated from each other’s states like cookies, local storage, and sessions (if contexts are separated).
How to Open a New Tab in Playwright?
(1) Start with a Browser and Context
- When you launch a browser in Playwright, you typically work inside a BrowserContext.
- A single context can represent one user session.
(2) Create a New Page (Tab)
- To open a new tab, you create a new Page inside the same BrowserContext.
- Each new Page = a new tab in that browser window.
(3) Switching Between Tabs
- Each tab (Page) can be referenced independently.
- You can perform actions like navigation, clicking, form filling, etc., on one Page while keeping the other Page open in the background.
(4) Running Multiple Tabs
- Playwright allows multiple Page instances in parallel.
- This is useful for scenarios like: Testing multi-tab workflows (e.g., clicking a link opens a new tab), Comparing data across tabs, Simulating multiple user journeys side by side.
(5) Closing Tabs
- Once a tab (Page) is no longer needed, it can be closed independently without affecting other open tabs.
- E-commerce: Add a product to the cart in one tab, check order history in another.
- Authentication: Login in one tab, open a secure page in another tab to verify session sharing.
- Social Media: Open multiple tabs to simulate different user feeds.
In Playwright Java, opening a new tab is done by creating a new Page from the existing BrowserContext. A tab in a browser corresponds to a Page object in Playwright. Here's how it works:
Steps to Open a New Tab in Playwright Java:
- Launch the browser.
- Create a browser context using
browser.newContext(). - Open the first tab using
context.newPage(). - Open a second tab (new page) using the same
context.newPage(). - Navigate both tabs to different websites or perform other actions.
Example Website Used:
We'll use https://example.com for the first tab and https://playwright.dev for the second.
Java Code to Open New Tab in Playwright:
import com.microsoft.playwright.*; public class OpenNewTabExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { // Step 1: Launch browser Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)); // Step 2: Create a new browser context BrowserContext context = browser.newContext(); // Step 3: Open the first tab Page firstTab = context.newPage(); firstTab.navigate("https://example.com"); System.out.println("First tab title: " + firstTab.title()); // Step 4: Open a new tab (second tab) Page secondTab = context.newPage(); secondTab.navigate("https://playwright.dev"); System.out.println("Second tab title: " + secondTab.title()); // Wait so you can see the browser tabs before the program closes Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }
Code explanation:
Important Points:
- BrowserContext behaves like an incognito window — tabs (Pages) under the same context can share session data.
- context.newPage() opens a new tab in the same browser window.
- navigate(url) is used to open a website in that tab.
- You can interact with both tabs using their respective Page objects.
Suggested Posts:
1. Playwright with TestNG Integration
2. Automate Login Page in Playwright
3. Comma Selectors in Playwright
4. Handle Alerts in Playwright
5. Page Object Model in Playwright
No comments:
Post a Comment