|
16 | 16 |
|
17 | 17 | package io.appium.java_client.remote;
|
18 | 18 |
|
| 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; |
19 | 23 | import org.openqa.selenium.WebDriver;
|
20 |
| -import org.openqa.selenium.html5.Location; |
21 | 24 | import org.openqa.selenium.html5.LocationContext;
|
22 | 25 | import org.openqa.selenium.remote.html5.RemoteLocationContext;
|
23 | 26 |
|
24 |
| -public interface SupportsLocation extends WebDriver, LocationContext { |
25 |
| - public RemoteLocationContext getLocationContext(); |
| 27 | +import java.util.Map; |
26 | 28 |
|
| 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 | + */ |
27 | 39 | 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 | + } |
29 | 59 | }
|
30 | 60 |
|
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)); |
33 | 68 | }
|
34 | 69 | }
|
0 commit comments