Changing geolocation in Automation testing with Java TestNG

In Automation testing, Change of geolocation can be done by using script-

String username = “USERNAME”;

String accesskey = “ACCESS KEY”;

String hub = “HUB URL”;

ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("–lang=pt-BR"); // Required locale-> pt-BR capabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptions); //Add this with desired capabilities.


driver.navigate().to(“TESTING URL”); //Testing url

Thread.sleep(5000); driver.manage().window().maximize();

JavascriptExecutor executor = (JavascriptExecutor) driver; String language = executor.executeScript(“return window.navigator.userlanguage || window.navigator.language”).toString(); // This method is used to check the browser locale is set to the expected locale.



assert language.equals(“pt-BR”); //The JS code returns the browser language which is compared with the expected locale. Assert is raised if the language does not match the expected locale.


1 Like