最近公司要求对APP模块自动化,以Android 自动化为例,把appium滑动的方法swipe()再小结下。滑动的目的,一方面是为了更好的查找元素,一方面就是为了滑屏操作。代码如下:

package Util;

import static Util.log.Console.infoLog;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import org.openqa.selenium.OutputType;

import ObjectFactory.DriverFactory;

import Util.log.Console;

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.TouchAction;

import java.time.Duration;

public class SwipScreen {

      private AppiumDriver<?> driver = DriverFactory.getAppiumDriver();

      // 向上滑动

      public void swipUp(int t, int num) {

            File screen = driver.getScreenshotAs(OutputType.FILE);

            try {

                  BufferedImage bufferedImage = ImageIO.read(screen);

                  int width = driver.manage().window().getSize().width;

                  int height = driver.manage().window().getSize().height;

                  Duration duration = Duration.ofSeconds(t / 1000);

                  Console.infoLog("this screen is: " + "width:" + width + ", "

                             + "height:" + height);

                  TouchAction action1 = new TouchAction(driver);

                  for(int i= 0; i < num; i++)

                  {

                       action1.press(width / 2, height * 3 / 4).waitAction(duration).moveTo(width / 2, height / 4).release();

                       action1.perform();

                  }

            } catch (IOException e) {

                  e.printStackTrace();

            }

      }

      // 向下滑动

      public void swipDown(int t, int num) {

            File screen = driver.getScreenshotAs(OutputType.FILE);

            try {

                  BufferedImage bufferedImage = ImageIO.read(screen);

                  int width = bufferedImage.getWidth();

                  int height = bufferedImage.getHeight();

                  Duration duration = Duration.ofSeconds(t / 1000);

                  Console.infoLog("width:" + width + ", " + "height:" + height);

                  TouchAction action1 = new TouchAction(driver);

                  for(int i = 0; i < num; i++)

                  {

                       action1.press(width / 2, height / 4).waitAction(duration).moveTo(width / 2, height * 3 / 4).release();

                      action1.perform();

                  }

            } catch (IOException e) {

                  e.printStackTrace();

            }

      }

      // 向右滑动

      public void swipRight(int t, int num) {

            File screen = driver.getScreenshotAs(OutputType.FILE);

            try {

                  BufferedImage bufferedImage = ImageIO.read(screen);

                  int width = bufferedImage.getWidth();

                  int height = bufferedImage.getHeight();

                  Duration duration = Duration.ofSeconds(t / 1000);

                  Console.infoLog("this screen is: " + "width:" + width + ", "

                             + "height:" + height);

                  TouchAction action1 = new TouchAction(driver);

                  for(int i = 0; i < num; i++)

                  {

                             action1.press(width/20, height / 2).waitAction(duration).moveTo(width*3/ 4, height / 2).release();

                             action1.perform();

                  }

            } catch (IOException e) {

                  e.printStackTrace();

            }

      }

      // 向左滑动

      public void swipLeft(int t, int num) {

            File screen = driver.getScreenshotAs(OutputType.FILE);

            try {

                  BufferedImage bufferedImage = ImageIO.read(screen);

                  int width = bufferedImage.getWidth();

                  int height = bufferedImage.getHeight();

                  Duration duration = Duration.ofSeconds(t / 1000);

                  Console.infoLog("this screen is: " + "width:" + width + ", "

                             + "height:" + height);

                  TouchAction action1 = new TouchAction(driver);

                  for(int i = 0; i < num; i++)

                  {

                       action1.press(width*3/4, height / 3).waitAction(duration).moveTo(width / 20, height / 3).release();

                       action1.perform();

                  }

            } catch (IOException e) {

                  e.printStackTrace();

            }

      }

}

已经封装了滑动的方法,直接使用即可

import Util.SwipScreen;

SwipScreen swip = new SwipScreen();

swip.swipDown(300,1);

这里的 swipDown就是向下滑动,现在很多app页面流行的下拉刷新,或者下拉增量刷新。300 和1分别是int t 和int  num,t是这里是填写毫秒数,这里的 毫秒数越小 滑动的速度越快,一般设定在500~1000。如果你想快速滑动 那就可以设置的更加小。Num是指滑动的次数,如app首页会有很多屏或者滑动到列表底部。就直接输入次数即可。滑动完之后记得睡眠下,然app加载好再做其他事情。

滑动API:Swipe(int start x,int start y,int end x,int y,duration)  
int start x-开始滑动的x坐标;
int start y -开始滑动的y坐标 ;
int end x -结束点x坐标;
int end y -结束点y坐标; 
duration 滑动时间(默认5毫秒)。

Appium--swipe滑动方法的更多相关文章

  1. Appium处理滑动方法是swipe

    滑动API:Swipe(int start x,int start y,int end x,int y,duration) 解释: int start x-开始滑动的x坐标:int start y - ...

  2. appium 处理滑动的方法

    appium 处理滑动的方法是 swipe(int start-x, int start-y, int end-x, int end-y, int during) - Method in class ...

  3. python3+Appium自动化07-滑动操作以及滑动方法封装

    滑动解析 滑动主要分为:水平滑动.垂直滑动.任意方向滑动 滑动轨迹 在Appium中模拟用户滑动操作需要使用swipe方法,该方法定义如下: def swipe(self, start_x, star ...

  4. APPium+Python+iOS屏幕滑动方法对比

    最近在学习appium自动化,对iOS手机进行滑动操作进行总结: 1.mobile:scroll;该方法在实际使用调用时,会滚动2次.执行时间很长. 向下滚动整个屏幕driver.execute_sc ...

  5. Python+Appium自动化测试(8)-swipe()滑动页面

    app自动化测试过程中,经常会遇到滑动屏幕操作,appium框架的话我们可以使用webdriver提供的swipe()方法来对屏幕页面进行上滑.下滑.左滑.右滑操作. 一,swipe方法介绍 swip ...

  6. appium java 滑动(js滑动和swipe滑动)

    最近有一个页面的内容很多,有的元素需要滑动到底部才能看到,所以就研究一下滑动,下面是我学习到的两种滑动方式 一:用js滑动 用js滑动的思路是很简单,首先是先定位到这个元素,定位到之后不做任何操作,然 ...

  7. 移动端图片轮播—swipe滑动插件

    swipe是一个轻量级的移动滑动组件,它可以支持精确的触滑移动操作,能解决移动端对滑动的需求. swipe插件的使用主要有四大块: 一.html <div id='slider' class=' ...

  8. jquery 连写注释;siblings() 方法;jQuery 的3种滑动方法;slideUp()向上滑动;slideDown()向下滑动;slideToggle()来回滑动

    首先我们看两个连写注释 第一个:   /* 点击头像,显示基本资料 */ $(".f-chatTit a.avatar").click(function(){ $(this).hi ...

  9. jQuery 滑动方法slideDown向下滑动元素

    通过 jQuery可以在元素上创建滑动效果,jQuery slideDown() 方法用于向下滑动元素. jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以 ...

  10. 解析6种常用View 的滑动方法

    View 的滑动是Android 实现自定义控件的基础,实现View 滑动有很多种方法,在这里主要讲解6 种滑动方法,分别是layout().offsetLeftAndRight()与offsetTo ...

随机推荐

  1. python网络编程基础(一)

    一.C/S架构 客户端/服务端架构 二.OSI七层架构 七层模型,亦称OSI(Open System Interconnection)参考模型,是参考模型是国际标准化组织(ISO)制定的一个用于计算机 ...

  2. ConcurrentHashMap基于JDK1.8源码剖析

    前言 声明,本文用的是jdk1.8 前面章节回顾: Collection总览 List集合就这么简单[源码剖析] Map集合.散列表.红黑树介绍 HashMap就是这么简单[源码剖析] LinkedH ...

  3. 学习React系列(六)——更新dom细节于原理

    React更新dom的依据: 1.不同类型的elements会产生不同的树 2.通过render方法中包含key属性的子元素,开发者可以示意哪些子元素可能是稳定的. 更新过程: 一.根元素类型不同:旧 ...

  4. 聊聊Servlet、Struts1、Struts2以及SpringMvc中的线程安全

    前言 很多初学者,甚至是工作1-3年的小伙伴们都可能弄不明白?servlet Struts1 Struts2 springmvc 哪些是单例,哪些是多例,哪些是线程安全? 在谈这个话题之前,我们先了解 ...

  5. Vue-cropper 图片裁剪的基本原理

    Vue-cropper 图片裁剪的基本原理 一:裁剪的思路: 1-1,裁剪区域: 需要进行裁剪首先需要形成裁剪区域,裁剪区域的大小和我们的鼠标移动的距离相关联,鼠标移动有多远,裁剪区域就有多大.如下图 ...

  6. thinphp验证码的简单实现

    index.html <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  7. SRM 558 SurroundingGame

    题意: 给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取  2.邻近方格都被选取(有公共边被称为邻近)  不一定要占据所有方格,求最大收益. 输入 ...

  8. [HNOI2010]PLANAR

    题目描述 若能将无向图G=(V,E)画在平面上使得任意两条无重合顶点的边不相交,则称G是平面图.判定一个图是否为平面图的问题是图论中的一个重要问题.现在假设你要判定的是一类特殊的图,图中存在一个包含所 ...

  9. UVA 11584 划分回文字串

    将其划分为尽可能少的回文串 dp[i] = min(dp[i],dp[j] + 1)    来表示j+1~i是回文串 #include <iostream> #include <cs ...

  10. [BZOJ]1069: [SCOI2007]最大土地面积

    题目大意:给出二维平面上n个点,求最大的由这些点组成的四边形面积.(n<=2000) 思路:求出凸包后旋转卡壳枚举对踵点对作为四边形的对角线,枚举或二分另外两个点,复杂度O(n^2)或O(nlo ...