一套appium 滑動方法的封裝,滑動方向及滑動次數
阿新 • • 發佈:2019-02-13
appium做自動化測試時,經常需要用到滑動螢幕功能,我這裡封裝一個支援前後左右滑動,且支援設定滑動次數,show me your code ,還是看程式碼吧
public enum ORIENTATION { UP(8), DOWN(2), LEFT(4), RIGHT(6); ORIENTATION(int orientCode) { this.orientCode = orientCode; } private int orientCode; public int getOrientCode() { return orientCode; } public void setOrientCode(int orientCode) { this.orientCode = orientCode; } public boolean equals(ORIENTATION orient) { if (this.orientCode == orient.getOrientCode()) { return true; } return false; } } public void swipe(ORIENTATION orient, Integer times) { Dimension size = getDriver().manage().window().getSize(); int height = size.height; int width = size.width; int x1Offset = 0; int y1Offset = 0; int x2Offset = 0; int y2Offset = 0; switch (orient) { case UP: x1Offset = width / 2; y1Offset = height * 3 / 4; x2Offset = width / 2; y2Offset = height / 4; break; case DOWN: x1Offset = width / 2; y1Offset = height / 4; x2Offset = width / 2; y2Offset = height * 3 / 4; break; case LEFT: x1Offset = width * 4 / 5; y1Offset = height / 2; x2Offset = width / 5; y2Offset = height / 2; break; case RIGHT: x1Offset = width / 5; y1Offset = height / 2; x2Offset = width * 4 / 5; y2Offset = height / 2; break; } int swipeTimes = Optional.ofNullable(times).orElse(1); for (int i = 0; i < swipeTimes; i++) { swipe(x1Offset, y1Offset, x2Offset, y2Offset); } } private void swipe(int x1Offset, int y1Offset, int x2Offset, int y2Offset) { TouchAction touchAction = new TouchAction<>(getDriver()); touchAction.press(PointOption.point(x1Offset, y1Offset)) .waitAction() .moveTo(PointOption.point(x2Offset, y2Offset)) .release() .perform(); }
首先封裝一個滑動方向的列舉,支援前後左右,然後獲取手機螢幕尺寸,根據需要滑動的方向進而設定滑動幅度所佔比例,若傳入的滑動次數times為null,則預設滑動一次,測試時你可以這樣使用:
swipe(ORIENTATION.LEFT, 4); //向左滑動4次
swipe(ORIENTATION.UP, null); //向上滑動1次