diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index 6f5f31106..997242cf6 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -98,6 +98,9 @@ public class MobileCommand { protected static final String NETWORK_SPEED; protected static final String POWER_CAPACITY; protected static final String POWER_AC_STATE; + protected static final String TOGGLE_WIFI; + protected static final String TOGGLE_AIRPLANE_MODE; + protected static final String TOGGLE_DATA; public static final Map commandRepository; @@ -163,6 +166,9 @@ public class MobileCommand { NETWORK_SPEED = "networkSpeed"; POWER_CAPACITY = "powerCapacity"; POWER_AC_STATE = "powerAC"; + TOGGLE_WIFI = "toggleWiFi"; + TOGGLE_AIRPLANE_MODE = "toggleFlightMode"; + TOGGLE_DATA = "toggleData"; commandRepository = new HashMap<>(); commandRepository.put(RESET, postC("/session/:sessionId/appium/app/reset")); @@ -238,6 +244,9 @@ public class MobileCommand { commandRepository.put(NETWORK_SPEED, postC("/session/:sessionId/appium/device/network_speed")); commandRepository.put(POWER_CAPACITY, postC("/session/:sessionId/appium/device/power_capacity")); commandRepository.put(POWER_AC_STATE, postC("/session/:sessionId/appium/device/power_ac")); + commandRepository.put(TOGGLE_WIFI, postC("/session/:sessionId/appium/device/toggle_wifi")); + commandRepository.put(TOGGLE_AIRPLANE_MODE, postC("/session/:sessionId/appium/device/toggle_airplane_mode")); + commandRepository.put(TOGGLE_DATA, postC("/session/:sessionId/appium/device/toggle_data")); } /** diff --git a/src/main/java/io/appium/java_client/android/AndroidDriver.java b/src/main/java/io/appium/java_client/android/AndroidDriver.java index d676cfb93..3af82826b 100644 --- a/src/main/java/io/appium/java_client/android/AndroidDriver.java +++ b/src/main/java/io/appium/java_client/android/AndroidDriver.java @@ -52,7 +52,8 @@ public class AndroidDriver implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity, FindsByAndroidUIAutomator, LocksDevice, HasAndroidSettings, HasDeviceDetails, HasSupportedPerformanceDataType, AuthenticatesByFinger, - CanRecordScreen, SupportsSpecialEmulatorCommands { + CanRecordScreen, SupportsSpecialEmulatorCommands, + SupportsNetworkStateManagement { private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID; @@ -176,4 +177,5 @@ public void openNotifications() { public void toggleLocationServices() { CommandExecutionHelper.execute(this, toggleLocationServicesCommand()); } + } diff --git a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java index 5066ce2a6..ba0e32ff7 100644 --- a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java +++ b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java @@ -396,4 +396,31 @@ public class AndroidMobileCommandHelper extends MobileCommand { return new AbstractMap.SimpleEntry<>(POWER_AC_STATE, prepareArguments("state", powerACState.name().toLowerCase())); } + + /** + * This method forms a {@link Map} of parameters for the toggling of wifi. + * + * @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments. + */ + public static Map.Entry> toggleWifiCommand() { + return new AbstractMap.SimpleEntry<>(TOGGLE_WIFI, ImmutableMap.of()); + } + + /** + * This method forms a {@link Map} of parameters for the toggle airplane mode. + * + * @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments. + */ + public static Map.Entry> toggleAirplaneCommand() { + return new AbstractMap.SimpleEntry<>(TOGGLE_AIRPLANE_MODE, ImmutableMap.of()); + } + + /** + * This method forms a {@link Map} of parameters for the toggle data. + * + * @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments. + */ + public static Map.Entry> toggleDataCommand() { + return new AbstractMap.SimpleEntry<>(TOGGLE_DATA, ImmutableMap.of()); + } } diff --git a/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java b/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java new file mode 100644 index 000000000..066d18af0 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/SupportsNetworkStateManagement.java @@ -0,0 +1,35 @@ +package io.appium.java_client.android; + +import io.appium.java_client.CommandExecutionHelper; +import io.appium.java_client.ExecutesMethod; + +import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleAirplaneCommand; +import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleDataCommand; +import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleWifiCommand; + +public interface SupportsNetworkStateManagement extends ExecutesMethod { + + /** + * Toggles Wifi on and off + */ + default void toggleWifi() { + CommandExecutionHelper.execute(this, toggleWifiCommand()); + } + + /** + * Toggle Airplane mode and this works + * on OS 6.0 and lesser + * and does not work on OS 7.0 and greater + */ + default void toggleAirplaneMode() { + CommandExecutionHelper.execute(this, toggleAirplaneCommand()); + } + + /** + * Toggle Mobile Data and this works on Emulator + * and rooted device + */ + default void toggleData() { + CommandExecutionHelper.execute(this, toggleDataCommand()); + } +} diff --git a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java index a1bf9c040..8d16f01a0 100644 --- a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java +++ b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java @@ -59,6 +59,30 @@ public class AndroidDriverTest extends BaseAndroidTest { } } + @Test public void toggleWiFi() { + try { + driver.toggleWifi(); + } catch (Exception e) { + fail("Not able to toggle wifi"); + } + } + + @Test public void toggleAirplane() { + try { + driver.toggleAirplaneMode(); + } catch (Exception e) { + fail("Not able to toggle airplane mode"); + } + } + + @Test public void toggleData() { + try { + driver.toggleData(); + } catch (Exception e) { + fail("Not able to toggle data"); + } + } + @Test public void gsmSignalStrengthTest() { try { driver.setGsmSignalStrength(GsmSignalStrength.GREAT); diff --git a/src/test/java/io/appium/java_client/android/BaseAndroidTest.java b/src/test/java/io/appium/java_client/android/BaseAndroidTest.java index 1510e4e89..c82886807 100644 --- a/src/test/java/io/appium/java_client/android/BaseAndroidTest.java +++ b/src/test/java/io/appium/java_client/android/BaseAndroidTest.java @@ -16,6 +16,7 @@ package io.appium.java_client.android; +import io.appium.java_client.remote.AutomationName; import io.appium.java_client.remote.MobileCapabilityType; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException; @@ -47,6 +48,7 @@ public class BaseAndroidTest { File appDir = new File("src/test/java/io/appium/java_client"); File app = new File(appDir, "ApiDemos-debug.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2); capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); driver = new AndroidDriver<>(service.getUrl(), capabilities);