forked from anandbagmar/AppiumJavaSample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppiumNativeAndroidParallelCalcTest.java
executable file
·114 lines (95 loc) · 4.02 KB
/
AppiumNativeAndroidParallelCalcTest.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
package com.eot.sample.android;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.testng.ITestResult;
import org.testng.annotations.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import static com.eot.sample.Hooks.*;
public class AppiumNativeAndroidParallelCalcTest {
private final HashMap<String, AppiumDriver> drivers = new HashMap<>();
@DataProvider(name = "device-provider", parallel = true)
public Object[][] provide() {
return new Object[][]{{"emulator-5554", 2, 5}, {"emulator-5556", 3, 6}};
}
@BeforeSuite
public void beforeAll() {
startAppiumServer();
}
@AfterSuite
public void afterAll() {
stopAppiumServer();
}
@BeforeMethod
public void beforeMethod(Object[] testArgs) {
String methodName = ((Method) testArgs[0]).getName();
String udid = (String) testArgs[2];
log(String.format("Running test '%s' on '%s'", methodName, udid));
log(String.format("Create AppiumDriver for - %s:%s", udid));
AppiumDriver driver = createAppiumDriver(getAppiumServerUrl(), udid);
drivers.put(udid, driver);
log(String.format("Created AppiumDriver for - %s:%s", udid));
}
@AfterMethod
public void afterMethod(Object[] testArgs) {
log(testArgs.toString());
String methodName = ((Method) testArgs[0]).getName();
ITestResult result = ((ITestResult) testArgs[1]);
log(String.format("Test '%s' result: '%s'", methodName, result.toString()));
String udid = (String) testArgs[2];
Integer systemPort = (Integer) testArgs[3];
AppiumDriver driver = drivers.get(udid);
try {
if (null != driver) {
driver.quit();
}
log(String.format("Visual Validation Results for - %s:%s", udid, systemPort));
} catch (Exception e) {
log("Exception - " + e.getMessage());
e.printStackTrace();
} finally {
}
}
@Test(dataProvider = "device-provider", threadPoolSize = 2)
public void runTest(Method method, ITestResult testResult, String udid, int num1, int num2) {
log(String.format("Runnng test on %s:%s, appiumPort - ", udid));
log(String.format("drivers.size()=%d", drivers.size()));
AppiumDriver driver = drivers.get(udid);
try {
driver.findElement(By.id("digit_" + num1))
.click();
driver.findElement(By.id("op_add"))
.click();
driver.findElement(By.id("digit_" + num2))
.click();
} catch (Exception e) {
log(e.toString());
} finally {
if (null != driver) {
driver.quit();
}
}
}
private void log(String message) {
System.out.println(" ### " + new Date() + " ### " + message);
}
private AppiumDriver createAppiumDriver(URL appiumServerUrl, String udid) {
// Appium 1.x
// DesiredCapabilities capabilities = new DesiredCapabilities();
// Appium 2.x
UiAutomator2Options capabilities = new UiAutomator2Options();
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11");
capabilities.setCapability(MobileCapabilityType.UDID, udid);
capabilities.setCapability("app", "src/test/resources/sampleApps/AndroidCalculator.apk");
capabilities.setCapability(MobileCapabilityType.NO_RESET, false);
capabilities.setCapability(MobileCapabilityType.FULL_RESET, false);
return new AppiumDriver(appiumServerUrl, capabilities);
}
}