Skip to content

Commit 06feb1f

Browse files
committed
chore: Add non-W3C Location-management endpoints deprecated in Selenium client
1 parent 43bbd48 commit 06feb1f

File tree

7 files changed

+109
-8
lines changed

7 files changed

+109
-8
lines changed

src/main/java/io/appium/java_client/AppiumDriver.java

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class AppiumDriver extends RemoteWebDriver implements
7373
// frequently used command parameters
7474
@Getter
7575
private final URL remoteAddress;
76+
@Deprecated(forRemoval = true)
7677
protected final RemoteLocationContext locationContext;
7778
private final ExecuteMethod executeMethod;
7879
private final Set<String> absentExtensionNames = new HashSet<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client;
18+
19+
/**
20+
* Represents the physical location.
21+
*/
22+
public class Location extends org.openqa.selenium.html5.Location {
23+
private final double latitude;
24+
private final double longitude;
25+
private final double altitude;
26+
27+
/**
28+
* Create {@link Location} with latitude, longitude and altitude values.
29+
*
30+
* @param latitude latitude value.
31+
* @param longitude longitude value.
32+
* @param altitude altitude value.
33+
*/
34+
public Location(double latitude, double longitude, double altitude) {
35+
super(latitude, longitude, altitude);
36+
this.latitude = latitude;
37+
this.longitude = longitude;
38+
this.altitude = altitude;
39+
}
40+
41+
public double getLatitude() {
42+
return latitude;
43+
}
44+
45+
public double getLongitude() {
46+
return longitude;
47+
}
48+
49+
public double getAltitude() {
50+
return altitude;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return String.format("Latitude: %s, Longitude: %s, Altitude: %s", latitude, longitude, altitude);
56+
}
57+
}

src/main/java/io/appium/java_client/MobileCommand.java

+6
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ public class MobileCommand {
187187
public static final String GET_CURRENT_CONTEXT_HANDLE = "getCurrentContextHandle";
188188
public static final String SWITCH_TO_CONTEXT = "switchToContext";
189189

190+
public static final String GET_LOCATION = "getLocation";
191+
public static final String SET_LOCATION = "setLocation";
192+
190193
public static final Map<String, CommandInfo> commandRepository;
191194

192195
static {
@@ -365,6 +368,9 @@ public class MobileCommand {
365368
commandRepository.put(GET_CONTEXT_HANDLES, getC("/session/:sessionId/contexts"));
366369
commandRepository.put(GET_CURRENT_CONTEXT_HANDLE, getC("/session/:sessionId/context"));
367370
commandRepository.put(SWITCH_TO_CONTEXT, postC("/session/:sessionId/context"));
371+
372+
commandRepository.put(GET_LOCATION, getC("/session/:sessionId/location"));
373+
commandRepository.put(SET_LOCATION, postC("/session/:sessionId/location"));
368374
}
369375

370376
/**

src/main/java/io/appium/java_client/android/AndroidDriver.java

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public AndroidBatteryInfo getBatteryInfo() {
252252
}
253253

254254
@Override
255+
@Deprecated(forRemoval = true)
255256
public RemoteLocationContext getLocationContext() {
256257
return locationContext;
257258
}

src/main/java/io/appium/java_client/android/geolocation/SupportsExtendedGeolocationCommands.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import io.appium.java_client.CommandExecutionHelper;
2020
import io.appium.java_client.ExecutesMethod;
21-
import org.openqa.selenium.remote.DriverCommand;
21+
import io.appium.java_client.MobileCommand;
2222

2323
import java.util.Map;
2424

@@ -31,7 +31,7 @@ public interface SupportsExtendedGeolocationCommands extends ExecutesMethod {
3131
* @param location The location object to set.
3232
*/
3333
default void setLocation(AndroidGeoLocation location) {
34-
CommandExecutionHelper.execute(this, Map.entry(DriverCommand.SET_LOCATION,
34+
CommandExecutionHelper.execute(this, Map.entry(MobileCommand.SET_LOCATION,
3535
Map.of("location", location.build())
3636
));
3737
}

src/main/java/io/appium/java_client/ios/IOSDriver.java

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class IOSAlert implements Alert {
280280
}
281281

282282
@Override
283+
@Deprecated(forRemoval = true)
283284
public RemoteLocationContext getLocationContext() {
284285
return locationContext;
285286
}

src/main/java/io/appium/java_client/remote/SupportsLocation.java

+41-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,54 @@
1616

1717
package io.appium.java_client.remote;
1818

19+
import io.appium.java_client.CommandExecutionHelper;
20+
import io.appium.java_client.ExecutesMethod;
21+
import io.appium.java_client.Location;
22+
import io.appium.java_client.MobileCommand;
1923
import org.openqa.selenium.WebDriver;
20-
import org.openqa.selenium.html5.Location;
2124
import org.openqa.selenium.html5.LocationContext;
2225
import org.openqa.selenium.remote.html5.RemoteLocationContext;
2326

24-
public interface SupportsLocation extends WebDriver, LocationContext {
25-
public RemoteLocationContext getLocationContext();
27+
import java.util.Map;
2628

29+
public interface SupportsLocation extends WebDriver, ExecutesMethod, LocationContext {
30+
31+
@Deprecated(forRemoval = true)
32+
RemoteLocationContext getLocationContext();
33+
34+
/**
35+
* Gets the physical location.
36+
*
37+
* @return A {@link Location} containing the location information. Returns null if the location is not available.
38+
*/
2739
default Location location() {
28-
return getLocationContext().location();
40+
Map<String, Number> result = CommandExecutionHelper.execute(this, MobileCommand.GET_LOCATION);
41+
if (result == null) {
42+
return null;
43+
}
44+
return new Location(
45+
convertToDouble(result.get("latitude")),
46+
convertToDouble(result.get("longitude")),
47+
convertToDouble(result.get("altitude"))
48+
);
49+
}
50+
51+
private Double convertToDouble(Number number) {
52+
if (number instanceof Double) {
53+
return (Double) number;
54+
} else if (number instanceof Long) {
55+
return number.doubleValue();
56+
} else {
57+
throw new IllegalArgumentException("Can't convert to double: " + number);
58+
}
2959
}
3060

31-
default void setLocation(Location location) {
32-
getLocationContext().setLocation(location);
61+
/**
62+
* Sets the physical location.
63+
*
64+
* @param location A {@link Location} containing the new location information.
65+
*/
66+
default void setLocation(org.openqa.selenium.html5.Location location) {
67+
execute(MobileCommand.SET_LOCATION, Map.of("location", location));
3368
}
3469
}

0 commit comments

Comments
 (0)