前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java自动化测试(App特殊元素定位 34)

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

作者头像
zx钟
发布2020-11-02 10:56:02
3190
发布2020-11-02 10:56:02
举报
文章被收录于专栏:测试游记

手势操作-滑动

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

代码语言: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();
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-10-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 测试游记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 手势操作-滑动
  • 滑动决定因素:距离/时间
  • 手势操作-多点触摸
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档