Appium--swipe滑動方法


最近公司要求對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毫秒)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM