In playwright java is there a way to get current browser which is running also the version and check that browser is connected?

Sameera De Silva
2 min readDec 26, 2023

--

Yes you can. You could use the below code.

package sam.scripts;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.testng.annotations.Test;

public class Basic {

@Test
public void firstTest() {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setHandleSIGINT(false));
Page page = browser.newPage();
// To get browser type.
BrowserType browserType = browser.browserType();
String browserVersion = browser.version();
// Using browser type, can get the version.
System.out.println("Current browser: " + browserType.name());
System.out.println("Browser version: " + browserVersion);
System.out.println("Browser is connected: "+browser.isConnected());

page.navigate("https://the-internet.herokuapp.com/");
page.click("//a[contains(@href, '/hovers')]");

}
}

Output-

Current browser: chromium
Browser version: 113.0.5672.53
Browser is connected: true

what is browser.isConnected()?

Indicates that the browser is connected return boolean yes or no.
It’s better to check the browser’s connection status before performing actions on it in below special cases.
To verify that a browser instance is still active and ready for use.
If the browser is not connected, any subsequent commands might fail or cause unexpected behavior.
Common scenarios where you might use isConnected():
Before navigating to a new page.
Before interacting with elements on the page.
Within conditional logic to handle different scenarios based on the browser’s connection state.

While browser.isConnected() is valuable for checking the browser’s connection to Playwright,
it doesn’t directly reflect the broader internet connectivity status. Here’s a visual breakdown to clarify:

What browser.isConnected() Checks:

Communication Channel: It verifies that the Playwright library can successfully communicate with the browser instance it launched.
Internal State: It ensures the browser is responsive and capable of executing commands sent by Playwright.
What It Doesn’t Check:

External Internet Access: It doesn’t guarantee that the browser can reach external websites or resources on the internet.
Network Connectivity: It doesn’t assess the overall network status or diagnose potential connectivity issues.

--

--

No responses yet