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 1 commit
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_WIFI, postC("/session/:sessionId/appium/device/toggle_airplane_mode"));
commandRepository.put(TOGGLE_WIFI, postC("/session/:sessionId/appium/device/toggle_data"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOGGLE_AIRPLANE_MODE

}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;
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.toggleLocationServicesCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleWifiCommand;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.CommandExecutionHelper;
Expand Down Expand Up @@ -176,4 +179,16 @@ public void openNotifications() {
public void toggleLocationServices() {
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
}

public void toggleWifi() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be useful to have these methods in the separate SupportsNetworkStateManagement interface

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay i will do that :) cheers

CommandExecutionHelper.execute(this, toggleWifiCommand());
}

public void toggleAirplaneMode() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstrings are missing. BTW, this method works for Android only. Also, it foes not work since Android 7+

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In such case, what to do ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply the interface to Android driver only. Also, describe the limitations in the docstring

Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move it inside SupportsSpecialEmulatorCommands

not needed. As you said Appium will anyway throw an exception if the command didn't work. Although, it might be useful to describe what is supported in the docstring

CommandExecutionHelper.execute(this, toggleAirplaneCommand());
}

public void toggleData() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works on Android. Also, the device should be rooted

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the server thrown exception in this case ?

CommandExecutionHelper.execute(this, toggleDataCommand());
}
}
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
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