When you execute a Playwright script on the Chrome browser, you are telling Playwright to launch tests on the real Chrome browser rather than the bundled Chromium. By default, Playwright uses Chromium, which is an open-source browser engine used by Chrome and Edge. However, if you specifically want to run on Google Chrome, Playwright allows you to configure it.
(1) Chromium vs Chrome
- Chromium is the base project that Chrome is built on. Playwright ships with its own bundled version of Chromium.
- Chrome is the actual browser installed on your system (with additional features, branding, and sometimes slight differences).
(2) How Playwright Handles Browsers
- Playwright has built-in support for Chromium, Firefox, and WebKit.
- For Chromium-based browsers, you can point Playwright to use either the bundled Chromium or your installed Chrome browser.
- This is done by specifying the executable path of Chrome.
(3) Why Run Tests on Chrome?
- Some projects or organizations require running automation scripts on the same browser version that end users use (i.e., Chrome instead of Playwright’s default Chromium).
- This ensures compatibility testing with the actual Chrome build.
- Running on Chrome may also be needed when browser policies, extensions, or enterprise configurations are involved.
(4) Process of Running on Chrome
- Playwright launches Chrome using its Playwright API, but instead of the bundled Chromium, it points to the Chrome executable available on your machine.
- Once launched, the script runs in the same way—navigating pages, finding locators, clicking elements, etc.—but all actions are now happening in the Google Chrome browser.
(5) Key Points
- Execution on Chrome is nearly identical to execution on Chromium in terms of Playwright commands.
- The main difference lies in specifying which browser binary to use.
- After that, all locators, assertions, and interactions remain the same.
To execute a Playwright script in the Chrome browser using Java, you need to launch the browser in "Chrome" mode instead of the default "Chromium" (Playwright uses Chromium by default).
Steps to Run Playwright in Chrome (Java):
- Install Chrome on your system (Playwright will launch the installed Chrome browser).
- Set the executable path of Chrome.
- Use
launchPersistentContextwith Chrome executable path to launch Chrome. - Write and run your Playwright Java test.
Maven Dependency
<dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.43.0</version> <!-- or latest --> </dependency>
Java Code Example: Launch in Chrome
import com.microsoft.playwright.*; import java.nio.file.Paths; public class PlaywrightChromeExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { // Path to the installed Chrome browser executable String chromePath = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; // Adjust this path BrowserType.LaunchOptions options = new BrowserType.LaunchOptions() .setExecutablePath(Paths.get(chromePath)) .setHeadless(false); // Show the browser window Browser browser = playwright.chromium().launch(options); Page page = browser.newPage(); page.navigate("https://example.com"); System.out.println("Page title: " + page.title()); browser.close(); } } }
Code explanation:
(a) Create Playwright object
(b) Create Browser object
(c) Then, create Page object
(d) Navigate to webpage by page.navigate()
(e) Print page title on console