一、手势操作
1.上下左右滑屏
swipe---滑动
java-client 4.x 是有swipe方法的,可以通过传递坐标信息就可以完成滑动androidDriver.swipe(startx, starty, endx, endy, duration);
<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>4.1.0</version> </dependency>
//向下滑动,实现下拉刷新
public static void testSwipe() {
/**
* Parameters:
* startx starting x coordinate.
* starty starting y coordinate.
* endx ending x coordinate.
* endy ending y coordinate.
* duration amount of time in milliseconds for the entire swipe action to take
*/
// androidDriver.swipe(startx, starty, endx, endy, duration);
androidDriver.swipe(400, 500, 400, 1000, 5000);
}
java-i5.0+ swipe方法已经失效,但是可以采取自定义的swipe方法实现,swipeLeft、swipeRight、swipeUp参考swipeDown实现;
public static void swipeDown(){ //1.实例化TouchAction对象,触摸操作相关 TouchAction touchAction = new TouchAction(androidDriver); //2.可以根据当前屏幕的宽度和高度来自定义起始点和终止点的坐标来实现滑动的通用操作 Dimension dimension = androidDriver.manage().window().getSize(); int X = dimension.getWidth(); int Y = dimension.getHeight(); int startx = X/2; int starty = Y/4; // startPoint 起始点 PointOption startPoint = PointOption.point(startx, starty); int endx = X/2; int endy = Y*3/4; // endPoint 终止点 PointOption endPoint = PointOption.point(endx, endy); //把原始的滑动时间转换成duration Duration duration = Duration.ofMillis(5000); //然后再把duration转化成 WaitOptions WaitOptions waitOptions = WaitOptions.waitOptions(duration); //appium将按-等待-移动-释放转换为滑动操作 touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release(); touchAction.perform(); }
2.放大缩小(zoom---放大,pinch---缩小)
java-client4.X 也是有zoom和pinch方法的可以通过传递坐标的信息就可以实现放大
androidDriver.zoom(x, y);
//androidDriver.zoom(WebElement element)
zoom( x, y )方法其实是由两个MultiTouchAction实现的多点触摸,首先根据你传入的坐标确定偏移量,然后创建两个action分别向反方向移动,同时释放,pinch同zoom相反
java-client5.0+ zoom和pinch方法已经失效,过期,但是可以采用java-client4.X老版本的实现思想
/** * 自定义放大效果的方法 */ public static void zoomOut() { //1.实例化多点触摸对象 MultiTouchAction multiTouch = new MultiTouchAction(androidDriver); //2.得到当前屏幕的高度 int scrHeight = androidDriver.manage().window().getSize().getHeight(); //3.获取当前屏幕的宽高来确定屏幕中心点(x,y) int x = androidDriver.manage().window().getSize().getWidth()/2; int y = androidDriver.manage().window().getSize().getHeight()/2; System.out.println(x+":"+y); int yOffset = 100; if (y - 100 < 0) { yOffset = y; } else if (y + 100 > scrHeight) { yOffset = scrHeight - y; } //两个手指同时向相反方向滑动实现放大的效果 //第一个手指实现滑动 TouchAction action0 = new TouchAction(androidDriver).press(PointOption.point(x, y)).moveTo(PointOption.point(x, y-yOffset)).release(); //第二个手指实现滑动 TouchAction action1 = new TouchAction(androidDriver).press(PointOption.point(x, y)).moveTo(PointOption.point(x, y+yOffset)).release(); multiTouch.add(action0).add(action1); multiTouch.perform(); }
3.多点触摸(tap---点点)
未完待续...