diff --git a/java/src/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideParameters.java b/java/src/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideParameters.java index 5f05bcbdf6c44..9bdb23be7f936 100644 --- a/java/src/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideParameters.java +++ b/java/src/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideParameters.java @@ -22,9 +22,10 @@ public class SetGeolocationOverrideParameters extends AbstractOverrideParameters // Constructor for coordinates - must specify either contexts or userContexts later public SetGeolocationOverrideParameters(GeolocationCoordinates coordinates) { if (coordinates == null) { - throw new IllegalArgumentException("GeolocationCoordinates cannot be null"); + map.put("coordinates", null); + } else { + map.put("coordinates", coordinates.toMap()); } - map.put("coordinates", coordinates.toMap()); } // Constructor for error - must specify either contexts or userContexts later diff --git a/java/test/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideTest.java b/java/test/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideTest.java index d183c996cb4f9..c7d3be51ac596 100644 --- a/java/test/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideTest.java +++ b/java/test/org/openqa/selenium/bidi/emulation/SetGeolocationOverrideTest.java @@ -18,6 +18,7 @@ package org.openqa.selenium.bidi.emulation; import static java.lang.Math.abs; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; import java.util.List; @@ -90,18 +91,15 @@ void canSetGeolocationOverrideWithCoordinatesInContext() { Object result = getBrowserGeolocation(driver, null, origin); Map r = ((Map) result); - assert !r.containsKey("error") : "Geolocation failed with error: " + r.get("error"); + assertThat(r.containsKey("error")).isFalse(); double latitude = ((Number) r.get("latitude")).doubleValue(); double longitude = ((Number) r.get("longitude")).doubleValue(); double accuracy = ((Number) r.get("accuracy")).doubleValue(); - assert abs(latitude - coords.getLatitude()) < 0.0001 - : "Latitude mismatch: expected " + coords.getLatitude() + ", got " + latitude; - assert abs(longitude - coords.getLongitude()) < 0.0001 - : "Longitude mismatch: expected " + coords.getLongitude() + ", got " + longitude; - assert abs(accuracy - coords.getAccuracy()) < 0.0001 - : "Accuracy mismatch: expected " + coords.getAccuracy() + ", got " + accuracy; + assertThat(abs(latitude - coords.getLatitude())).isLessThan(0.0001); + assertThat(abs(longitude - coords.getLongitude())).isLessThan(0.0001); + assertThat(abs(accuracy - coords.getAccuracy())).isLessThan(0.0001); } @Test @@ -134,15 +132,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() { Map r = (Map) getBrowserGeolocation(driver, userContext1, origin1); - assert !r.containsKey("error") : "Context1 geolocation failed with error: " + r.get("error"); + assertThat(r.containsKey("error")).isFalse(); double latitude1 = ((Number) r.get("latitude")).doubleValue(); double longitude1 = ((Number) r.get("longitude")).doubleValue(); double accuracy1 = ((Number) r.get("accuracy")).doubleValue(); - assert abs(latitude1 - coords.getLatitude()) < 0.0001 : "Context1 latitude mismatch"; - assert abs(longitude1 - coords.getLongitude()) < 0.0001 : "Context1 longitude mismatch"; - assert abs(accuracy1 - coords.getAccuracy()) < 0.0001 : "Context1 accuracy mismatch"; + assertThat(abs(latitude1 - coords.getLatitude())).isLessThan(0.0001); + assertThat(abs(longitude1 - coords.getLongitude())).isLessThan(0.0001); + assertThat(abs(accuracy1 - coords.getAccuracy())).isLessThan(0.0001); driver.switchTo().window(context2.getId()); String url2 = appServer.whereIsSecure("blank.html"); @@ -154,15 +152,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() { Map r2 = (Map) getBrowserGeolocation(driver, userContext2, origin2); - assert !r2.containsKey("error") : "Context2 geolocation failed with error: " + r2.get("error"); + assertThat(r2.containsKey("error")).isFalse(); double latitude2 = ((Number) r2.get("latitude")).doubleValue(); double longitude2 = ((Number) r2.get("longitude")).doubleValue(); double accuracy2 = ((Number) r2.get("accuracy")).doubleValue(); - assert abs(latitude2 - coords.getLatitude()) < 0.0001 : "Context2 latitude mismatch"; - assert abs(longitude2 - coords.getLongitude()) < 0.0001 : "Context2 longitude mismatch"; - assert abs(accuracy2 - coords.getAccuracy()) < 0.0001 : "Context2 accuracy mismatch"; + assertThat(abs(latitude2 - coords.getLatitude())).isLessThan(0.0001); + assertThat(abs(longitude2 - coords.getLongitude())).isLessThan(0.0001); + assertThat(abs(accuracy2 - coords.getAccuracy())).isLessThan(0.0001); context1.close(); context2.close(); @@ -171,6 +169,7 @@ void canSetGeolocationOverrideWithMultipleUserContexts() { } @Test + @NeedsFreshDriver @Ignore(FIREFOX) void canSetGeolocationOverrideWithError() { @@ -192,7 +191,49 @@ void canSetGeolocationOverrideWithError() { Object result = getBrowserGeolocation(driver, null, origin); Map r = ((Map) result); - assert r.containsKey("error") : "Expected geolocation to fail with error, but got: " + r; + assertThat(r.containsKey("error")).isTrue(); + + context.close(); + } + + @Test + @NeedsFreshDriver + void canResetGeolocationOverrideWithNullCoordinates() { + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); + String contextId = context.getId(); + + String url = appServer.whereIsSecure("blank.html"); + context.navigate(url, ReadinessState.COMPLETE); + driver.switchTo().window(context.getId()); + + String origin = + (String) ((JavascriptExecutor) driver).executeScript("return window.location.origin;"); + + Emulation emul = new Emulation(driver); + + GeolocationCoordinates coords = new GeolocationCoordinates(37.7749, -122.4194); + emul.setGeolocationOverride( + new SetGeolocationOverrideParameters(coords).contexts(List.of(contextId))); + + Object firstResult = getBrowserGeolocation(driver, null, origin); + Map r1 = ((Map) firstResult); + + assertThat(r1.containsKey("error")).isFalse(); + double latitude1 = ((Number) r1.get("latitude")).doubleValue(); + double longitude1 = ((Number) r1.get("longitude")).doubleValue(); + + assertThat(abs(latitude1 - coords.getLatitude())).isLessThan(0.0001); + assertThat(abs(longitude1 - coords.getLongitude())).isLessThan(0.0001); + + emul.setGeolocationOverride( + new SetGeolocationOverrideParameters((GeolocationCoordinates) null) + .contexts(List.of(contextId))); + + Object secondResult = getBrowserGeolocation(driver, null, origin); + Map r2 = ((Map) secondResult); + + // Error because there's no real geolocation available + assertThat(r2.containsKey("error")).isTrue(); context.close(); }