Java自动化测试(App特殊元素定位 34)

2020-11-02 10:56:02 浏览数 (1)

手势操作-滑动

滑动操作 = 点击屏幕某一点 移动 松开

代码语言:javascript复制
@Test
public void test01() {
    // 滑动操作 = 点击屏幕某一点   移动   松开
    // 点击 (365,170) 拖动(365,966)
    TouchAction touchAction = new TouchAction(androidDriver);
    PointOption startPoint = PointOption.point(365, 170);
    PointOption endPoint = PointOption.point(365, 966);
    touchAction.press(startPoint).moveTo(endPoint).release().perform();
}

如上操作不会出现下拉刷新的情况

滑动决定因素:距离/时间

  • 滑动距离
  • 滑动时间

现在增加滑动的时间

代码语言:javascript复制
Duration duration = Duration.ofMillis(2000);
WaitOptions waitOptions = WaitOptions.waitOptions(duration);
touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform();

为了使它更通用使用屏幕的分辨率来计算坐标

代码语言:javascript复制
int width = androidDriver.manage().window().getSize().getWidth();
int height = androidDriver.manage().window().getSize().getWidth();
TouchAction touchAction = new TouchAction(androidDriver);
PointOption startPoint = PointOption.point(width / 2, height / 4);
PointOption endPoint = PointOption.point(width / 2, 3 * height / 4);

对滑动进行封装

代码语言:javascript复制
/**
 * 向下滑动
 *
 * @param times:等待时间
 */
public void swipeDown(long times) {
    int width = androidDriver.manage().window().getSize().getWidth();
    int height = androidDriver.manage().window().getSize().getWidth();
    TouchAction touchAction = new TouchAction(androidDriver);
    PointOption startPoint = PointOption.point(width / 2, height / 4);
    PointOption endPoint = PointOption.point(width / 2, 3 * height / 4);
    Duration duration = Duration.ofMillis(times);
    WaitOptions waitOptions = WaitOptions.waitOptions(duration);
    touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform();
}

/**
 * 向上滑动
 *
 * @param times:等待时间
 */
public void swipeUp(long times) {
    int width = androidDriver.manage().window().getSize().getWidth();
    int height = androidDriver.manage().window().getSize().getWidth();
    TouchAction touchAction = new TouchAction(androidDriver);
    PointOption startPoint = PointOption.point(width / 2, 3 * height / 4);
    PointOption endPoint = PointOption.point(width / 2, height / 4);
    Duration duration = Duration.ofMillis(times);
    WaitOptions waitOptions = WaitOptions.waitOptions(duration);
    touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform();
}

/**
 * 向左滑动
 *
 * @param times:等待时间
 */
public void swipeLeft(long times) {
    int width = androidDriver.manage().window().getSize().getWidth();
    int height = androidDriver.manage().window().getSize().getWidth();
    TouchAction touchAction = new TouchAction(androidDriver);
    PointOption startPoint = PointOption.point(width / 4, height / 2);
    PointOption endPoint = PointOption.point(3 * width / 4, height / 2);
    Duration duration = Duration.ofMillis(times);
    WaitOptions waitOptions = WaitOptions.waitOptions(duration);
    touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform();
}

/**
 * 向右滑动
 *
 * @param times:等待时间
 */
public void swipeRight(long times) {
    int width = androidDriver.manage().window().getSize().getWidth();
    int height = androidDriver.manage().window().getSize().getWidth();
    TouchAction touchAction = new TouchAction(androidDriver);
    PointOption startPoint = PointOption.point(3 * width / 4, height / 2);
    PointOption endPoint = PointOption.point(width / 4, height / 2);
    Duration duration = Duration.ofMillis(times);
    WaitOptions waitOptions = WaitOptions.waitOptions(duration);
    touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform();
}

手势操作-多点触摸

MultiTouchAction可以模拟用户多点触摸操作

  • add:添加
  • perform:执行

实现放大操作

代码语言:javascript复制
@Test
public void test01() throws InterruptedException {
    //手指1
    TouchAction action1 = new TouchAction(androidDriver);
    //手指2
    TouchAction action2 = new TouchAction(androidDriver);
    int y = androidDriver.manage().window().getSize().getHeight();
    int x = androidDriver.manage().window().getSize().getWidth();
    PointOption pointA = PointOption.point(x / 5, y / 5);
    PointOption pointB = PointOption.point(x * 2 / 5, y * 2 / 5);
    PointOption pointC = PointOption.point(x * 3 / 5, y * 3 / 5);
    PointOption pointD = PointOption.point(x * 4 / 5, y * 4 / 5);
    // 放大 B->A C->D
    action1.press(pointB).moveTo(pointA).release();
    action2.press(pointC).moveTo(pointD).release();
    MultiTouchAction multiTouchAction = new MultiTouchAction(androidDriver);
    multiTouchAction.add(action1);
    multiTouchAction.add(action2);
    multiTouchAction.perform();
}
add

0 人点赞