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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
| public static final String APP_ID = "com.taobao.idlefish";
private static AppiumDriverLocalService service;
private static AndroidDriver<AndroidElement> driver;
private static WebDriverWait wait;
private void initialization() throws MalformedURLException {
initialization(APP_ID, "com.taobao.idlefish.router.JumpActivity", true, false);
}
private void initialization1() throws MalformedURLException {
initialization("com.miui.calculator", ".cal.CalculatorActivity", null, null);
}
/**
* @param noReset null will remove from the caps.
* @param autoLaunch null will remove from the caps.
*/
protected void initialization(String appPackage, String appActivity, Boolean noReset, Boolean autoLaunch) throws MalformedURLException {
URL url = new URL("http://localhost:4723/wd/hub");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName", "UiAutomator2" /* "Appium" */);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
capabilities.setCapability("appPackage", appPackage);
capabilities.setCapability("appActivity", appActivity);
// http://appium.io/docs/en/writing-running-appium/caps/
capabilities.setCapability("noReset", noReset);
capabilities.setCapability("autoLaunch", autoLaunch);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 100);
driver = new AndroidDriver<>(url, capabilities);
wait = new WebDriverWait(driver, 20);
}
protected void finishing() {
try {
if (driver != null) {
driver.quit();
}
if (service != null) {
service.stop();
}
} catch (Exception e) {
} finally {
driver = null;
service = null;
}
}
protected void swiping() {
Dimension windowSize = getSize();
int startX = windowSize.width / 2;
int startY = (int) (windowSize.height * 0.8);
int endX = windowSize.width / 2;
int endY = (int) (windowSize.height * 0.1);
swipe(startX, startY, endX, endY, 1200);
}
protected void swipe(int x1, int y1, int x2, int y2, int waitMillis) {
new TouchAction<>(driver) //
.press(ElementOption.point(x1, y1))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(waitMillis)))
.moveTo(ElementOption.point(x2, y2))
.release()
.perform();
}
protected void touch(int x, int y) {
new TouchAction<>(driver).tap(TapOptions.tapOptions().withPosition(PointOption.point(x, y))).perform();
}
protected void touch(Point p) {
touch(p.x, p.y);
}
protected Dimension getSize() {
return driver.manage().window().getSize();
}
protected Point getCenter() {
Dimension size = getSize();
return new Point(size.width / 2, size.height / 2);
}
private void touchAndWait() throws InterruptedException {
touchAndWait(null);
}
private void touchAndWait(By by) throws InterruptedException {
// 等一下界面效果
// Thread.sleep(500);
TimeUnit.MILLISECONDS.sleep(500);
try {
swiping();
} catch (Exception e) {
touch(getCenter());
}
if (by != null) {
wait.until(ExpectedConditions.presenceOfElementLocated(by));
}
}
private void doCalculator() throws InterruptedException {
By btn_9_s = By.id("com.miui.calculator:id/btn_9_s");
By btn_plus_s = By.id("com.miui.calculator:id/btn_plus_s");
By btn_3_s = By.id("com.miui.calculator:id/btn_3_s");
By btn_equal_s = By.id("com.miui.calculator:id/btn_equal_s");
By textViews = By.className("android.widget.TextView");
touchAndWait(btn_9_s);
driver.findElement(btn_9_s).click();
// driver.findElementByAccessibilityId("加");
driver.findElement(btn_plus_s).click();
driver.findElement(btn_3_s).click();
driver.findElement(btn_equal_s).click();
touchAndWait(textViews);
List<? extends WebElement> allText = driver.findElements(textViews);
boolean isOk = false;
for (int i = 0; i < allText.size(); i++) {
String txt = allText.get(i).getText();
System.out.println(txt);
// 由于第一次无法根据元素的ClassName确定顺序,所以使用遍历的方式进行断言
if (txt.equals("= 12")) {
isOk = true;
break;
}
}
if (isOk) {
System.out.println("测试成功.");
} else {
System.out.println("测试失败.");
}
}
public void testCalculator() throws Exception {
try {
initialization1();
doCalculator();
} finally {
finishing();
}
}
public void testHello() throws InterruptedException, MalformedURLException {
try {
this.initialization1();
doCalculator();
driver.activateApp(APP_ID);
// driver.startActivity(new Activity(APP_ID, "com.taobao.idlefish.router.JumpActivity"));
test0();
test1();
} finally {
this.finishing();
}
}
private String toString(Rectangle rect) {
return String.format("(%s,%s,%s,%s)", rect.x, rect.y, rect.width, rect.height);
}
private void printAllView() throws Exception {
try {
List<AndroidElement> allText = driver.findElementsByXPath("//*");
for (int i = 0; i < allText.size(); i++) {
AndroidElement ele = allText.get(i);
System.out.println("===========================");
System.out.println(ele.getTagName() + ","
+ ele.isSelected() + ","
+ ele.isEnabled() + ","
+ ele.isDisplayed() + ","
+ ele.getText() + ","
+ toString(ele.getRect()));
System.out.println(ele.getAttribute("class"));
System.out.println(ele.getAttribute("content-desc"));
System.out.println(ele.getAttribute("text"));
}
} catch (Exception e) {
String errMsg = e.getMessage();
System.err.println(errMsg);
if (errMsg.contains("A session is either terminated or not started")) {
try {
this.finishing();
} catch (Exception ex) {
}
this.initialization();
}
}
}
private void doProcessManual() throws Exception {
while (true) {
printAllView();
swiping();
Thread.sleep(1000);
}
}
public void testXianyu() throws Exception {
try {
this.initialization();
doProcessManual();
} finally {
this.finishing();
}
}
private void test0() {
System.out.println("appId: " + driver.getCurrentPackage());
System.out.println("activity: " + driver.currentActivity());
System.out.println(APP_ID + " installed?: " + driver.isAppInstalled(APP_ID));
// driver.runAppInBackground(Duration.ofSeconds(-1));
// driver.activateApp(appId);
// driver.closeApp();
// driver.resetApp();
// driver.rotate(new DeviceRotation(0, 0, 0));
// driver.launchApp();
}
private void test1() {
System.out.println("AppStringMap size: " + driver.getAppStringMap().size());
System.out.println("en AppStringMap size: " + driver.getAppStringMap("en").size());
System.out.println("build: " + driver.getStatus().get("build"));
System.out.println("DeviceTime: " + driver.getDeviceTime());
}
|