Features
< Back to Blog Overview

Run Selenium 4 BiDi tests on TestingBot

2024-08-14
Selenium WebDriver BiDi
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

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 to true
  • 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.

Copy code
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.

TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Test your website and mobile apps on iOS 18.

With the preview release of macOS Sequoia testing, TestingBot now also supports running automated, manual and visual tests against iOS 18 Simulator...

Read more
Test on macOS Sequoia

TestingBot now offers macOS Sequoia beta testing for automated, visual and manual tests. This update allows developers to run tests on the upcoming...

Read more
Visual Regression Testing

TestingBot released visual regression testing in Q1 of 2024. With this release, testers can now automatically discover visual defects on their we...

Read more
blank

Today we have added PhantomJS to our Linux VMs on our Selenium Grid. You can now run your WebDriver tests on PhantomJS + Ubuntu. PhantomJS is a he...

Read more
blank

We’re excited to announce that starting from today, our customers can choose to either run their tests on our existing US cloud or on our new Europ...

Read more
TestingBot Browser Extension

TestingBot has released a Chrome Browser Extension and a Firefox Extension, which will allow you to test any webpage, on a remote browser, with t...

Read more