Appium appium 的 java-client 新版本的滑动操作代码 (TouchAction 取代 swipe)

泽雨 · 2019年01月26日 · 901 次阅读

java-client-4.0.0 里面用的是 swipe 方法,新版本的 javaclient 已经废弃了 swipe 方法,下面是用的 TouchAction

appium 的 java-client-6.1.0 新版本的滑动 一部分代码

import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

public class Commom {

public static Duration duration=Duration.ofMillis(300);//滑动 300ms
//下面两个参数,我是放到 testng 里面的 @beforetest里面做了初始化的,这里没有把代码放出来
public static int width;// 手机屏幕宽
public static int height;// 手机屏幕长

// 滑动操作
/**
* This Method for swipe up
* 上滑
* @author X
* @throws InterruptedException
*/
public static void swipeToUp() {
try {
new TouchAction(Init.getDriver()).press(PointOption.point(width / 2, height * 3 / 4)).
waitAction(WaitOptions.waitOptions(duration)).
moveTo(PointOption.point(width / 2, height / 4)).release().perform();
//滑动以后最好是等待一点时间,方便元素加载之类的
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* This Method for swipe down
* 下滑
* @author X
* @throws InterruptedException
*/
public static void swipeToDown() {
try {
new TouchAction(Init.getDriver()).press(PointOption.point(width / 2, height / 4)).
waitAction(WaitOptions.waitOptions(duration)).
moveTo(PointOption.point(width / 2, height * 3 / 4)).release().perform();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 左滑
*
*/
public static void swipeToLeft() {
try {

new TouchAction(Init.getDriver()).press(PointOption.point(width * 3/ 4, height / 2))
.waitAction(WaitOptions.waitOptions(duration)).
moveTo(PointOption.point(width / 4, height / 2)).release().perform();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* This Method for swipe Right
* 右滑
* @author X
*/
public static void swipeToRight() {
try {
new TouchAction(Init.getDriver()).press(PointOption.point(width / 4, height / 2)).
waitAction(WaitOptions.waitOptions(duration)).
moveTo(PointOption.point(width * 3/ 4, height / 2)).release().perform();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}

}
}

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册