appium 处理滑动的方法

appium 处理滑动的方法是 swipe(int start-x, int start-y, int end-x, int end-y, int during) - Method in class io.appium.java_client.AppiumDriver此方法共有5个参数,都是整形,依次是起始位置的x y坐标和终点位子的x y坐标和滑动间隔时间,单位毫秒坐标是指:屏幕左上角为坐标系原点(0,0),屏幕分辨率为终点坐标,比如你手机分辨率10801920,那么该手机坐标系的终点是(10801920),每个元素都在坐标系中,可以用坐标系定位元素。

比如这个登陆按钮在坐标系中的位置是起始点 + 终点(32,1040) (351,1152)

向上滑动向上滑动,就是从下到上,那么怎么进行从下到上的滑动呢?按照之前理解的坐标系,从上到下滑动,一般是垂直滑动,也就是X轴不变,Y轴从大变小的过程我们可以采取Y轴固定在屏幕正当中,Y轴滑动屏幕的1/2,Y轴的起始位置为屏幕的3/4,滑动终点为Y轴的1/4(尽量避免使用坐标系的起点和终点),滑动时间为1s,如果滑动时间过短,将会出现一次滑动多页。需要提醒的是,很多app运行的时候并不是全屏,还有系统自带工具栏和边框,一定要避免!!!

package com.java.driver;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
//
public class CrazySwipe {
private AndroidDriver driver;
//
int x;
int y;
public CrazySwipe(AndroidDriver driver){
this.driver=driver;
this.x=driver.manage().window().getSize().getWidth();
this.y=driver.manage().window().getSize().getHeight();
}
public void swipeToUp(int duration){
driver.swipe(x/2, 7y/8, x/2, y/8, duration);
}
public void swipeToDown(int duration){
driver.swipe(x/2,y/8, x/2, 7
y/8, duration);
}
public void swipeToLeft(int duration){
driver.swipe(7x/8,y/2, x/8, y/2, duration);
}
public void swipeToRight(int duration){
driver.swipe(x/8,y/2, 7
x/8, y/2, duration);
}
public void swipe(String direction,int duration){
switch (direction.toLowerCase()) {
case "up":
this.swipeToUp(duration);
break;
case "down":
this.swipeToDown(duration);
break;
case "left":
this.swipeToLeft(duration);
break;
case "right":
this.swipeToRight(duration);
break;
default:
System.out.println("方向参数不正确,需传入up/down/left/right");
break;
}

}

}

原文地址:https://www.cnblogs.com/ITniu/p/6402906.html