|
| 1 | +package com.lambdatest; |
| 2 | + |
| 3 | +import java.net.MalformedURLException; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.JavascriptExecutor; |
| 11 | +import org.openqa.selenium.WebDriver; |
| 12 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 13 | +import org.openqa.selenium.devtools.DevTools; |
| 14 | +import org.openqa.selenium.devtools.HasDevTools; |
| 15 | +import org.openqa.selenium.devtools.v96.emulation.Emulation; |
| 16 | +import org.openqa.selenium.remote.Augmenter; |
| 17 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 18 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 19 | + |
| 20 | +public class EmulateGeolocation { |
| 21 | + |
| 22 | + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; |
| 23 | + private WebDriver driver; |
| 24 | + |
| 25 | + public void setup() throws MalformedURLException { |
| 26 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 27 | + capabilities.setCapability("browserName", "Chrome"); |
| 28 | + capabilities.setCapability("browserVersion", "latest"); |
| 29 | + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); |
| 30 | + ltOptions.put("user", System.getenv("LT_USERNAME")); |
| 31 | + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); |
| 32 | + ltOptions.put("build", "Selenium 4"); |
| 33 | + ltOptions.put("name", this.getClass().getName()); |
| 34 | + ltOptions.put("platformName", "Windows 10"); |
| 35 | + ltOptions.put("seCdp", true); |
| 36 | + ltOptions.put("selenium_version", "4.0.0"); |
| 37 | + capabilities.setCapability("LT:Options", ltOptions); |
| 38 | + |
| 39 | + ChromeOptions options = new ChromeOptions(); |
| 40 | + |
| 41 | + Map<String, Object> prefs = new HashMap<String, Object>(); |
| 42 | + prefs.put("googlegeolocationaccess.enabled", true); |
| 43 | + prefs.put("profile.default_content_setting_values.geolocation", 1); // 1:allow 2:block |
| 44 | + prefs.put("profile.default_content_setting_values.notifications", 1); |
| 45 | + prefs.put("profile.managed_default_content_settings", 1); |
| 46 | + options.setExperimentalOption("prefs", prefs); |
| 47 | + capabilities.setCapability(ChromeOptions.CAPABILITY, options); |
| 48 | + |
| 49 | + driver = new RemoteWebDriver(new URL(hubURL), capabilities); |
| 50 | + |
| 51 | + System.out.println(driver); |
| 52 | + } |
| 53 | + |
| 54 | + public void emulateGeolocation() { |
| 55 | + Augmenter augmenter = new Augmenter(); |
| 56 | + driver = augmenter.augment(driver); |
| 57 | + |
| 58 | + DevTools devTools = ((HasDevTools) driver).getDevTools(); |
| 59 | + devTools.createSession(); |
| 60 | + |
| 61 | + // setGeolocationOverride() takes input lattitude, longitude and accuracy as |
| 62 | + // parameters. |
| 63 | + devTools.send(Emulation.setGeolocationOverride(Optional.of(28.622409), |
| 64 | + Optional.of(77.364925), |
| 65 | + Optional.of(1))); |
| 66 | + driver.get("https://my-location.org"); |
| 67 | + |
| 68 | + String address = driver.findElement(By.id("address")).getText(); |
| 69 | + System.out.println(address); |
| 70 | + if (address.contains("Noida")) { |
| 71 | + markStatus("passed", "I am in Noida", driver); |
| 72 | + } else { |
| 73 | + markStatus("failed", "I am not in Noida", driver); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public void tearDown() { |
| 79 | + try { |
| 80 | + driver.quit(); |
| 81 | + |
| 82 | + } catch ( |
| 83 | + |
| 84 | + Exception e) { |
| 85 | + markStatus("failed", "Got exception!", driver); |
| 86 | + e.printStackTrace(); |
| 87 | + driver.quit(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static void markStatus(String status, String reason, WebDriver driver) { |
| 92 | + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; |
| 93 | + jsExecute.executeScript("lambda-status=" + status); |
| 94 | + System.out.println(reason); |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String[] args) throws MalformedURLException, InterruptedException { |
| 98 | + EmulateGeolocation test = new EmulateGeolocation(); |
| 99 | + test.setup(); |
| 100 | + test.emulateGeolocation(); |
| 101 | + test.tearDown(); |
| 102 | + } |
| 103 | +} |
0 commit comments