Skip to content

#835 added handlers for toggle_wifi,data and airplane mode #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, CommandInfo> commandRepository;

Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class AndroidDriver<T extends WebElement>
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
HasSupportedPerformanceDataType, AuthenticatesByFinger,
CanRecordScreen, SupportsSpecialEmulatorCommands {
CanRecordScreen, SupportsSpecialEmulatorCommands,
SupportsNetworkStateManagement {

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

Expand Down Expand Up @@ -176,4 +177,5 @@ public void openNotifications() {
public void toggleLocationServices() {
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, ?>> toggleWifiCommand() {
return new AbstractMap.SimpleEntry<>(TOGGLE_WIFI, ImmutableMap.<String, Object>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<String, Map<String, ?>> toggleAirplaneCommand() {
return new AbstractMap.SimpleEntry<>(TOGGLE_AIRPLANE_MODE, ImmutableMap.<String, Object>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<String, Map<String, ?>> toggleDataCommand() {
return new AbstractMap.SimpleEntry<>(TOGGLE_DATA, ImmutableMap.<String, Object>of());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down