appium滑动及滑到底部判断

目的: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");
            }
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值