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