Playwright Selenium Grid Example

The Playwright selenium grid example below can quickly be run on a free Gridlastic selenium grid and at much lower cost per minute than other cloud services.

HybridParallelTest.java

import com.microsoft.playwright.*;
import org.junit.jupiter.api.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.ClientConfig;

import java.net.URL;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class HybridParallelTest {
    private WebDriver seleniumDriver;
    private Browser playwrightBrowser;
    private Playwright playwright;
    private Page page;

    // Start a free Gridlastic Selenium Grid and these credentials will be available to you in your Gridlastic dashboard.
    private static final String GRIDLASTIC_HUB = ""; // like https://your-gridlastic-username:your-gridlastic-password@your-gridlastic-subdomain.gridlastic.com/wd/hub
    private static final String GRIDLASTIC_GRID_VIDEO_URL = ""; // like: https://s3-us-west-1.amazonaws.com/023a15f2-534d-31e5-f8cc-7ceaf6355377/238a51b7-c526-ceb8-8ffd-1759b612a464/play.html?

    @BeforeEach
    void setup() throws Exception {
        // --- 1. Launch a new browser for each test using Gridlastic Selenium Grid ---
        // Supported browsers are Chrome and Edge on Windows and Linux.
        ChromeOptions options = new ChromeOptions();
       // EdgeOptions options = new EdgeOptions();

        options.setCapability("browserVersion", "latest");
        options.setCapability("platformName", "WIN11");

        Map gridlasticOptions = new HashMap<>();
        gridlasticOptions.put("video", true);
        options.setCapability("gridlastic:options", gridlasticOptions);

        // On Linux start-maximized does not expand browser window to max screen size. Always set a window size.
        if (options.getPlatformName().toString().equalsIgnoreCase("linux")) {
            options.addArguments(Arrays.asList("--window-position=0,0"));
            options.addArguments(Arrays.asList("--window-size=1920,1080"));
        } else {
            options.addArguments(Arrays.asList("--start-maximized"));
        }

        ClientConfig config = ClientConfig.defaultConfig().readTimeout(Duration.ofMinutes(10)); // if there are no selenium grid nodes available it might take several minutes before more are spun up.
        URL seleniumRemoteAddress = new URL(GRIDLASTIC_HUB);
        seleniumDriver = RemoteWebDriver.builder().address(seleniumRemoteAddress).oneOf(options).config(config).build();

        // --- 2. Extract the CDP endpoint from Selenium's capabilities ---
        playwright = Playwright.create();
        String cdpEndpoint = ((RemoteWebDriver) seleniumDriver).getCapabilities().getCapability("se:cdp").toString();
        assertNotNull(cdpEndpoint, "Failed to retrieve the CDP endpoint from Selenium.");
        System.out.println("Retrieved CDP endpoint: " + cdpEndpoint);

        // --- 3. Connect Playwright to the same browser via CDP ---
        playwrightBrowser = playwright.chromium().connectOverCDP(cdpEndpoint); // used for both Chrome and Edge
        page = playwrightBrowser.contexts().get(0).newPage();
    }

    @Test
    void testGridlasticHomepage() throws InterruptedException {
        page.navigate("https://www.gridlastic.com/?demo");
        assertEquals("Selenium grid", page.title());
        Thread.sleep(5000); // Wait for 5 seconds for demo purposes
    }

    @Test
    void testH1Text() throws InterruptedException {
        // Navigate to your page
        page.navigate("https://www.gridlastic.com/?demo");
        // Locate the h1 element by its tag name
        Locator h1 = page.locator("h1");
        // Assert that it contains the expected text
        assertThat(h1).hasText("SELENIUM GRID");
        Thread.sleep(5000); // Wait for 5 seconds for demo purposes
    }

    @AfterEach
    void tearDown() {
        // --- Clean up: close Playwright browser, then quit Selenium driver ---
        System.out.println("Video URL: " + GRIDLASTIC_GRID_VIDEO_URL + ((RemoteWebDriver) seleniumDriver).getSessionId());

        if (page != null) {
            page.close();
        }
        if (playwrightBrowser != null) {
            playwrightBrowser.close();
        }
        if (playwright != null) {
            playwright.close();
        }
        if (seleniumDriver != null) {
            seleniumDriver.quit(); // This closes the actual browser instance
        }
    }
}

junit-platform.properties

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent

The Gridlastic grid hub, credentials and video url used in these code examples are presented after launching your Gridlastic selenium grid.

Get a free account and launch your Gridlastic selenium grid, then run this code locally and test your grid!