Skip to content

Commit 6ac3d9b

Browse files
authored
chore: Add non-W3C Location-management endpoints deprecated in Selenium client (#2098)
1 parent c4b4b27 commit 6ac3d9b

File tree

7 files changed

+148
-4
lines changed

7 files changed

+148
-4
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,58 @@
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+
import lombok.EqualsAndHashCode;
20+
import lombok.Getter;
21+
import lombok.ToString;
22+
23+
import javax.annotation.Nullable;
24+
25+
/**
26+
* Represents the physical location.
27+
*/
28+
@Getter
29+
@ToString
30+
@EqualsAndHashCode
31+
public class Location {
32+
private final double latitude;
33+
private final double longitude;
34+
@Nullable private final Double altitude;
35+
36+
/**
37+
* Create {@link Location} with latitude, longitude and altitude values.
38+
*
39+
* @param latitude latitude value.
40+
* @param longitude longitude value.
41+
* @param altitude altitude value (can be null).
42+
*/
43+
public Location(double latitude, double longitude, @Nullable Double altitude) {
44+
this.latitude = latitude;
45+
this.longitude = longitude;
46+
this.altitude = altitude;
47+
}
48+
49+
/**
50+
* Create {@link Location} with latitude and longitude values.
51+
*
52+
* @param latitude latitude value.
53+
* @param longitude longitude value.
54+
*/
55+
public Location(double latitude, double longitude) {
56+
this(latitude, longitude, null);
57+
}
58+
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,16 @@ public AndroidBatteryInfo getBatteryInfo() {
251251
return new AndroidBatteryInfo(CommandExecutionHelper.executeScript(this, "mobile: batteryInfo"));
252252
}
253253

254+
/**
255+
* Provides the location context.
256+
*
257+
* @return instance of {@link RemoteLocationContext}
258+
* @deprecated This method, {@link org.openqa.selenium.html5.LocationContext} and {@link RemoteLocationContext}
259+
* interface are deprecated, use {@link #getLocation()} and
260+
* {@link #setLocation(io.appium.java_client.Location)} instead.
261+
*/
254262
@Override
263+
@Deprecated(forRemoval = true)
255264
public RemoteLocationContext getLocationContext() {
256265
return locationContext;
257266
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,16 @@ class IOSAlert implements Alert {
279279

280280
}
281281

282+
/**
283+
* Provides the location context.
284+
*
285+
* @return instance of {@link RemoteLocationContext}
286+
* @deprecated This method, {@link org.openqa.selenium.html5.LocationContext} and {@link RemoteLocationContext}
287+
* interface are deprecated, use {@link #getLocation()} and
288+
* {@link #setLocation(io.appium.java_client.Location)} instead.
289+
*/
282290
@Override
291+
@Deprecated(forRemoval = true)
283292
public RemoteLocationContext getLocationContext() {
284293
return locationContext;
285294
}

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

+63-2
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,80 @@
1616

1717
package io.appium.java_client.remote;
1818

19+
import com.google.common.collect.ImmutableMap;
20+
import io.appium.java_client.CommandExecutionHelper;
21+
import io.appium.java_client.ExecutesMethod;
22+
import io.appium.java_client.MobileCommand;
1923
import org.openqa.selenium.WebDriver;
24+
import org.openqa.selenium.WebDriverException;
2025
import org.openqa.selenium.html5.Location;
2126
import org.openqa.selenium.html5.LocationContext;
2227
import org.openqa.selenium.remote.html5.RemoteLocationContext;
2328

24-
public interface SupportsLocation extends WebDriver, LocationContext {
25-
public RemoteLocationContext getLocationContext();
29+
import java.util.Map;
30+
import java.util.Optional;
2631

32+
public interface SupportsLocation extends WebDriver, ExecutesMethod, LocationContext {
33+
34+
/**
35+
* Provides the location context.
36+
*
37+
* @return instance of {@link RemoteLocationContext}
38+
* @deprecated This method, {@link LocationContext} and {@link RemoteLocationContext} interface are deprecated, use
39+
* {@link #getLocation()} and {@link #setLocation(io.appium.java_client.Location)} instead.
40+
*/
41+
@Deprecated(forRemoval = true)
42+
RemoteLocationContext getLocationContext();
43+
44+
/**
45+
* Gets the current device's geolocation coordinates.
46+
*
47+
* @return A {@link Location} containing the location information. Returns null if the location is not available
48+
* @deprecated This method and whole {@link LocationContext} interface are deprecated, use {@link #getLocation()}
49+
* instead.
50+
*/
51+
@Deprecated(forRemoval = true)
2752
default Location location() {
2853
return getLocationContext().location();
2954
}
3055

56+
/**
57+
* Gets the current device's geolocation coordinates.
58+
*
59+
* @return A {@link Location} containing the location information. Throws {@link WebDriverException} if the
60+
* location is not available.
61+
*/
62+
default io.appium.java_client.Location getLocation() {
63+
Map<String, Number> result = CommandExecutionHelper.execute(this, MobileCommand.GET_LOCATION);
64+
return new io.appium.java_client.Location(
65+
result.get("latitude").doubleValue(),
66+
result.get("longitude").doubleValue(),
67+
Optional.ofNullable(result.get("altitude")).map(Number::doubleValue).orElse(null)
68+
);
69+
}
70+
71+
/**
72+
* Sets the current device's geolocation coordinates.
73+
*
74+
* @param location A {@link Location} containing the new location information.
75+
* @deprecated This method and whole {@link LocationContext} interface are deprecated, use
76+
* {@link #setLocation(io.appium.java_client.Location)} instead.
77+
*/
78+
@Deprecated(forRemoval = true)
3179
default void setLocation(Location location) {
3280
getLocationContext().setLocation(location);
3381
}
82+
83+
/**
84+
* Sets the current device's geolocation coordinates.
85+
*
86+
* @param location A {@link Location} containing the new location information.
87+
*/
88+
default void setLocation(io.appium.java_client.Location location) {
89+
ImmutableMap.Builder<String, Object> locationParameters = ImmutableMap.builder();
90+
locationParameters.put("latitude", location.getLatitude());
91+
locationParameters.put("longitude", location.getLongitude());
92+
Optional.ofNullable(location.getAltitude()).ifPresent(altitude -> locationParameters.put("altitude", altitude));
93+
execute(MobileCommand.SET_LOCATION, Map.of("location", locationParameters));
94+
}
3495
}

0 commit comments

Comments
 (0)