EmulateTimezones, CaptureNetworkPerformanceMetrics – New feature Selenium 4

Emulate Timezones, Capture Network Performance Metrics​​ – New feature in​​ Selenium 4

Let us go to the site​​ https://whatismytimezone.com/​​ 

This site provides us the information of our current timezone


Our goal here is to emulate another country’s timezone and validate the above text if it shows the respective country’s timezone (just like we had emulated another geo location in one of ours another article).​​ 

Let us launch below site that shows time zones of all the countries​​ 

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List​​ 

Let us suppose we want to emulate Asia/Tokyo

Let us also inspect the below text. Notice that it is represented by ‘article’ tagname

​​ 

Below piece of code will serve the purpose. In line#21, we are loading the current country’s timezone as is. ​​ In line#26, we are adding a key/value pair. In line#28, we are using an inbuilt method ‘setTimezoneOverride’ that overrides our current timezone. Line#31 will load and emulate the country you have mentioned in line#26​​ 

Line#33 simply prints the text.

Let us execute. Notice that it first shows us the current timezone and then switches to Tokyo’s

The console shows the Tokyo’s timezone

Let us now look at how to capture performance metrics of any website.

The​​ below piece of code will do the needful. In line#23, we are enabling the network performance. In line#25, we are creating a list of performance metrics and in line#30, we are printing the respective websites’ metrics​​ ​​ 

Run and notice below. We can now capture lot of available metrics of any website

 

Code snippet​​ (timezone)

package​​ sel4scripts;

 

import​​ java.util.HashMap;

import​​ java.util.Map;

 

import​​ org.openqa.selenium.By;

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.chrome.ChromeDriver;

 

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ MockTimezone {

 

public​​ static​​ void​​ main(String[]​​ args)​​ throws​​ InterruptedException {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

//https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List

//Will emulate your current location​​ timezone​​ 

driver.get("https://whatismytimezone.com/");

 Map<String, Object>​​ timezon​​ =​​ new​​ HashMap<>();

//Emulate Japan's​​ timezone

timezon.put("timezoneId",​​ "Asia/Tokyo");

 ((ChromeDriver)​​ driver).executeCdpCommand("Emulation.setTimezoneOverride",​​ timezon);

//Will emulate Japan's​​ timezone​​ 

driver.get("https://whatismytimezone.com/");

 System.out.println(driver.findElement(By.tagName("article")).getText());

 }

 

}

 

Code snippet (perf metrics)

package​​ sel4scripts;

 

import​​ java.util.List;

import​​ java.util.Optional;

 

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ org.openqa.selenium.devtools.DevTools;

import​​ org.openqa.selenium.devtools.v85.performance.Performance;

import​​ org.openqa.selenium.devtools.v85.performance.model.Metric;

 

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ PerformanceMetricsNetwork {

public​​ static​​ void​​ main(String[]​​ args) {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

 DevTools​​ devTools​​ = ((ChromeDriver)​​ driver).getDevTools();

devTools.createSession();

 ​​ ​​ ​​​​ 

//Start capturing network performance metrics

 ​​ ​​ ​​​​ devTools.send(Performance.enable(Optional.empty()));

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ List<Metric>​​ metList​​ =​​ devTools.send(Performance.getMetrics());

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ driver.get("https://www.amazon.com/");

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ for(Metric​​ met:​​ metList) {

 ​​ ​​ ​​​​  System.out.println(met.getName() +​​ "========="​​ +​​ met.getValue());

 ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ 

}

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/ 149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 11th Oct 2024