两个关于wifi的测试用例 Uiautomator2+Android Studio
测试用例1
1 enter the WIFI setting
2 open the WIFI switch
3 find a specific and enter the password
4 verify the results of the WIFI connection
5 Turn off WIFI in WIFI setting interface and verify the results of network connection (20s timeout)
6 Turn on WIFI in WIFI setting interface and verify the results of network connection (20s timeout)
7 click the SSID which connected, select forget and verify the results
8 Close the WIFI and exit the setting interface.
1进入WIFI设置
2打开WIFI开关
3找到特定的SSID并输入密码
4验证WIFI连接的结果
5在WIFI设置界面中关闭WIFI并验证网络连接结果(20s超时)
6在WIFI设置界面打开WIFI并验证网络连接结果(20s超时)
7单击连接的SSID,选择忘记并验证结果
8关闭WIFI并退出设置界面。
//main
public void test_wifi1() throws IOException, UiObjectNotFoundException {
String number = "wifimima";
for (int i = 0; i < 10; i++){
try {
//开启WiFi
reliaCom.OpenOrCloseWifi(true);
//打开WiFi输入密码
reliaCom.findwifi(number);
//判断WiFi是否连接成功
boolean a = device.wait(Until.gone(By.textContains("OFF").res(StringUtils.COM_AND_SETTIINGS_ID_SW)), 20000);
boolean b = device.wait(Until.gone(By.textContains(StringUtils.NOT_CONNE).res(StringUtils.ANDROID_SUMMARY_ID)), 20000);
Assert.assertTrue("WIFI连接失败", a || b);
//验证wifi
device.pressBack();
device.findObject(By.res(StringUtils.WIFISTEING)).click();//关闭wifi
org.junit.Assert.assertTrue("关闭wifi失败", device.wait(Until.hasObject(By.textContains("Off")), 20000));
solo.sleep(5000);
device.findObject(By.res(StringUtils.WIFISTEING)).click();//开启WiFi
org.junit.Assert.assertTrue("打开wifi失败", device.wait(Until.hasObject(By.textContains("wifimingzi")), 20000));
solo.sleep(5000);
//忘记wifi
reliaCom.removewifi();
} finally {
reliaCom.OpenOrCloseWifi(false);
}
device.pressHome();
}
}
下面是两个比较重要的方法
public void OpenOrCloseWifi(boolean flag) throws IOException, UiObjectNotFoundException {
try {
//新增开启关闭wifi
if (flag == true) {
device.executeShellCommand("svc wifi enable");
WifiManager wifiManager = (WifiManager) targetContext.getSystemService(Context.WIFI_SERVICE);
boolean wifiFlag = wifiManager.isWifiEnabled();
if (wifiFlag == true) {
return;
}
} else if (flag == false) {
device.executeShellCommand("svc wifi disable");
WifiManager wifiManager = (WifiManager) targetContext.getSystemService(Context.WIFI_SERVICE);
boolean wifiFlag = wifiManager.isWifiEnabled();
if (wifiFlag == false) {
return;
}
}
solo.sleep(1000);
soloUtils.launchByIntent(StringUtils.SETTINGS_PKG);
solo.sleep(1500);
if (device.hasObject(By.text(Pattern.compile((StringUtils.NETWORK_INT))))) {
device.findObject(By.text(Pattern.compile((StringUtils.NETWORK_INT)))).click();
solo.sleep(3000);
closeHelp();
} else {
device.pressBack();
solo.sleep(2000);
launchSettingsAndSelect(StringUtils.NET_INT);
}
} catch (Exception E) {
launchSettingsAndSelect(StringUtils.NET_INT);
}
solo.sleep(1000);
UiObject2 switchWidget = device.findObject(By.textContains("Wi‑Fi")).getParent().getParent().getParent().getChildren().get(2).getChildren().get(0);
if (flag && (!switchWidget.isChecked())) {
switchWidget.click();
}
if ((!flag) && (switchWidget.isChecked())) {
switchWidget.click();
}
}
public void findwifi(String number) {
if(device.hasObject(By.res("com.android.settings:id/icon_frame"))){
device.findObject(By.res("com.android.settings:id/icon_frame")).click();
}
solo.sleep(5000);
org.junit.Assert.assertTrue("进入wifi列表失败", device.wait(Until.hasObject(By.res("com.android.settings:id/content_frame")), 2000));
solo.sleep(1000);
//列表查找wifi名称1
/* UiObject2 listView = device.findObject(By.res("com.android.settings:id/recycler_view"));
List<String> list = new ArrayList<String>();
List<UiObject2> c = listView.getChildren();
for (int i = 0; i < listView.getChildren().size(); i++) {
String dateNumberList = listView.getChildren().get(i).getChildren().get(0).getChildren().get(1).getChildren().get(0).getText();
if(dateNumberList.equals("wifimingzi")){
device.findObject(By.text("wifimingzi")).click();
break;
}
}*/
//列表查找wifi名称2
if(!device.hasObject(By.text("wifimingzi"))){
device.swipe(350,1370,350,430,1000);
device.findObject(By.text("wifimingzi")).click();
}else {
device.findObject(By.text("wifimingzi")).click();
}
solo.sleep(1000);
UiObject2 obj = device.findObject(By.res(Pattern.compile(StringUtils.wifi_test)));
obj.setText(number);
solo.sleep(5000);
device.findObject(By.text("CONNECT")).click();
}
其中当时写的时候wifi由于信号比较弱,定位找不到,写了个很简便的下滑勉强过了,后续下条用例对下滑进行了个优化。