Skip to content

[java][BiDi] implement emulation #16070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: trunk
Choose a base branch
from
23 changes: 23 additions & 0 deletions java/src/org/openqa/selenium/bidi/emulation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("//java:defs.bzl", "java_library")

java_library(
name = "emulation",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
],
)
46 changes: 46 additions & 0 deletions java/src/org/openqa/selenium/bidi/emulation/Emulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.emulation;

import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.internal.Require;

public class Emulation {
private final BiDi bidi;

public Emulation(WebDriver driver) {
Require.nonNull("WebDriver", driver);

if (!(driver instanceof HasBiDi)) {
throw new IllegalArgumentException("WebDriver must implement BiDi interface");
}

this.bidi = ((HasBiDi) driver).getBiDi();
}

public Map<String, Object> setGeolocationOverride(SetGeolocationOverrideParameters parameters) {
Require.nonNull("SetGeolocationOverride parameters", parameters);

return bidi.send(
new Command<>("emulation.setGeolocationOverride", parameters.toMap(), Map.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.emulation;

import java.util.HashMap;
import java.util.Map;

public class GeolocationCoordinates {
private final double latitude;
private final double longitude;
private final double accuracy;
private final Double altitude;
private final Double altitudeAccuracy;
private final Double heading;
private final Double speed;

// Constructor with default accuracy = 1.0
public GeolocationCoordinates(
double latitude,
double longitude,
Double altitude,
Double altitudeAccuracy,
Double heading,
Double speed) {
this(latitude, longitude, 1.0, altitude, altitudeAccuracy, heading, speed);
}

public GeolocationCoordinates(
double latitude,
double longitude,
double accuracy,
Double altitude,
Double altitudeAccuracy,
Double heading,
Double speed) {

if (latitude < -90.0 || latitude > 90.0) {
throw new IllegalArgumentException("Latitude must be between -90.0 and 90.0");
}
if (longitude < -180.0 || longitude > 180.0) {
throw new IllegalArgumentException("Longitude must be between -180.0 and 180.0");
}
if (accuracy < 0.0) {
throw new IllegalArgumentException("Accuracy must be >= 0.0");
}
if (altitudeAccuracy != null && altitude == null) {
throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude");
}
if (altitudeAccuracy != null && altitudeAccuracy < 0.0) {
throw new IllegalArgumentException("Altitude accuracy must be >= 0.0");
}
if (heading != null && (heading < 0.0 || heading >= 360.0)) {
throw new IllegalArgumentException("Heading must be between 0.0 and 360.0");
}
if (speed != null && speed < 0.0) {
throw new IllegalArgumentException("Speed must be >= 0.0");
}

this.latitude = latitude;
this.longitude = longitude;
this.accuracy = accuracy;
this.altitude = altitude;
this.altitudeAccuracy = altitudeAccuracy;
this.heading = heading;
this.speed = speed;
}

public double getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

public double getAccuracy() {
return accuracy;
}

public Double getAltitude() {
return altitude;
}

public Double getAltitudeAccuracy() {
return altitudeAccuracy;
}

public Double getHeading() {
return heading;
}

public Double getSpeed() {
return speed;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("latitude", getLatitude());
map.put("longitude", getLongitude());
map.put("accuracy", getAccuracy());
if (getAltitude() != null) {
map.put("altitude", getAltitude());
}
if (getAltitudeAccuracy() != null) {
map.put("altitudeAccuracy", getAltitudeAccuracy());
}
if (getHeading() != null) {
map.put("heading", getHeading());
}
if (getSpeed() != null) {
map.put("speed", getSpeed());
}
return Map.copyOf(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.emulation;

import java.util.Map;

public class GeolocationPositionError {
String type = "positionUnavailable";
Copy link
Preview

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field should be private and final since it's not intended to be modified. Consider adding 'private final' modifiers.

Suggested change
String type = "positionUnavailable";
private final String type = "positionUnavailable";

Copilot uses AI. Check for mistakes.


public Map<String, Object> toMap() {
return Map.of("type", type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.emulation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetGeolocationOverrideParameters {
private final GeolocationCoordinates coordinates;
private final GeolocationPositionError error;
private final List<String> contexts;
private final List<String> userContexts;

SetGeolocationOverrideParameters(
Copy link
Preview

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor has package-private visibility, which may limit API usability. Consider making it public to allow external instantiation of parameters.

Suggested change
SetGeolocationOverrideParameters(
public SetGeolocationOverrideParameters(

Copilot uses AI. Check for mistakes.

GeolocationCoordinates coordinates,
GeolocationPositionError error,
List<String> contexts,
List<String> userContexts) {

this.coordinates = coordinates;
this.error = error;
this.contexts = contexts;
this.userContexts = userContexts;

if (this.coordinates != null && this.error != null) {
throw new IllegalArgumentException("Cannot specify both coordinates and error");
}
if (this.contexts != null && this.userContexts != null) {
throw new IllegalArgumentException("Cannot specify both contexts and userContexts");
}

if (this.contexts == null && this.userContexts == null) {
throw new IllegalArgumentException("Must specify either contexts or userContexts");
}
}

public Map<String, Object> toMap() {
Map<String, Object> param = new HashMap<>();

if (this.coordinates != null) {
param.put("coordinates", this.coordinates.toMap());
}

if (this.error != null) {
param.put("error", this.error.toMap());
}

if (this.contexts != null) {
param.put("contexts", this.contexts);
} else {
param.put("userContexts", this.userContexts);
}

return Map.copyOf(param);
}
}
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/bidi/module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browser",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/emulation",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/network",
"//java/src/org/openqa/selenium/bidi/permissions",
Expand Down
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ java_export(
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi:augmenter",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/emulation",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/module",
"//java/src/org/openqa/selenium/bidi/network",
Expand Down
36 changes: 36 additions & 0 deletions java/test/org/openqa/selenium/bidi/emulation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite")

java_selenium_test_suite(
name = "large-tests",
size = "large",
srcs = glob(["*Test.java"]),
browsers = [
"firefox",
"chrome",
],
tags = [
"selenium-remote",
],
deps = [
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/emulation",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/module",
"//java/src/org/openqa/selenium/bidi/network",
"//java/src/org/openqa/selenium/bidi/script",
"//java/src/org/openqa/selenium/firefox",
"//java/src/org/openqa/selenium/grid/security",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/support",
"//java/test/org/openqa/selenium/environment",
"//java/test/org/openqa/selenium/testing:annotations",
"//java/test/org/openqa/selenium/testing:test-base",
"//java/test/org/openqa/selenium/testing/drivers",
artifact("com.google.guava:guava"),
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
] + JUNIT5_DEPS,
)
Loading
Loading