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

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

手势操作-滑动

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

1
2
3
4
5
6
7
8
9
@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();
}

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

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

  • 滑动距离
  • 滑动时间

现在增加滑动的时间

1
2
3
Duration duration = Duration.ofMillis(2000);
WaitOptions waitOptions = WaitOptions.waitOptions(duration);
touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform();

下拉刷新

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

1
2
3
4
5
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);

对滑动进行封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 向下滑动
*
* @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:执行

实现放大操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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();
}
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!