1. 程式人生 > >Appium左右、上下滑動(Java)

Appium左右、上下滑動(Java)

網上很多文章都說用swipe來左右滑動,你把程式碼一貼,結果報錯,看半天,原來是java-client中swipe早就被廢除了!!!下面介紹一種Java寫法來左右上下滑動:

首先,建立一個Swipe類


import org.openqa.selenium.Dimension;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.PointOption;

public class Swipe {
    // 上滑
    public void SwipeUp(AndroidDriver driver) {
        Dimension size = driver.manage
().window().getSize(); int height = size.height; int width = size.width; new TouchAction(driver).longPress(PointOption.point(width / 2, 100)) .moveTo(PointOption.point(width / 2, height - 100)).release() .perform(); } // 下滑 public void SwipeDown(AndroidDriver driver) { Dimension size = driver.manage
().window().getSize(); int height = size.height; int width = size.width; new TouchAction(driver) .longPress(PointOption.point(width / 2, height - 100)) .moveTo(PointOption.point(width / 2, 100)).release().perform(); } // 左滑 public void SwipeLeft(AndroidDriver driver) { Dimension size = driver.manage
().window().getSize(); int height = size.height; int width = size.width; new TouchAction(driver) .longPress(PointOption.point(width - 100, height / 2)) .moveTo(PointOption.point(100, height / 2)).release().perform(); } // 右滑 public void SwipeRight(AndroidDriver driver) { Dimension size = driver.manage().window().getSize(); int height = size.height; int width = size.width; new TouchAction(driver).longPress(PointOption.point(100, height / 2)) .moveTo(PointOption.point(width - 100, height / 2)).release() .perform(); } }

那麼,在測試類裡就可以進行呼叫

public static AndroidDriver driver;

Swipe swipe=new Swipe();
    swipe.SwipeLeft(driver);

當然,也可以嘗試將longpress換成press,前提是你的測試機足夠靈敏~~