appium滑动操作
2015-10-15 16:40
295 查看
目的: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");
}
}
}
代码小白尝试写的,还有很多地方不完善,所以,大神看官请忽略,小白看官请参考~~~
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");
}
}
}
代码小白尝试写的,还有很多地方不完善,所以,大神看官请忽略,小白看官请参考~~~
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统