Handling windows and tabs is a crucial aspect of browser automation, and Playwright Java provides powerful capabilities to manage them effectively. This article will discuss about handling windows and tabs using Playwright Java.
- Open a new window using Playwright
- Switch to a new window
- Open a new tab using Playwright
- Switch to a new tab
Let’s look at them one by one.
Open a new window using Playwright
In Playwright, a browser context typically corresponds to a window. Therefore, utilizing a browser context allows for managing multiple windows.
Making an instance of a browser
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
Create a new browser context which represents a window
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
Creating a Page and navigating to the URL
Page page = browserContext.newPage();
// Navigating to the URL
page.navigate("https://testkru.com/Elements/TextFields");
To open a new window, create a new browser context.
// creating a new BrowserContext
BrowserContext browserContext2 = browser.newContext();
We can now navigate to any URL
Page page2 = browserContext2.newPage();
page2.navigate("https://testkru.com/Elements/Buttons");
Here is the whole code.
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class CodekruTest {
public static void main(String[] args) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
Page page = browserContext.newPage();
// Navigating to the URL
page.navigate("https://testkru.com/Elements/TextFields");
System.out.println("Title of current window: " + page.title());
// Creating a new BrowserContext
BrowserContext browserContext2 = browser.newContext();
// Navigating to the new URL
Page page2 = browserContext2.newPage();
page2.navigate("https://testkru.com/Elements/Buttons");
System.out.println("Title of new window: " + page2.title());
// closing the instances
browser.close();
playwright.close();
}
}
Output –
Title of current window: Text Fields
Title of new window: Buttons
Switch to a new window
As previously mentioned, Playwright utilizes browser context to efficiently handle multiple windows. This allows for seamless switching between windows and executing various actions.
Get all browser contexts
// get all browser context
List<BrowserContext> contexts = browser.contexts();
Iterate through the list and perform actions on your desired window
for (BrowserContext context : contexts) {
// get first page for a browser context
Page currentPage = context.pages().get(0);
System.out.println("Title of Page: " + currentPage.title());
}
Below is the whole code for the same.
import java.util.List;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class CodekruTest {
public static void main(String[] args) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
Page page = browserContext.newPage();
// Navigating to the URL
page.navigate("https://testkru.com/Elements/TextFields");
// Creating a new BrowserContext
BrowserContext browserContext2 = browser.newContext();
// Navigating to the new URL
Page page2 = browserContext2.newPage();
page2.navigate("https://testkru.com/Elements/Buttons");
List<BrowserContext> contexts = browser.contexts();
for (BrowserContext context : contexts) {
// get first page for a browser context
Page currentPage = context.pages().get(0);
System.out.println("Title of Page: " + currentPage.title());
}
// closing the instances
browser.close();
playwright.close();
}
}
Output –
Title of Page: Text Fields
Title of Page: Buttons
Open a new tab using Playwright
In Playwright, a Page typically refers to a tab within a browser window. It’s helpful to think of browserContext as representing the entire browser window, with each Page representing a single tab within that window. Similar to how a window can have multiple tabs, a browserContext can have multiple Pages.
Creating Browser context
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
Opening the first tab
// creating a page
Page page1 = browserContext.newPage();
// Navigating to the URL
page1.navigate("https://testkru.com/Elements/TextFields");
Opening the second tab
// Opening second tab
Page page2 = browserContext.newPage();
page2.navigate("https://testkru.com/Elements/Buttons");
Here is the whole code where we have opened two tabs.
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class CodekruTest {
public static void main(String[] args) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
// creating a page
Page page1 = browserContext.newPage();
// Navigating to the URL
page1.navigate("https://testkru.com/Elements/TextFields");
System.out.println("Current tab title: " + page1.title());
// Opening second tab
Page page2 = browserContext.newPage();
page2.navigate("https://testkru.com/Elements/Buttons");
System.out.println("New tab title: " + page2.title());
// closing the instances
browser.close();
playwright.close();
}
}
Output –
Current tab title: Text Fields
New tab title: Buttons
Switch to a new tab
Managing multiple tabs within a single browser window can be a challenge. Playwright provides us with the ability to iterate through these tabs and execute various actions on the specific tab we desire.
Get all tabs of a browser window
We can use the pages() method of BrowserContext to get all pages ( or we can say tabs ) of a browser window.
// get all pages
List<Page> pages = browserContext.pages();
Switching between tabs
Now, we can switch between the tabs and perform various actions on the desired tabs.
// iterate over pages
for(Page singlePage: pages) {
System.out.println("Title of the page: " + singlePage.title());
}
Below is the whole code –
import java.util.List;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class CodekruTest {
public static void main(String[] args) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
// creating a page
Page page1 = browserContext.newPage();
// Navigating to the URL
page1.navigate("https://testkru.com/Elements/TextFields");
// Opening second tab
Page page2 = browserContext.newPage();
page2.navigate("https://testkru.com/Elements/Buttons");
// get all pages
List<Page> pages = browserContext.pages();
// iterate over pages
for(Page singlePage: pages) {
System.out.println("Title of the page: " + singlePage.title());
}
// closing the instances
browser.close();
playwright.close();
}
}
Output –
Title of the page: Text Fields
Title of the page: Buttons
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.