Use Selenium BiDi with Perfecto

Perfecto supports Selenium BiDirectional (BiDi) functionality, allowing you, for example, to inspect console logs during browser sessions. Read this article to learn how to enable Selenium BiDirectional (BiDi) functionality in Perfecto and capture console logs during browser sessions.

To learn more, see Selenium's Bidirectional functionality documentation.

Enable BiDi in Perfecto

  1. Add the capability to your automation script:

    Copy
    capabilities.setCapability("webSocketUrl", true);
  2. Upgrade the driver using the following:

    Copy
    Augmenter augmenter = new Augmenter();
    driver = augmenter.augment(driver);
  3. Use BiDi modules such as LogInspector to capture console logs.

Example code

Copy
import org.openqa.selenium.*;

import org.openqa.selenium.bidi.module.LogInspector;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class seleniumBidiTest {
    public static void main(String[] args) throws MalformedURLException {

        ChromeOptions capabilities = new ChromeOptions();
        Map<String, Object> perfectoOptions = new HashMap<>();

        String cloud;
        cloud = "web-demo2";
        //need to add capability for bidi to work
        capabilities.setCapability("webSocketUrl", true);

        capabilities.setCapability("perfecto:securityToken", "*******");
        capabilities.setCapability("browserName", "Chrome");
        capabilities.setCapability("platformName", "Windows");
        capabilities.setCapability("perfecto:options", perfectoOptions);

        WebDriver driver = new RemoteWebDriver(new URL("http://" + cloud + ".perfectomobile.com/nexperience/perfectomobile/wd/hub"), capabilities);
        long start = System.currentTimeMillis();
        try {
            //upgrade the driver to bidi driver
            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());
                });
            }
            driver.get("https://www.perforce.com");
        } catch (Exception e) {
            System.out.println("Time taken: " + (System.currentTimeMillis() - start) + " ms");
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }
}