目的:1.Android手机上、下、左、右滑动操作
2.判断滑动到底部。思路:多次滑动后,比较最后一个元素是否相同。如果相同,则判定滑动到底部。
环境:1.java包:java-client-3.1.0.jar/ java-client-3.1.0-sources.jar
2.使用appium(环境请自行搭建)
写在文章开头,感谢陌神的帮助,也是在他的代码基础上做的。
由于涉及公司自己的APP,所以这里类.函数中类名统统改为:ClassName。需自行修改。控件的Id统统改为resourceId。需自行修改。
// 获取应用占屏幕大小
public static int[] appScreen() {
int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();
int[] appSize = { width, height };
return appSize;
}
// 向左滑动
public static void swipeToLeft(int duration) {
int startx = ClassName.appScreen()[0] * 4 / 5;
int endx = ClassName.appScreen()[0] * 1 / 5;
int y = ClassName.appScreen()[1] * 1 / 2;
try {
driver.swipe(startx, y, endx, y, duration);
} catch (Exception e) {
e.printStackTrace();
}
}
// 向右滑动
public static void swipeToRight(int duration) {
int startx = ClassName.appScreen()[0] * 1 / 5;
int endx = ClassName.appScreen()[0] * 4 / 5;
int y = ClassName.appScreen()[1] * 1 / 2;
try {
driver.swipe(startx, y, endx, y, duration);
} catch (Exception e) {
e.printStackTrace();
}
}
// 向上滑动
public static void swipeToUp(int duration) {
int starty = ClassName.appScreen()[1] * 4 / 5;
int endy = ClassName.appScreen()[1] * 1 / 5;
int x = ClassName.appScreen()[0] * 1 / 2;
try {
driver.swipe(x, starty, x, endy, duration);
} catch (Exception e) {
e.printStackTrace();
}
}
// 向下滑动
public static void swipeToDown(int duration) {
int starty = ClassName.appScreen()[1] * 1 / 5;
int endy = ClassName.appScreen()[1] * 4 / 5;
int x = ClassName.appScreen()[0] * 1 / 2;
try {
driver.swipe(x, starty, x, endy, duration);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取所有信息,判断是否已经滑动到底部
public static void getInfo() throws Exception {
// 第一次滑动前,获取最后一个元素
List<MobileElement> infolists1 = driver.findElementsById("resourceId");
String originalinfo = infolists1.get(infolists1.size() - 1).getAttribute("text");
System.out.println(originalinfo);
Thread.sleep(1000);
boolean isSwipe = true;
String currentinfo;
// 滑动
while (isSwipe) {
swipeToUp(1000);
List<MobileElement> infolists2 = driver.findElementsById("resourceId");
currentinfo= infolists2.get(infolists2.size() - 1).getAttribute("text");
if (!currentinfo.equals(originalinfo))
originalinfo= currentinfo;
else {
isSwipe = false;
System.out.println(currentinfo);
System.out.println("This is the buttom");
}
}
}