appium TouchAction实现上下左右和自定义滑动

时间:2021-10-12 18:45:08

  • 代码中的TestStep是封装的bean,自己可以根据需求重写
  • /**
         * @description: TODO 上滑
         * @param
         * @return 
         * @throws
         * @author guguangyu
         * @date 2019/11/12 9:21 
         */
        public void slideUp(TestStep step){
            System.out.println("『正常测试』开始执行: "   "<"  step.getDesc()   ">");
            //获取屏幕的高度
            int height = step.getAndroidDriver().manage().window().getSize().height;
            //获取屏幕的宽带
            int width = step.getAndroidDriver().manage().window().getSize().width;
            //设定上滑的起点
            PointOption pointOption = PointOption.point(width/2,height*3/4);
            //Action
            TouchAction touchAction = new TouchAction(step.getAndroidDriver());
            //上滑
            // ·release 意思相当于放开
            //·perform 相当于发送执行
            //waitAction 等待
            touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(width/2,height/4)).release().perform();
        }
    
        /**
         * @description: TODO 下滑
         * @param  
         * @return 
         * @throws
         * @author guguangyu
         * @date 2019/11/12 11:30 
         */
        public void slideDown(TestStep step){
            System.out.println("『正常测试』开始执行: "   "<"  step.getDesc()   ">");
            //获取屏幕高度
            int height = step.getAndroidDriver().manage().window().getSize().height;
            //获取页面的宽度
            int width = step.getAndroidDriver().manage().window().getSize().width;
            //设定下滑起点
            PointOption pointOption = PointOption.point(width/2,height/4);
            //Action
            TouchAction touchAction = new TouchAction(step.getAndroidDriver());
            //下滑
            // ·release 意思相当于放开
            //·perform 相当于发送执行
            //waitAction 等待
            touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(width/2,height*3/4)).release().perform();
    
        }
    
        /**
         * @description: TODO 左滑
         * @param
         * @return 
         * @throws
         * @author guguangyu
         * @date 2019/11/12 11:30 
         */
        public void slideLeft(TestStep step){
            System.out.println("『正常测试』开始执行: "   "<"  step.getDesc()   ">");
            //获取屏幕高度
            int height = step.getAndroidDriver().manage().window().getSize().height;
            //获取页面的宽度
            int width = step.getAndroidDriver().manage().window().getSize().width;
            //设定左滑起点
            PointOption pointOption = PointOption.point(width*3/4,height/2);
            //Action
            TouchAction touchAction = new TouchAction(step.getAndroidDriver());
            //左滑
            // ·release 意思相当于放开
            //·perform 相当于发送执行
            //waitAction 等待
            touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(width/4,height/2)).release().perform();
        }
    
        /**
         * @description: TODO 右滑
         * @param
         * @return
         * @throws
         * @author guguangyu
         * @date 2019/11/12 11:31
         */
        public void  slideRight(TestStep step){
            System.out.println("『正常测试』开始执行: "   "<"  step.getDesc()   ">");
            //获取屏幕高度
            int height = step.getAndroidDriver().manage().window().getSize().height;
            //获取页面的宽度
            int width = step.getAndroidDriver().manage().window().getSize().width;
            //设定右滑起点
            PointOption pointOption = PointOption.point(width/4,height/2);
            //Action
            TouchAction touchAction = new TouchAction(step.getAndroidDriver());
            //右滑
            // ·release 意思相当于放开
            //·perform 相当于发送执行
            //waitAction 等待
            touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(PointOption.point(width*3/4,height/2)).release().perform();
        }
    
        /**
         * @description: TODO 自定义滑动
         * @param
         * @return
         * @throws
         * @author guguangyu
         * @date 2019/11/12 11:50
         */
        public void slide(TestStep step){
            System.out.println("『正常测试』开始执行: "   "<"  step.getDesc()   ">");
            try {
                generalSlide(step);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @description: TODO 通用
         * @param
         * @return
         * @throws
         * @author guguangyu
         * @date 2019/11/12 12:02
         */
        public void generalSlide(TestStep step)throws Exception{
            //获取屏幕高度
            int height = step.getAndroidDriver().manage().window().getSize().height;
            //获取页面的宽度
            int width = step.getAndroidDriver().manage().window().getSize().width;
            //获取等待事件
            int during = Integer.valueOf(step.getDetails().get("duration"));
            //滑动次数
            int num = Integer.valueOf(step.getDetails().get("num"));
            Integer[] cor = new Integer[4];
    
            String[] corname = {"xOffSetStart","yOffSetStart","xOffSetEnd","yOffSetEnd"};
            int count = 0;
            for(int i = 0;i < 4;i  ){
                String cc = step.getDetails().get(corname[i]);
                if(!StringUtils.isBlank(cc)){
                    cor[i] = Integer.valueOf(cc);
                }else{
                    count  ;
                }
            }
    
            if(count == 4 ) throw new Exception("滑动的坐标配置不全或错误!");
    
            PointOption pointOption = PointOption.point(cor[0],cor[1]);
            //Action
            TouchAction touchAction = new TouchAction(step.getAndroidDriver());
            //右滑
            // ·release 意思相当于放开
            //·perform 相当于发送执行
            //waitAction 等待
            for(int i=0;i<num;i  ){
                touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(during))).moveTo(PointOption.point(cor[2],cor[3])).release().perform();
            }
        }