以打开飞行模式的操作为例
一. 父类page页
package com.qwert.autotest.simcard.page;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiSelector;
public class Page {
UiDevice mDevice;
public Page(UiDevice device) {
mDevice = device;
}
public UiObject find(UiSelector selector) {
return new UiObject(selector);
}
// UI2.0
/*public UiObject2 find(BySelector by) {
return mDevice.findObject(by);
}*/
// UI2.0
/*public UiObject find(UiSelector selector) {
return mDevice.findObject(selector);
}*/
}
二. 快速设置页面
package com.qwert.autotest.simcard.page;
import android.widget.ImageView;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
public class QuickSettingPage extends Page {
private UiSelector airPlaneSelector = new UiSelector().description("Airplane mode");
private UiSelector airPlaneIconSelector = new UiSelector().description("Airplane mode.").className(ImageView.class);
public QuickSettingPage(UiDevice device) {
super(device);
}
public static QuickSettingPage openQuickSettings(UiDevice device) {
device.openQuickSettings();
device.waitForIdle();
return new QuickSettingPage(device);
}
private boolean isAirplaneModeEnabled() {
try {
return "On".equals(find(airPlaneSelector).getText());
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
return false;
}
}
public boolean setAirplaneMode(boolean enabled) {
if (enabled == isAirplaneModeEnabled()) {
return true;
}
try {
find(airPlaneSelector).click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
return enabled == isAirplaneModeEnabled();
}
public boolean isAirPlaneMode() {
return find(airPlaneIconSelector).exists();
}
}
三. 测试用例
package com.qwert.autotest.simcard.testcase;
import android.os.SystemClock;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.transsion.autotest.simcard.common.AutomatorHelper;
import com.transsion.autotest.simcard.page.*;
public class SimCardTest extends UiAutomatorTestCase {
private UiDevice mDevice;
private AutomatorHelper mHelper;
@Override
protected void setUp() throws Exception {
super.setUp();
mDevice = UiDevice.getInstance();
mHelper = new AutomatorHelper(mDevice);
System.out.println("RUN START!!");
mHelper.openLauncher();// 唤醒屏幕,解锁,打开桌面
}
/**
* 飞行模式切换到普通模式
*/
public void testSwitchToNormalMode() {
// 打开快速设置,打开飞行模式
QuickSettingPage settingPage = QuickSettingPage.openQuickSettings(mDevice);
settingPage.setAirplaneMode(true);
// 由飞行模式切换到普通模式
settingPage.setAirplaneMode(false);
// 验证是否切换成功
if (!settingPage.isAirPlaneMode()) {
System.out.println("RESULT:Pass");
System.out.println("switch to normal mode success");
} else {
System.out.println("RESULT:Fail switch to normal mode fail");
}
}
/**
* 普通模式切换到飞行模式
*/
public void testSwitchToAirplaneMode() {
// 打开快速设置,打开飞行模式,验证是否打开成功
QuickSettingPage settingPage = QuickSettingPage.openQuickSettings(mDevice);
settingPage.setAirplaneMode(true);
if (settingPage.isAirPlaneMode()) {
System.out.println("RESULT:Pass");
System.out.println("switch to airplane mode success");
} else {
System.out.println("RESULT:Fail switch to airplane mode fail");
}
// 测试完成,取消飞行模式。恢复手机状态,避免影响后续用例执行。
settingPage.setAirplaneMode(false);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
System.out.println("RUN FINISH!!");
getUiDevice().pressBack();
getUiDevice().pressHome();
}
}