截图功能:
//截屏并保存至本地
File screen = driver.getScreenshotAs(OutputType.FILE);
File screenFile = new File("d:\\screen.png");
FileUtils.copyFile(screen, screenFile); //commons-io-2.0.1.jar中的api
push文件、pull文件
File file = new File("d:\\test.txt"); //test.txt内容为"test"
String content = null;
try {
content = FileUtils.readFileToString(file);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = Base64.encodeBase64(content.getBytes());
driver.pushFile("sdcard/test.txt", data);
byte[] resultDate = driver.pullFile("sdcard/test.txt");
System.out.println(new String(Base64.decodeBase64(resultDate))); //打印结果为"test"
发送按键事件
driver.sendKeyEvent(AndroidKeyCode.HOME);
tap点击元素
driver.tap(1, driver.findElementByName("日期和时间"), 0);
单击坐标
如果怎么都定位不到控件,那就可以考虑坐标定位
driver.swipe(1257, 2263, 1257, 2263, 5);
driver. tap(int fingers, int x, int y, int duration) ;
//tap点击元素位置
driver.tap(1, driver.findElementByName("日期和时间"), 0);
//获取当前界面的activity,可用于断言是否跳转到预期的activity
driver.currentActivity();
//打开通知栏界面
driver.openNotifications();
//获取网络状态
int status = driver.getNetworkConnection().value;
System.out.println(status);
//设置网络状态
driver.setNetworkConnection(new NetworkConnectionSetting(status));
//或者
driver.setNetworkConnection(new NetworkConnectionSetting(false, true, false));
//启动其他应用,跨APP
driver.startActivity("com.android.camera", ".CameraLauncher");
//安装APP
driver.installApp(appPath);
//判断应用是否已安装
driver.isAppInstalled("package name");
//拖动相机图标至日历图标位置
new TouchAction(driver).longPress(driver.findElementByName("相机"))
.moveTo(driver.findElementByName("日历")).release().perform();
new TouchAction(driver).longPress(driver.findElementByName("我的账户"))
.moveTo(540, 68).release().perform();
//锁屏
driver.lockScreen(2);
//判断是否锁屏
driver.isLocked();
//相同属性的元素使用List存放
List<WebElement> elements = driver.findElementsByClassName("class name");
elements.get(0).click(); //点击List中的第一个元素
// 绘制手势密码
new TouchAction(driver).press(x,y).waitAction(100).moveTo(x1, y1).waitAction(100).release().perform();
x1,y1 相对x,y的坐标
或者
.moveTo(X, Y + height)
.moveTo(X, Y + height + height)
.moveTo(X + width, Y + height + height).release();
gesture.perform();