forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidTouchTest.java
202 lines (167 loc) · 9.03 KB
/
AndroidTouchTest.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package io.appium.java_client.android;
import static io.appium.java_client.TestUtils.getCenter;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.WaitOptions.waitOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
import static io.appium.java_client.touch.offset.PointOption.point;
import static java.time.Duration.ofSeconds;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import java.util.List;
public class AndroidTouchTest extends BaseAndroidTest {
@BeforeEach
public void setUp() {
driver.resetApp();
}
@Test public void dragNDropByElementTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo");
driver.startActivity(activity);
WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1"));
WebElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3"));
WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text"));
assertEquals("Drag text not empty", "", dragText.getText());
TouchAction dragNDrop = new TouchAction(driver)
.longPress(element(dragDot1))
.moveTo(element(dragDot3))
.release();
dragNDrop.perform();
assertNotEquals("Drag text empty", "", dragText.getText());
}
@Test public void dragNDropByElementAndDurationTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo");
driver.startActivity(activity);
WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1"));
WebElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3"));
WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text"));
assertEquals("Drag text not empty", "", dragText.getText());
TouchAction dragNDrop = new TouchAction(driver)
.longPress(longPressOptions()
.withElement(element(dragDot1))
.withDuration(ofSeconds(2)))
.moveTo(element(dragDot3))
.release();
dragNDrop.perform();
assertNotEquals("Drag text empty", "", dragText.getText());
}
@Test public void dragNDropByCoordinatesTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo");
driver.startActivity(activity);
WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1"));
WebElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3"));
WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text"));
assertEquals("Drag text not empty", "", dragText.getText());
Point center1 = getCenter(dragDot1);
Point center2 = getCenter(dragDot3);
TouchAction dragNDrop = new TouchAction(driver)
.longPress(point(center1.x, center1.y))
.moveTo(point(center2.x, center2.y))
.release();
dragNDrop.perform();
assertNotEquals("Drag text empty", "", dragText.getText());
}
@Test public void dragNDropByCoordinatesAndDurationTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo");
driver.startActivity(activity);
WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1"));
WebElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3"));
WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text"));
assertEquals("Drag text not empty", "", dragText.getText());
Point center1 = getCenter(dragDot1);
Point center2 = getCenter(dragDot3);
TouchAction dragNDrop = new TouchAction(driver)
.longPress(longPressOptions()
.withPosition(point(center1.x, center1.y))
.withDuration(ofSeconds(2)))
.moveTo(point(center2.x, center2.y))
.release();
dragNDrop.perform();
assertNotEquals("Drag text empty", "", dragText.getText());
}
@Test public void pressByCoordinatesTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.Buttons1");
driver.startActivity(activity);
Point point = driver.findElement(By.id("io.appium.android.apis:id/button_toggle")).getLocation();
new TouchAction(driver)
.press(point(point.x + 20, point.y + 30))
.waitAction(waitOptions(ofSeconds(1)))
.release()
.perform();
assertEquals("ON", driver.findElement(By.id("io.appium.android.apis:id/button_toggle")).getText());
}
@Test public void pressByElementTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.Buttons1");
driver.startActivity(activity);
new TouchAction(driver)
.press(element(driver.findElement(By.id("io.appium.android.apis:id/button_toggle"))))
.waitAction(waitOptions(ofSeconds(1)))
.release()
.perform();
assertEquals("ON", driver.findElement(By.id("io.appium.android.apis:id/button_toggle")).getText());
}
@Test public void tapActionTestByElement() throws Exception {
Activity activity = new Activity("io.appium.android.apis", ".view.ChronometerDemo");
driver.startActivity(activity);
WebElement chronometer = driver.findElement(By.id("io.appium.android.apis:id/chronometer"));
TouchAction startStop = new TouchAction(driver)
.tap(tapOptions().withElement(element(driver.findElement(By.id("io.appium.android.apis:id/start")))))
.waitAction(waitOptions(ofSeconds(2)))
.tap(tapOptions().withElement(element(driver.findElement(By.id("io.appium.android.apis:id/stop")))));
startStop.perform();
String time = chronometer.getText();
assertNotEquals(time, "Initial format: 00:00");
Thread.sleep(2500);
assertEquals(time, chronometer.getText());
}
@Test public void tapActionTestByCoordinates() throws Exception {
Activity activity = new Activity("io.appium.android.apis", ".view.ChronometerDemo");
driver.startActivity(activity);
WebElement chronometer = driver.findElement(By.id("io.appium.android.apis:id/chronometer"));
Point center1 = getCenter(driver.findElement(By.id("io.appium.android.apis:id/start")));
TouchAction startStop = new TouchAction(driver)
.tap(point(center1.x, center1.y))
.tap(element(driver.findElement(By.id("io.appium.android.apis:id/stop")), 5, 5));
startStop.perform();
String time = chronometer.getText();
assertNotEquals(time, "Initial format: 00:00");
Thread.sleep(2500);
assertEquals(time, chronometer.getText());
}
@Test public void horizontalSwipingTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.Gallery1");
driver.startActivity(activity);
WebElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));
List<WebElement> images = gallery.findElements(AppiumBy.className("android.widget.ImageView"));
int originalImageCount = images.size();
Point location = gallery.getLocation();
Point center = getCenter(gallery);
TouchAction swipe = new TouchAction(driver)
.press(element(images.get(2),-10, center.y - location.y))
.waitAction(waitOptions(ofSeconds(2)))
.moveTo(element(gallery,10,center.y - location.y))
.release();
swipe.perform();
assertNotEquals(originalImageCount,
gallery.findElements(AppiumBy.className("android.widget.ImageView")).size());
}
@Test public void multiTouchTest() {
Activity activity = new Activity("io.appium.android.apis", ".view.Buttons1");
driver.startActivity(activity);
TouchAction press = new TouchAction(driver)
.press(element(driver.findElement(By.id("io.appium.android.apis:id/button_toggle"))))
.waitAction(waitOptions(ofSeconds(1)))
.release();
new MultiTouchAction(driver)
.add(press)
.perform();
assertEquals("ON", driver.findElement(By.id("io.appium.android.apis:id/button_toggle")).getText());
}
}