Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,18 +91,15 @@ void canSetGeolocationOverrideWithCoordinatesInContext() {
Object result = getBrowserGeolocation(driver, null, origin);
Map<String, Object> r = ((Map<String, Object>) 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
Expand Down Expand Up @@ -134,15 +132,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
Map<String, Object> r =
(Map<String, Object>) 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");
Expand All @@ -154,15 +152,15 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
Map<String, Object> r2 =
(Map<String, Object>) 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();
Expand All @@ -171,6 +169,7 @@ void canSetGeolocationOverrideWithMultipleUserContexts() {
}

@Test
@NeedsFreshDriver
@Ignore(FIREFOX)
void canSetGeolocationOverrideWithError() {

Expand All @@ -192,7 +191,49 @@ void canSetGeolocationOverrideWithError() {
Object result = getBrowserGeolocation(driver, null, origin);
Map<String, Object> r = ((Map<String, Object>) 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<String, Object> r1 = ((Map<String, Object>) 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<String, Object> r2 = ((Map<String, Object>) secondResult);

// Error because there's no real geolocation available
assertThat(r2.containsKey("error")).isTrue();

context.close();
}
Expand Down