I want to switch on wifi as a part of test case using uiautomator
tool in android. I tried using following code in uiautomator
test case:
WifiManager wi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(wi.isWifiEnabled()){
wi.setWifiEnabled(false);
}else{
wi.setWifiEnabled(true);
}
but it gave this error:
"getSystemservice" method is undefined for Mainclass
uiautomator. Click Add Library > JUnit then select JUnit3 to add JUnit support. Click Add External JARs... and navigate to the SDK directory. Under the platforms directory, select the latest SDK version and add both the uiautomator.
The way to start a session using the UiAutomator driver is to include the platformName capability in your new session request, with the value Android . Of course, you must also include appropriate platformVersion , deviceName , and app capabilities, at a minimum.
description. UiSelector description (String desc) Set the search criteria to match the content-description property for a widget. The content-description is typically used by the Android Accessibility framework to provide an audio prompt for the widget when the widget is selected.
You can actually use UIAutomator to set the WiFi setting on and off. I wrote the code this evening :)
Here's the code. You can add it to the Android example which is here http://developer.android.com/tools/testing/testing_ui.html
Add the following enum at the top of the class
private enum OnOff {
Off,
On
};
Add the new code after:
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(new UiSelector()
.packageName("com.android.settings"));
assertTrue("Unable to detect Settings", settingsValidation.exists());
Here is the new code:
UiSelector settingsItems = new UiSelector().className(android.widget.TextView.class.getName());
UiObject wiFi = appViews.getChildByText(settingsItems, "Wi-Fi");
// We can click on Wi-Fi, e.g. wiFi.clickAndWaitForNewWindow();
// So we know we have found the Wi-Fi setting
UiSelector switchElement = new UiSelector().className(android.widget.Switch.class.getName());
setSwitchTo(OnOff.Off); // Or set it to On as you wish :)
}
private void setSwitchTo(OnOff value) throws UiObjectNotFoundException {
String text;
UiObject switchObject = getSwitchObject();
for (int attempts = 0; attempts < 5; attempts++) {
text = switchObject.getText();
boolean switchIsOn = switchObject.isChecked();
final OnOff result;
if (switchIsOn) {
result = OnOff.On;
} else {
result = OnOff.Off;
}
System.out.println("Value of switch is " + switchObject.isSelected() + ", " + text + ", " + switchIsOn);
if (result == value) {
System.out.println("Switch set to correct value " + result);
break;
} else {
switchObject.click();
}
}
}
private UiObject getSwitchObject() {
UiObject switchObject = new UiObject(new UiSelector().className(android.widget.Switch.class.getName()));
assertTrue("Unable to find the switch object", switchObject.exists());
String text;
return switchObject;
}
The loop was to compensate for some behaviour I observed, where the click didn't seem to change the switch position.
To enable WiFi: device.executeShellCommand("svc wifi enable");
To disable WiFi: device.executeShellCommand("svc wifi disable");
These are the commands to use on your UiDevice.
Used on production on Android 4.2 and 4.4
To open the Android Wifi settings in your code:
final Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
mContext.startActivity(intent);
To click the on/off switch with UiAutomator (after you're sure you're on the good activity):
public void enableWifiOnAndroidWifiSettings(boolean enabled) throws UiObjectNotFoundException {
final UiSelector wifiSwitchSelector = new UiSelector().className(android.widget.Switch.class.getName());
UiObject wifiSwitch = UiDevice.getInstance(sInstrumentation).findObject(wifiSwitchSelector);
if (wifiSwitch.waitForExists(5000) && wifiSwitch.isEnabled()) {
if (wifiSwitch.isChecked() != enabled) {
wifiSwitch.click();
}
}
}
Known limitation: It's searching the first Switch available. If you've custom ROM or if the Android Settings app evolves in the future, it will maybe not be enough.
In my suite tests with UIAutomator I use:
Runtime.getRuntime().exec("\"svc wifi disable\"")
Runtime.getRuntime().exec("\"svc wifi enable\"")
Runtime.getRuntime().exec("\"svc data disable\"")
Runtime.getRuntime().exec("\"svc data enable\"")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With