实际上,你可以使用UIAutomator设置无线网络设置和关闭。我今天晚上写了代码:)
这是代码。你可以把它添加到Android例子是在这里http://developer.android.com/tools/testing/testing_ui.html
在类
private enum OnOff {
Off,
On
};
添加新代码的顶部添加以下枚举后:
// 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());
这是新的代码:
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;
}
该循环是为了补偿我观察到的一些行为,其中点击似乎没有改变开关位置。
另外我想知道,如果我们可以在uiautomator中运行测试用例时检查wifi连接是否处于活动状态,即使我手动开启wifi也可以检查数据包是否交换。那可能吗? – user1907534
既然我们可以在这个中使用android的所有assert函数,为什么不用这个函数呢? – user1907534