模拟器创建
- 查看模拟器列表: emulator -list-avds
- 在命令行启动模拟器:
emulator @foo
oremulator -avd foo
desirecapbility配置
- caps.setCapability(AVD,“Pixel_Android6.0”);
- 注意自动启动模拟器,只能是sdk的模拟器,第三方模拟器不支持,7.0不支持
模拟电话、短信
//拨打电话(模拟器)
driver.makeGsmCall(“18390522307”, GsmCallActions.CALL);
//接听电话(模拟器)
driver.makeGsmCall(“18390522307”, GsmCallActions.ACCEPT);
// 拒接电话/挂断电话()
driver.makeGsmCall(“18390522307”, GsmCallActions.CANCEL);
//发送短信(模拟器)
driver.sendSMS(“18390522307”,“hello appium message”);
网络模式
driver.toggleAirplaneMode();//飞行模式
driver.toggleWifi();//wifi模式
driver.toggleData();//数据流量模式
横竖屏切换
//横屏切换
driver.rotate(ScreenOrientation.LANDSCAPE);
//切换成竖屏
driver.rotate(ScreenOrientation.PORTRAIT);
App处理
//锁屏
driver.lockDevice();
//截图
File screenshotAs = driver.getScreenshotAs(OutputType.FILE);
File file = new File(System.getProperty(“user.dir”) + “/src/main/resources/demo.png”);
FileUtils.copyFile(screenshotAs,file);
//设置地理位置(仅限于模拟器)
Location location = new Location(1,2,3);
driver.setLocation(location);
录屏
//录屏
driver.startRecordingScreen();
driver.stopRecordingScreen();
硬件操作
-//硬件操作
driver.pressKey(new KeyEvent().withKey(AndroidKey.BACK));
- 比如 back,home,menu,power,voice up,voice down,
- keycode查看地址:
https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_MENU
官网地址
qemu-system
@Test
public void deviceTest() throws IOException {
//拨打电话(模拟器)
driver.makeGsmCall("18390522307", GsmCallActions.CALL);
//接听电话(模拟器)
driver.makeGsmCall("18390522307", GsmCallActions.ACCEPT);
// 拒接电话/挂断电话()
driver.makeGsmCall("18390522307", GsmCallActions.CANCEL);
//发送短信(模拟器)
driver.sendSMS("18390522307","hello appium message");
//网络设置
driver.toggleAirplaneMode();//飞行模式
driver.toggleWifi();//wifi模式
driver.toggleData();//数据流量模式
//横屏切换
driver.rotate(ScreenOrientation.LANDSCAPE);
//切换成竖屏
driver.rotate(ScreenOrientation.PORTRAIT);
//锁屏
driver.lockDevice();
//截图
File screenshotAs = driver.getScreenshotAs(OutputType.FILE);
File file = new File(System.getProperty("user.dir") + "/src/main/resources/demo.png");
FileUtils.copyFile(screenshotAs,file);
//录屏
driver.startRecordingScreen();
driver.stopRecordingScreen();
//设置地理位置(仅限于模拟器)
Location location = new Location(1,2,3);
driver.setLocation(location);
//硬件操作
driver.pressKey(new KeyEvent().withKey(AndroidKey.BACK));
}