Selenium Java - Access Device Location using Chrome Options

Selenium Java - Access Device Location using Chrome Options
Introduction
In this blog, we'll dive into how to set up Chrome Options in Selenium WebDriver with Java to let you access the device's location. This handy trick helps you automate test scenarios that use location-based features, like maps or geolocation services.
Accessing Device Location using Chrome Options
Below is a complete Selenium code example that demonstrates how to set up Chrome Options to allow geolocation access.
java
package sa.Demo.SeleniumPractice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
public class Selenium_079 {
/****
* Sample Code Definition :- Selenium - Access Device Location
* Developed By :- Pankaj Kumar
* Date of Creation :- 15-June-2024
* Project :- SAJVSAUTO - Java [Web / Mobile / API]
* SA_BLOG_CODE :- SAJVSAUTO_079
*/
@Test
public void Selenium_079() throws InterruptedException {
System.out.println("Selenium - Access Device Location");
// INIT CHROME OPTIONS
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> profile = new HashMap<String, Object>();
Map<String, Object> contentSettings = new HashMap<String, Object>();
// SET CHROME OPTIONS
// 0 - Default, 1 - Allow, 2 - Block
contentSettings.put("geolocation", 1);
profile.put("managed_default_content_settings", contentSettings);
prefs.put("profile", profile);
options.setExperimentalOption("prefs", prefs);
// Open Chrome Browser
WebDriver driver = new ChromeDriver(options);
// Maximize the Browser Window
driver.manage().window().maximize();
// Open the URL
driver.get("https://the-internet.herokuapp.com/geolocation");
// Wait for 5 Seconds
Thread.sleep(5000);
// Click on Element
driver.findElement(By.xpath("//*[@id='content']/div/button")).click();
// Wait for 5 Seconds
Thread.sleep(5000);
// Quit the Driver
driver.quit();
}
}
Explanation
Chrome Options Setup: Configures Chrome Options to allow geolocation access.
Geolocation settings are adjusted using preferences in the form of nested maps.
The
contentSettings.put("geolocation", 1)
allows geolocation prompts to be automatically accepted.
Browser Initialization: Creates a WebDriver instance with the customized Chrome Options.
Page Interaction: Opens a test page, triggers a location-based functionality by clicking a button, and processes the location access prompt.
Wait Intervals: Uses pauses (
Thread.sleep()
) for observing the workflow (optional for actual test execution).Driver Cleanup: Ensures proper closure of the WebDriver instance to release resources.
Conclusion
By using Chrome Options in Selenium, you can easily automate test cases that need to access the device's location. This method makes sure that tests relying on geolocation run smoothly without needing any manual steps.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
