6

uiautomator我想使用android 中的工具打开 wifi 作为测试用例的一部分。我尝试在uiautomator测试用例中使用以下代码:

WifiManager wi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      if(wi.isWifiEnabled()){
        wi.setWifiEnabled(false);
      }else{
        wi.setWifiEnabled(true);
    }

但它给出了这个错误:

Mainclass 未定义“getSystemservice”方法

4

6 回答 6

9

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.

于 2013-03-08T22:17:44.717 回答
3

启用 WiFi:
device.executeShellCommand("svc wifi enable");

禁用 WiFi:
device.executeShellCommand("svc wifi disable");

这些是在您的 UiDevice 上使用的命令。

于 2020-05-23T11:51:24.190 回答
2

在 Android 4.2 和 4.4 上用于生产

要在您的代码中打开 Android Wifi 设置:

final Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
mContext.startActivity(intent);

要使用 UiAutomator 单击开/关开关(在您确定您的活动良好之后):

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();
        }
    }
}

已知限制:它正在搜索第一个可用的 Switch。如果您有自定义 ROM,或者如果 Android 设置应用程序在未来发展,这可能还不够。

于 2016-05-12T13:31:06.880 回答
1

在我使用 UIAutomator 进行的套件测试中,我使用:

Runtime.getRuntime().exec("\"svc wifi disable\"")

Runtime.getRuntime().exec("\"svc wifi enable\"")

Runtime.getRuntime().exec("\"svc data disable\"")

Runtime.getRuntime().exec("\"svc data enable\"")
于 2014-10-15T21:34:03.877 回答
0

您可以通过 adb 启用或禁用 wifi,如下所示

adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db update secure set value=1 where name='wifi_on'; 。出口

但是你不能使用 uiautomator 工具来做同样的事情

于 2013-01-14T16:26:00.657 回答
-5

你不能这样做。UI Automator 测试不作为 Android 框架的一部分运行,因此它无法访问 Android 系统服务。它旨在测试用户界面;它并不声称是一个功能齐全的测试框架。在运行测试之前手动打开 WiFi。

于 2012-12-21T18:55:24.990 回答