Tutorial 3 – Launch Chromium, Mozilla, Webkit, Chrome, Edge browsers using Playwright-Java
What you will Learn:
Launch chromium browser in header mode
Launch firefox browser
Launch webkit browser
Launch chrome browser
Launch Microsoft Edge browser
Code snippets
Launch chromium browser in header mode
Below is the snapshot of test that we had executed in our previous blog. One important thing to note here is that, the chromium browser was NOT launched.
The reason for is, by default, the test script runs in headless mode
In order to launch the browser, we have to execute the test in header mode.
To enable the header mode, we have to write the highlighted argument in the launch() method as shown below:
.launch(new BrowserType.LaunchOptions().setHeadless(false))
Save and execute.
Notice that the chromium browser launches this time!
Launch firefox browser
Similarly we can launch firefox browser (comment line#14, add line#15)
Save and execute, firefox launches
Launch webkit browser
Similarly we can launch webkit browser
Launch chrome browser
To launch chrome browser, we have to simply create an object of ‘LaunchOptions’ class, set the channel as ‘chrome’ and finally set headless to false mode (lines#15-17).
Also, un-comment line#19 and comment line#21
Save and execute, chrome browser launches
Launch Microsoft Edge browser
To launch edge, comment line#16 and add line#17
Save and execute, edge browser gets launched
Code snippet (chromium, firefox, webkit)
package com.w2a.pwjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class PWTest {
@Test
public void PWFirstTest() {
Playwright pw = Playwright.create();
//BrowserType browsertype = pw.chromium();
//BrowserType browsertype = pw.firefox();
BrowserType browsertype = pw.webkit();
Browser browser = browsertype.launch(new BrowserType.LaunchOptions().setHeadless(false));
//Page page = pw.chromium().launch().newPage();
Page page = browser.newPage();
page.navigate("https://www.way2automation.com/lifetime-membership-club/");
System.out.println(page.title());
}
}
Code snippet (chrome, edge)
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("chrome");
lop.setChannel("msedge");
lop.setHeadless(false);
BrowserType browsertype = pw.chromium();
//BrowserType browsertype = pw.firefox();
//BrowserType browsertype = pw.webkit();
//Browser browser = browsertype.launch(new BrowserType.LaunchOptions().setHeadless(false));
Browser browser = browsertype.launch(lop);
//Page page = pw.chromium().launch().newPage();
Page page = browser.newPage();
page.navigate("https://www.way2automation.com/lifetime-membership-club/");
System.out.println(page.title());
}
}
Thank you for reading!