package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v119.emulation.Emulation;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.Map;
import java.util.Optional;
//Reference: https://gist.github.com/ShamaUgale/552df64db476f26459a38e69fa02cfbe
public class GeoLocationTest {
private RemoteWebDriver driver;
@BeforeClass
public void setup() {
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = Map.of(
"profile.default_content_settings.geolocation", 1,
"googlegeolocationaccess.enabled", true
);
options.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(options);
}
@Test
public void testMethod() {
double expectedLatitude = 37.615223;
double expectedLongitude = -122.389977;
DevTools devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
devTools.send(Emulation.setGeolocationOverride(Optional.of(expectedLatitude),
Optional.of(expectedLongitude), Optional.of(100)));
driver.get("https://mycurrentlocation.net/");
WebElement btn = driver.findElement(By.xpath("//button[@class='btn btn-warning mt-3 main-button']"));
btn.click();
dismissAlertsSilently();
double actualLatitude = Double.parseDouble(driver.findElement(By.id("detail-latitude")).getText());
System.err.println("Latitude = " + actualLatitude);
double actualLongitude = Double.parseDouble(driver.findElement(By.id("detail-longitude")).getText());
System.err.println("Longitude = " + actualLongitude);
Assert.assertEquals(expectedLatitude, actualLatitude);
Assert.assertEquals(expectedLongitude, actualLongitude);
}
@AfterClass
public void cleanup() {
driver.quit();
}
private void dismissAlertsSilently() {
try {
driver.switchTo().alert().dismiss();
} catch (WebDriverException e) {
System.err.println(e.getMessage());
}
}
}
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/570b28f8-caa1-49e0-9e8e-41c59eeda3bcn%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/JYCt1X1Tfjw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CANikZLnpxjKe94b6UW%3DO%3DEq6ruyfGiwap_UQEWeb5jNg8rMkyA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/708fafcb-e243-432a-a71e-8b34e51e8a95n%40googlegroups.com.