Monday 2 December 2019

Features of Selenium 4: Chromium DevTools


    Selenium 4 alpha versions came up with much awaited native support for Chrome Dev Protocol through “DevTools” interface. This helps us getting Chrome Development properties such as Application Cache, Fetch, Network, Performance, Profiler, Resource Timing, Security and Target CDP domains etc.

    Chrome DevTools is a set of web developer tools built directly into the Google Chrome browser. DevTools can help you edit pages on-the-fly and diagnose problems quickly, which ultimately helps you build better websites, faster.

Initializing Devtools session:
You can get this devtools object from driver object. Once you receive the object, we must create a session.

@BeforeMethod
public void before(){        System.setProperty("webdriver.chrome.driver","pathoftheexefile");
    WebDriver driver=new ChromeDriver();
    driver = new ChromeDriver();
    devtools = driver.getDevTools();
    devtools.createSession();
}

Verifying the performance metrics of a page:
Most modern browsers have excellent tools that help measure the runtime performance of websites. Metrics like frame rates, paint and layout times present an aggregated state of the website’s runtime performance from these logs. Automating this process can also help us integrate these metrics into the dashboards that we use to monitor the general health of our web properties. Below code shows, how we can use devtools to get the metrics through automation.

public void getPerfMetrics(){         devTools.send(Performance.setTimeDomain(TimeDomain.ThreadTicks));
    devTools.send(enable());
    driver.get("<Ur browser url>");
    List<Metric> metrics = devTools.send(getMetrics());   
    devTools.send(disable());
}

Emulate Geo Location:
Certain applications have different features and functionalities across different locations. Automating such application is difficult using Selenium as we cannot emulate the geo location in the browser. But with the help of Devtools, we can emulate it. Below code snippet demonstrates that.

@Test
public void setGeoLocationTest(){
   driver.get("<your site url>");
   Map<String, Object> params = new HashMap<>();
   params.put("latitude", 50.2334);
   params.put("longitude", 0.2334);
   params.put("accuracy", 1);
   driver.executeCdpCommand("Emulation.setGeolocationOverride", params);
}

First, you need to find the latitude and longitude of your location. Then using executeCdpCommand, we can override the geo location.

Testing network throttling Conditions:
Testing the application in different network conditions(i.e 2G, 3G, 4G etc) is always challenging. But we can easily do it using devtools as part of our automation itself.

private static void enableNetworkOffline() {
  devTools.send(Network.enable(of(100000000), empty(), empty()));
  devTools.send(emulateNetworkConditions(true, 100, 1000, 2000,
                of(ConnectionType.cellular3g));
  driver.get(“<your browser>”);
}

The above example is to emulate 3G speed in your browser. You can also add 2G, 4G etc in the same way. We can also emulate ‘offline’ condition by just sending ‘false’ as the first parameter in emulateNetworkConditions().

Loading Insecure websites:
When you have a website which has SSL certificates, it is difficult to load and proceed further in the automation. We can use this dev tools to ignore that SSL certificate errors and load the website. Code snippet is as below.



@Test
public void loadInsecureWebsite() {
   chromeDevTools.send(Security.enable());
   chromeDevTools.send(Security.setIgnoreCertificateErrors(true));
   driver.get("https://expired.badssl.com/");
}

Verifying Console messages:
We can also verify the console logs using devtools. The below snippet verifies the given message from the console logs.

private static void consoleLogs(String message) {
   devTools.send(Console.enable());
   //add listener to verify the console message
   devTools.addListener(Console.messageAdded(),    consoleMessageFromDevTools -> assertEquals(true, consoleMessageFromDevTools.getText().equals(message)));
}


3 comments:

  1. I am looking for capturing performance metrics. But this line is not working for me
    devTools.send(Performance.setTimeDomain(TimeDomain.ThreadTicks));
    Can you help me with this or if you have any java code file.

    ReplyDelete
  2. to send perf metrics you need to add the following command
    devTools.send(Performance.enable(Optional.empty()));
    command given above "devTools.send(enable());" does not work any more.

    ReplyDelete
  3. Great insights, Nainappa! Your approach to automating Tableau dashboard testing using Tableau’s REST APIs is impressive and very practical. It’s fantastic to see how you found a reliable and lightweight alternative to traditional UI automation tools. For those interested in mastering such techniques, I highly recommend checking out Naresh-IT’s Selenium Online Training. Their comprehensive course covers everything you need to become proficient in automation testing. Kudos to you for sharing this valuable method and making complex tasks more manageable! Keep up the excellent work!

    ReplyDelete