Skip to content

Commit d1fcced

Browse files
committed
Bài 20 - Multiple Platform - Parallel Execution
1 parent 50b3240 commit d1fcced

34 files changed

+2541
-0
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store
39+
.idea/
40+
out/
41+
*.png
42+
*.jpg
43+
exports/
44+
logs/

pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.anhtester</groupId>
8+
<artifactId>AppiumJava122024_Multi_Platform</artifactId>
9+
<version>2.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
19+
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
20+
<dependency>
21+
<groupId>org.testng</groupId>
22+
<artifactId>testng</artifactId>
23+
<version>7.11.0</version>
24+
</dependency>
25+
26+
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
27+
<dependency>
28+
<groupId>io.appium</groupId>
29+
<artifactId>java-client</artifactId>
30+
<version>9.4.0</version>
31+
</dependency>
32+
33+
<!-- https://mvnrepository.com/artifact/io.github.ashwithpoojary98/appium_flutterfinder_java -->
34+
<dependency>
35+
<groupId>io.github.ashwithpoojary98</groupId>
36+
<artifactId>appium_flutterfinder_java</artifactId>
37+
<version>1.0.10</version>
38+
</dependency>
39+
40+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
41+
<dependency>
42+
<groupId>org.slf4j</groupId>
43+
<artifactId>slf4j-api</artifactId>
44+
<version>2.0.17</version>
45+
</dependency>
46+
47+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
48+
<dependency>
49+
<groupId>org.slf4j</groupId>
50+
<artifactId>slf4j-simple</artifactId>
51+
<version>2.0.17</version>
52+
<scope>test</scope>
53+
</dependency>
54+
55+
</dependencies>
56+
57+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.anhtester.drivers;
2+
3+
import io.appium.java_client.android.AndroidDriver;
4+
5+
public class AndroidDriverManager {
6+
private static ThreadLocal<AndroidDriver> driver = new ThreadLocal<>();
7+
8+
public static void setDriver(AndroidDriver driverInstance) {
9+
driver.set(driverInstance);
10+
}
11+
12+
public static AndroidDriver getDriver() {
13+
return driver.get();
14+
}
15+
16+
public static void closeDriver() {
17+
if (driver.get() != null) {
18+
getDriver().close();
19+
}
20+
}
21+
22+
public static void quitDriver() {
23+
if (driver.get() != null) {
24+
getDriver().quit();
25+
driver.remove();
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.anhtester.drivers;
2+
3+
import io.appium.java_client.AppiumDriver;
4+
5+
public class DriverManager {
6+
private static ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();
7+
8+
public static void setDriver(AppiumDriver driverInstance) {
9+
driver.set(driverInstance);
10+
}
11+
12+
public static AppiumDriver getDriver() {
13+
return driver.get();
14+
}
15+
16+
public static void closeDriver() {
17+
if (driver.get() != null) {
18+
getDriver().close();
19+
}
20+
}
21+
22+
public static void quitDriver() {
23+
if (driver.get() != null) {
24+
getDriver().quit();
25+
driver.remove();
26+
}
27+
}
28+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.anhtester.helpers;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.text.Normalizer;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Locale;
11+
import java.util.regex.Pattern;
12+
13+
public class SystemHelpers {
14+
15+
private static final Pattern NONLATIN = Pattern.compile("[^\\w-]");
16+
private static final Pattern WHITESPACE = Pattern.compile("[\\s]");
17+
18+
public static String makeSlug(String input) {
19+
if (input == null)
20+
throw new IllegalArgumentException();
21+
22+
String noWhiteSpace = WHITESPACE.matcher(input).replaceAll("_");
23+
String normalized = Normalizer.normalize(noWhiteSpace, Normalizer.Form.NFD);
24+
String slug = NONLATIN.matcher(normalized).replaceAll("");
25+
return slug.toLowerCase(Locale.ENGLISH);
26+
}
27+
28+
/**
29+
* @return Get the path to your source directory with a / at the end
30+
*/
31+
public static String getCurrentDir() {
32+
String current = System.getProperty("user.dir") + File.separator;
33+
return current;
34+
}
35+
36+
public static boolean createFolder(String path) {
37+
try {
38+
File folder = new File(path);
39+
40+
// Kiểm tra xem đã tồn tại và có phải là folder không
41+
if (folder.exists() && folder.isDirectory()) {
42+
System.out.println("Folder đã tồn tại: " + path);
43+
return false;
44+
}
45+
46+
// Tạo folder và các thư mục cha
47+
boolean created = folder.mkdirs();
48+
49+
if (created) {
50+
System.out.println("Tạo folder thành công: " + path);
51+
} else {
52+
System.out.println("Tạo folder thất bại: " + path);
53+
}
54+
55+
return created;
56+
} catch (Exception e) {
57+
System.out.println("Lỗi khi tạo folder: " + e.getMessage());
58+
return false;
59+
}
60+
}
61+
62+
/**
63+
* @param str string to be split based on condition
64+
* @param valueSplit the character to split the string into an array of values
65+
* @return array of string values after splitting
66+
*/
67+
public static ArrayList<String> splitString(String str, String valueSplit) {
68+
ArrayList<String> arrayListString = new ArrayList<>();
69+
for (String s : str.split(valueSplit, 0)) {
70+
arrayListString.add(s);
71+
}
72+
return arrayListString;
73+
}
74+
75+
public static boolean checkValueInListString(String expected, String listValues[]) {
76+
boolean found = false;
77+
78+
for (String s : listValues) {
79+
if (s.equals(expected)) {
80+
found = true;
81+
break;
82+
}
83+
}
84+
return found;
85+
}
86+
87+
public static boolean checkValueInListString(String expected, List<String> listValues) {
88+
boolean found = false;
89+
90+
for (String s : listValues) {
91+
if (s.equals(expected)) {
92+
found = true;
93+
break;
94+
}
95+
}
96+
return found;
97+
}
98+
99+
public static void killProcessOnPort(String port) {
100+
String command = "";
101+
102+
// Check OS to set command to find and kill process
103+
if (System.getProperty("os.name").toLowerCase().contains("win")) {
104+
command = "cmd /c netstat -ano | findstr :" + port;
105+
} else {
106+
command = "lsof -i :" + port;
107+
}
108+
109+
try {
110+
Process process = Runtime.getRuntime().exec(command);
111+
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
112+
String line;
113+
while ((line = reader.readLine()) != null) {
114+
String[] tokens = line.trim().split("\\s+");
115+
String pid = tokens[1]; // PID position may vary by OS
116+
if (System.getProperty("os.name").toLowerCase().contains("win")) {
117+
Runtime.getRuntime().exec("taskkill /F /PID " + pid);
118+
} else {
119+
Runtime.getRuntime().exec("kill -9 " + pid);
120+
}
121+
}
122+
reader.close();
123+
process.waitFor();
124+
System.out.println("####### Kill process on port " + port + " successfully.");
125+
} catch (IOException | InterruptedException e) {
126+
e.printStackTrace();
127+
}
128+
}
129+
130+
public static void startAppiumWithPlugins(String server, String port) {
131+
ProcessBuilder processBuilder = new ProcessBuilder(
132+
"appium",
133+
"-a", server,
134+
"-p", port,
135+
"-ka", "800",
136+
"--use-plugins", "appium-reporter-plugin,element-wait,gestures,device-farm,appium-dashboard",
137+
"-pa", "/",
138+
"--plugin-device-farm-platform", "android"
139+
);
140+
141+
// Redirect error and output streams
142+
processBuilder.redirectErrorStream(true);
143+
144+
try {
145+
// Start the process
146+
Process process = processBuilder.start();
147+
System.out.println("Appium server started with plugins.");
148+
149+
// Optional: Read the output (if needed for debugging)
150+
new Thread(() -> {
151+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
152+
String line;
153+
while ((line = reader.readLine()) != null) {
154+
System.out.println(line);
155+
}
156+
} catch (IOException e) {
157+
e.printStackTrace();
158+
}
159+
}).start();
160+
161+
} catch (IOException e) {
162+
e.printStackTrace();
163+
}
164+
}
165+
166+
}

0 commit comments

Comments
 (0)