Selenium BiDi Example

Selenium BiDi example using BrowsingContext to create windows and tabs and switch between them.

Note: you must activate bidi like this:

options.setCapability("webSocketUrl", true);

Selenium 4.10.0+ Java BiDi Example

package test_bidi;

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
import org.openqa.selenium.bidi.browsingcontext.NavigationResult;
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.ClientConfig;

public class test_bidi {

	private static WebDriver driver;

	// NOTE: find these credentials in your Gridlastic dashboard after launching
	// your selenium grid (get a free account).
	public static String VIDEO_URL = System.getenv("VIDEO_URL"); // like https://s3-us-west-1.amazonaws.com/027a15f2-530d-31e5-f8cc-7ceaf6355377/239a51a9-c526-ceb8-9ffd-1759b782a464/play.html?
	public static String HUB_URL = System.getenv("HUB_URL"); // like "https://USERNAME:ACCESS_KEY@YOUR_SUBDOMAIN.gridlastic.com/wd/hub";

	public static void main(String[] args) throws Exception {

		String platform_name = "win10"; // win10, linux
		String browser_version = "latest"; // like 114 or latest

		// chrome
		ChromeOptions options = new ChromeOptions();
		options.setCapability("browserVersion", browser_version);
		options.setCapability("platformName", platform_name);

		// ****************** REQUIRED FOR BIDI
		options.setCapability("webSocketUrl", true);

		// Enable video recording
		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 (platform_name.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));
		driver = RemoteWebDriver.builder().address(new URL(HUB_URL)).oneOf(options).config(config).build();

		SessionId sessionId = null;

		try {
			sessionId = ((RemoteWebDriver) driver).getSessionId();
			BrowsingContext tab1 = new BrowsingContext(driver, WindowType.WINDOW, driver.getWindowHandle());
			NavigationResult navigate = tab1.navigate("https://java.com", ReadinessState.COMPLETE);
			Thread.sleep(2000); // slow down for demo purposes
			BrowsingContext tab2 = new BrowsingContext(driver, WindowType.TAB, tab1.getId());
			tab2.navigate("https://google.com", ReadinessState.COMPLETE);
			Thread.sleep(2000); // slow down for demo purposes
			driver.switchTo().window(tab1.getId());
			Thread.sleep(2000); // slow down for demo purposes
			driver.switchTo().window(tab2.getId());
			Thread.sleep(2000); // slow down for demo purposes
			System.out.println("Test Video: " + VIDEO_URL + sessionId.toString());
			driver.quit();
		} catch (Exception e) {
			e.printStackTrace();
			driver.quit();
			System.out.println("Test Video: " + VIDEO_URL + sessionId.toString());
		}

	}
}