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