Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch on wifi in uiautomator test case in android device?

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

like image 569
user1907534 Avatar asked Dec 21 '12 14:12

user1907534


People also ask

How do you set up a Uiautomator?

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.

How do you use the Uiautomator in Appium?

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.

What is UiSelector?

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.


4 Answers

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.

like image 70
JulianHarty Avatar answered Oct 12 '22 03:10

JulianHarty


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.

like image 41
avz Avatar answered Oct 12 '22 01:10

avz


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.

like image 23
Kikiwa Avatar answered Oct 12 '22 02:10

Kikiwa


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\"")
like image 42
Eric Rommel Avatar answered Oct 12 '22 02:10

Eric Rommel