How to handle the Location permission(GeoLocation) in desktop for your website?
Hey @saanvi.savlani thank you for reachng out to us,
If you ever encounter this location prompt and don’t know how to handle it while running your automation scripts then please follow the below confihgaure tion
If you are using Chrome then use the below code :
ChromeOptions browserOptions = new ChromeOptions();
// Set preferences for handling permissions
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.geolocation", 2);
prefs.put("profile.default_content_setting_values.notifications", 1);
prefs.put("profile.default_content_setting_values.media_stream_mic", 1);
prefs.put("profile.default_content_setting_values.media_stream_camera",
browserOptions.setExperimentalOption("prefs", prefs);
// Set additional capabilities
browserOptions.setPlatformName("Windows 10");
browserOptions.setBrowserVersion("121");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("username", "LT_USERNAME");
ltOptions.put("accessKey", "LT_ACCESS_KEY");
ltOptions.put("project", "GeoLocationTest");
ltOptions.put("w3c", true);
browserOptions.setCapability("LT:Options", ltOptions);
If you are using Firfox then use the below code :
FirefoxOptions browserOptions = new FirefoxOptions();
// Create a new Firefox profile
FirefoxProfile profile = new FirefoxProfile();
// Set preferences for handling permissions
profile.setPreference("permissions.default.geo", 1); // 1: Allow, 2: Block
profile.setPreference("permissions.default.desktop-notification", 1); // 1: Allow, 2: Block
profile.setPreference("permissions.default.microphone", 1); // 1: Allow, 2: Block
profile.setPreference("permissions.default.camera", 1); // 1: Allow, 2: Block
browserOptions.setProfile(profile);
browserOptions.setPlatformName("Windows 10");
browserOptions.setBrowserVersion("118");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("username", "LT_USERNAME");
ltOptions.put("accessKey", "LT_ACCESS_KEY");
ltOptions.put("project", "GeoLocationTest");
ltOptions.put("w3c", true);
browserOptions.setCapability("LT:Options", ltOptions);
Just a small note to rememeber that in case of FF, we have observed that it works well with FF version 118 and below (validated till 115) and does not work with FF version 119 and above.
Also just putting the entire code sample here for your reference:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.util.HashMap;
public class geoLocationTestAmit {
public static void main(String[] args) {
try {
ChromeOptions browserOptions = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.geolocation", 2);
prefs.put("profile.default_content_setting_values.notifications", 1);
prefs.put("profile.default_content_setting_values.media_stream_mic", 1);
prefs.put("profile.default_content_setting_values.media_stream_camera", 1);
browserOptions.setExperimentalOption("prefs", prefs);
browserOptions.setPlatformName("Windows 10");
browserOptions.setBrowserVersion("121");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("username", "LT_USERNAME");
ltOptions.put("accessKey", "LT_ACCESS_KEY");
ltOptions.put("project", "GeoLocationTest");
ltOptions.put("w3c", true);
browserOptions.setCapability("LT:Options", ltOptions);
WebDriver driver = new RemoteWebDriver(new URL(
"https://LT_USERNAME:LT_ACCESS_KEY@hub.lambdatest.com/wd/hub"),
browserOptions);
driver.get("https://the-internet.herokuapp.com/geolocation");
Thread.sleep(10000);
driver.findElement(By.xpath("//*[text()='Where am I?']")).click();
Thread.sleep(5000);
driver.quit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hope this helped answer your query , if in case of any more doubts and need clarification feel free to use this thread or reach us out .