Tutorial 4 – Inspector usage and launch multiple browser tabs using Playwright-Java
What you will Learn:
Usage of ‘Pause’ and Playwright Inspector
Multiple Browser Pages/Tabs using single browser instance
Code snippets
Usage of ‘Pause’ and Playwright Inspector
Sometimes we may want to pause our test so that we can either debug or wait for an even to occur etc.
.pause() method comes handy here, see the method description below
The below script will launch the browser and then playwright will pause the script
Execute the script, notice below that ‘Playwright Inspector’ is launched and the script pauses and the browser does not close automatically this time
Click the ‘Resume’ button to continue the script.
The browser closes and the test gets passed
Multiple Browser Pages/Tabs using single browser instance
We can use the same browser instance to launch multiple browser pages. Let us see how.
We can create a new browser context using ‘newContext’ method as shown below. Also, as can be seen in the description, the browser context will not share cache with other browser contexts
This method returns ‘BrowserContext’
So let us create a ‘context’ variable holding the browser context
Using this context, we can now create a new browse page
So we can write
Now the newPage() methods returns a Page
Let us catch this new page in a variable page1
Similarly let us create 2 more pages using the same browser context
Now we can launch 3 websites on 3 different pages
Save and execute.
Notice below that 3 tabs open
In the above case, all 3 websites were same.
Below we have 3 different sites
Save and execute
Code snippet#1
package com.w2a.pwjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class PWTest {
@Test
public void PWFirstTest() {
Playwright pw = Playwright.create();
LaunchOptions lop = new LaunchOptions();
lop.setChannel("msedge");
lop.setHeadless(false);
BrowserType browsertype = pw.chromium();
Browser browser = browsertype.launch(lop);
Page page = browser.newPage();
page.navigate("https://www.way2automation.com/lifetime-membership-club/");
page.pause();
}
}
Code snippet#2
package com.w2a.pwjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class PWTest {
@Test
public void PWFirstTest() {
Playwright pw = Playwright.create();
LaunchOptions lop = new LaunchOptions();
lop.setChannel("msedge");
lop.setHeadless(false);
BrowserType browsertype = pw.chromium();
Browser browser = browsertype.launch(lop);
BrowserContext context = browser.newContext();
Page page1 = context.newPage();
Page page2 = context.newPage();
Page page3 = context.newPage();
page1.navigate("https://www.way2automation.com/lifetime-membership-club/");
page2.navigate("https://www.bing.com/");
page3.navigate("https://www.bbc.com/");
page3.pause();
}
}
Thank you for reading!