如何在Android设备的uiautomator测试用例中打开wifi?

如何在Android设备的uiautomator测试用例中打开wifi?,第1张

概述我想在android中使用uiautomator工具打开wifi作为测试用例的一部分.我尝试在uiautomator测试用例中使用以下代码:WifiManagerwi=(WifiManager)this.getSystemService(Context.WIFI_SERVICE);if(wi.isWifiEnabled()){wi.setWifiEnabled(false);}else{

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

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

但它给出了这个错误:

“getSystemservice” method is undefined for Mainclass

解决方法:

您实际上可以使用UIautomator来打开和关闭WiFi设置.我今天晚上写了代码:)

这是代码.您可以将其添加到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;}

循环是为了补偿我观察到的一些行为,其中点击似乎没有改变开关位置.

总结

以上是内存溢出为你收集整理的如何在Android设备的uiautomator测试用例中打开wifi?全部内容,希望文章能够帮你解决如何在Android设备的uiautomator测试用例中打开wifi?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1110075.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存