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)));
}