Selenium 4 supports to automate the chrome dev tools features, in which we have as option to emulate geoLocation & timeZones so the application can pickup the specified locations instead of the default or current location.
This will help to validate the applications functionality which are based on locations like store addresses across different countries or language etc
The same thing with mocking time zones, we can validate the applications those provide different features based on different time zones, so we can emulate specific time zone and validate the functionalities.
Watch the demo here –
To implement the mocking, we need to use method executeCdpCommand
, this method accepts 2 parameters 1. command 2. params
All the commands can be found from selenium cdp commands
For Mocking geoLocation –
we have to use command – Emulation.setGeolocationOverride
Note – make sure the the driver should be ChromiumDriver i.e – either ChromeDriver or EdgeDriver
package sel4; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.network.Network; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.time.Duration; import java.util.HashMap; import java.util.Map; import java.util.Optional; import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable; public class Emulate_Geo_TimeZone { WebDriver driver; DevTools devTools; WebDriverWait wait; @BeforeMethod void Login() throws InterruptedException { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); devTools = ((ChromeDriver)driver).getDevTools(); wait = new WebDriverWait(driver, Duration.ofSeconds(5)); } @Test public void emulateGeoLocation() throws InterruptedException { //https://www.selenium.dev/selenium/docs/api/rb/Selenium/WebDriver/DevTools/Emulation.html driver.get("https://the-internet.herokuapp.com/geolocation"); By Where = By.xpath("//button[contains(text(),'Where am I')]"); wait.until(elementToBeClickable(driver.findElement(Where))).click(); Thread.sleep(8000); Map<String, Object> params = new HashMap<>(); params.put("latitude", 51.5055); params.put("longitude", 0.0754); params.put("accuracy", 1); ((ChromeDriver)driver).executeCdpCommand("Emulation.setGeolocationOverride", params); wait.until(elementToBeClickable(driver.findElement(Where))).click(); Thread.sleep(3000); driver.quit(); } @Test public void emulateGeoLocation_1() throws InterruptedException { driver.get("https://mycurrentlocation.net/"); Thread.sleep(8000); Map<String, Object> params = new HashMap<>(); params.put("latitude", 41.881832); params.put("longitude", -87.623177); params.put("accuracy", 1); ((ChromeDriver)driver).executeCdpCommand("Emulation.setGeolocationOverride", params); driver.get("https://mycurrentlocation.net/"); Thread.sleep(6000); driver.quit(); } }
the 2nd parameter for the method executeCdpCommand, we are passing as Map and the values are latitude, longitude & accuracy [how close the location would be, in meters]
For mocking time zones
we have to use command – Emulation.setTimezoneOverride
@Test public void emulateTimezoneTest() throws InterruptedException { driver.get("https://whatismytimezone.com/"); Thread.sleep(4000); devTools.createSession(); Map<String, Object> map = new HashMap<>(); //https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List map.put("timezoneId", "Asia/Singapore"); devTools.send(Network.enable(Optional.of(1000000), Optional.empty(), Optional.empty())); ((ChromeDriver)driver).executeCdpCommand("Emulation.setTimezoneOverride", map); driver.get("https://whatismytimezone.com/"); Thread.sleep(2000); System.out.println(driver.findElement(By.tagName("article")).getText()); driver.quit(); }
executeCdpCommand parameter is map which is the timezoneId to mock, this is basically the continent/cityname
list of timezoneIds are available here
To mock the timeZone, we have to create a session using devTools.createSession() & enable the Network by Network.enable(…)
At the end we are just capturing the text [mocked timezone] and printing on console.