Skip to content

Commit ba63c06

Browse files
refactor: Finish migrating Android helpers to mobile extensions (#1901)
1 parent 95d4272 commit ba63c06

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
import java.net.URL;
5252

5353
import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand;
54-
import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;
55-
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;
5654

5755
/**
5856
* Android driver implementation.
@@ -86,6 +84,8 @@ public class AndroidDriver extends AppiumDriver implements
8684
HasBattery<AndroidBatteryInfo>,
8785
ExecuteCDPCommand,
8886
CanReplaceElementValue,
87+
SupportsGpsStateManagement,
88+
HasNotifications,
8989
SupportsExtendedGeolocationCommands {
9090
private static final String ANDROID_PLATFORM = Platform.ANDROID.name();
9191

@@ -262,17 +262,6 @@ public void endTestCoverage(String intent, String path) {
262262
CommandExecutionHelper.execute(this, endTestCoverageCommand(intent, path));
263263
}
264264

265-
/**
266-
* Open the notification shade, on Android devices.
267-
*/
268-
public void openNotifications() {
269-
CommandExecutionHelper.execute(this, openNotificationsCommand());
270-
}
271-
272-
public void toggleLocationServices() {
273-
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
274-
}
275-
276265
@Override
277266
public AndroidBatteryInfo getBatteryInfo() {
278267
return new AndroidBatteryInfo(CommandExecutionHelper.executeScript(this, "mobile: batteryInfo"));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ default void replaceElementValue(RemoteWebElement element, String value) {
2828
// TODO: Remove the fallback
2929
this.execute(MobileCommand.REPLACE_VALUE, ImmutableMap.of(
3030
"id", element.getId(),
31+
"text", value,
3132
"value", value
3233
));
3334
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.appium.java_client.android;
2+
3+
import io.appium.java_client.CommandExecutionHelper;
4+
import io.appium.java_client.ExecutesMethod;
5+
import org.openqa.selenium.UnsupportedCommandException;
6+
7+
import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;
8+
9+
public interface HasNotifications extends ExecutesMethod {
10+
11+
/**
12+
* Opens notification drawer on the device under test.
13+
*/
14+
default void openNotifications() {
15+
try {
16+
CommandExecutionHelper.executeScript(this, "mobile: openNotifications");
17+
} catch (UnsupportedCommandException e) {
18+
// TODO: Remove the fallback
19+
CommandExecutionHelper.execute(this, openNotificationsCommand());
20+
}
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.appium.java_client.android;
2+
3+
import io.appium.java_client.CommandExecutionHelper;
4+
import io.appium.java_client.ExecutesMethod;
5+
import org.openqa.selenium.UnsupportedCommandException;
6+
7+
import static com.google.common.base.Preconditions.checkNotNull;
8+
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;
9+
10+
public interface SupportsGpsStateManagement extends ExecutesMethod {
11+
12+
/**
13+
* Toggles GPS service state.
14+
* This method only works reliably since API 31 (Android 12).
15+
*/
16+
default void toggleLocationServices() {
17+
try {
18+
CommandExecutionHelper.executeScript(this, "mobile: toggleGps");
19+
} catch (UnsupportedCommandException e) {
20+
// TODO: Remove the fallback
21+
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
22+
}
23+
}
24+
25+
/**
26+
* Check GPS service state.
27+
*
28+
* @return true if GPS service is enabled.
29+
*/
30+
default boolean isLocationServicesEnabled() {
31+
return checkNotNull(
32+
CommandExecutionHelper.executeScript(this, "mobile: isGpsEnabled")
33+
);
34+
}
35+
}

0 commit comments

Comments
 (0)