Selenium 4 is the latest version of the widely-used open-source browser testing framework. A new feature introduced in this version is support for the WebDriver BiDi protocol.
TestingBot is happy to announce that it is now possible to run Selenium automated tests on the TestingBot browser grid with WebDriver Bidi.
The WebDriver BiDi protocol is designed to offer a stable, cross-browser API that enables bidirectional communication between the test script and the (remote) browser. This new protocol improves upon learnings from the WebDriver protocol, which is based on HTTP and only supports unidirectional communication.
With the BiDi protocol, communication occurs over WebSockets through JSON-RPC messaging. This unlocks new capabilities such as intercepting network requests, mocking backends, performing basic authentication and accessing console logs in real-time.
To get started, please read the Selenium BiDi documentation which consists of examples on how to run tests with BiDi.
Important to remember is that you need at least 2 capabilities in your Selenium tests to use WebDriver BiDi:
-
webSocketUrl
capability set totrue
-
selenium-version
capability set to Selenium 4, for example4.23.0
Browser support for BiDi
Important to know is that Chrome, Edge and Firefox currently fully support WebDriver bidi, while other browser vendors such as Safari have yet to implement this new protocol.
This means you can only run BiDi tests on remote Chrome, Firefox and Edge browsers on TestingBot.
Listen to console.log events
One of the useful new features that comes with BiDi is the ability to listen to browser events as they happen. For example, let's say you want to retrieve in realtime the console.logs that are generated while testing a website.
Below is an example on how to set this up with Java. For other programming languages, please see the console bidi examples.
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.module.LogInspector;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class ListenToConsoleLogs {
public static final String KEY = "KEY";
public static final String SECRET = "SECRET";
public static final String REMOTE_URL = "https://" + KEY + ":" + SECRET + "@hub.testingbot.com/wd/hub";
static Boolean success = false;
public static void main(String[] args) throws MalformedURLException {
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("webSocketUrl", true);
HashMap<String, Object> testingbotOptions = new HashMap<>();
testingbotOptions.put("selenium-version", "4.23.0");
capabilities.setCapability("tb:options", testingbotOptions);
WebDriver driver = new RemoteWebDriver(new URL(REMOTE_URL), capabilities);
try {
Augmenter augmenter = new Augmenter();
driver = augmenter.augment(driver);
try (LogInspector logInspector = new LogInspector(driver)) {
logInspector.onConsoleEntry(consoleLogEntry -> {
System.out.println("text: " + consoleLogEntry.getText());
System.out.println("level: " + consoleLogEntry.getLevel());
success = true;
});
String page = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";
driver.get(page);
driver.findElement(By.id("consoleLog")).click();
if (success) {
markTestStatus(true, "Console logs streaming", driver);
} else {
markTestStatus(false, "Console logs did not stream", driver);
}
driver.quit();
}
} catch (Exception e) {
markTestStatus(false, "Exception!", driver);
e.printStackTrace();
driver.quit();
}
}
public static void markTestStatus(Boolean status, String reason, WebDriver driver) {
TestingbotREST r = new TestingbotREST("key", "secret");
Map<String, String> data = new HashMap<String, String>();
data.put("success", "1");
data.put("name", "My Test");
r.updateTest(driver.getSessionId(), data);
}
}
Future features for BiDi
The bidirectional communication will introduce exciting new concepts and features for tests, which may include new features such as:
- Listen for DOM events
- Dynamic changes to iframe or documents
- Performance timings
- Bootstrap Scripts
WebDriver Bidi is already gaining traction amongst testers, for example Puppeteer will now use BiDi for Firefox automation, which means support for CDP in Firefox will be removed.
If you would like to get started with BiDi, feel free to sign up for a free trial and start running tests on remote browsers.