11package com .appium .executor ;
22
33import static com .appium .filelocations .FileLocations .PARALLEL_XML_LOCATION ;
4- import static com .appium .utils .ConfigFileManager .CATEGORY ;
5- import static com .appium .utils .ConfigFileManager .EXCLUDE_GROUPS ;
6- import static com .appium .utils .ConfigFileManager .INCLUDE_GROUPS ;
7- import static com .appium .utils .ConfigFileManager .LISTENERS ;
8- import static com .appium .utils .ConfigFileManager .RUNNER_LEVEL ;
9- import static com .appium .utils .ConfigFileManager .SUITE_NAME ;
4+ import static com .appium .utils .ConfigFileManager .*;
105import static com .appium .utils .FigletHelper .figlet ;
116import static java .lang .System .getProperty ;
127import static java .util .Collections .addAll ;
2318import org .testng .annotations .Test ;
2419import org .testng .collections .Lists ;
2520import org .testng .xml .XmlClass ;
21+ import org .testng .xml .XmlInclude ;
2622import org .testng .xml .XmlSuite ;
2723import org .testng .xml .XmlSuite .ParallelMode ;
2824import org .testng .xml .XmlTest ;
3329import java .lang .reflect .Method ;
3430import java .net .MalformedURLException ;
3531import java .net .URL ;
36- import java .util .ArrayList ;
37- import java .util .Collection ;
38- import java .util .HashMap ;
39- import java .util .Iterator ;
40- import java .util .List ;
41- import java .util .Map ;
42- import java .util .Set ;
32+ import java .util .*;
4333
4434public class ATDExecutor {
4535 private final DeviceAllocationManager deviceAllocationManager ;
@@ -65,7 +55,9 @@ public boolean constructXMLAndTriggerParallelRunner(List<String> test, String pa
6555 if (runnerLevel != null && runnerLevel .equalsIgnoreCase ("class" )) {
6656 constructXmlSuiteForClassLevelDistributionRunner (test , getTestMethods (setOfMethods ),
6757 suiteName , categoryName , deviceCount );
68- } else {
58+ } else if (test !=null && !test .isEmpty ()){
59+ constructXmlAndExecuteTestCaseAtRuntime (test , getTestMethods (setOfMethods ), suiteName , categoryName , deviceCount );
60+ }else {
6961 constructXmlSuiteForMethodLevelDistributionRunner (test ,
7062 getTestMethods (setOfMethods ), suiteName , categoryName , deviceCount );
7163 }
@@ -112,8 +104,8 @@ public XmlSuite constructXmlSuiteForParallelRunner(List<String> tests,
112104 }
113105
114106 public XmlSuite constructXmlSuiteForClassLevelDistributionRunner (List <String > tests ,
115- Map <String , List <Method >> methods ,
116- String suiteName , String categoryName , int deviceCount ) {
107+ Map <String , List <Method >> methods ,
108+ String suiteName , String categoryName , int deviceCount ) {
117109 XmlSuite suite = new XmlSuite ();
118110 suite .setName (suiteName );
119111 suite .setThreadCount (deviceCount );
@@ -138,8 +130,8 @@ public XmlSuite constructXmlSuiteForClassLevelDistributionRunner(List<String> te
138130
139131
140132 public XmlSuite constructXmlSuiteForMethodLevelDistributionRunner (List <String > tests ,
141- Map <String , List <Method >> methods , String suiteName ,
142- String category , int deviceCount ) {
133+ Map <String , List <Method >> methods , String suiteName ,
134+ String category , int deviceCount ) {
143135 include (groupsInclude , INCLUDE_GROUPS );
144136 XmlSuite suite = new XmlSuite ();
145137 suite .setName (suiteName );
@@ -163,6 +155,42 @@ public XmlSuite constructXmlSuiteForMethodLevelDistributionRunner(List<String> t
163155 return suite ;
164156 }
165157
158+ public XmlSuite constructXmlAndExecuteTestCaseAtRuntime (List <String > testCases ,
159+ Map <String , List <Method >> methods , String suiteName , String category , int deviceCount ) {
160+
161+ List <XmlClass > classes = new ArrayList <>(); // equivalent of <classes> tag
162+ XmlSuite suite = new XmlSuite ();
163+ suite .setName (suiteName );
164+ XmlTest test = new XmlTest (suite );
165+ test .setName ("regression" );
166+ suite .setThreadCount (deviceCount );
167+ suite .setDataProviderThreadCount (deviceCount );
168+ suite .setVerbose (2 );
169+ suite .setParallel (ParallelMode .METHODS );
170+ listeners .add ("com.appium.manager.AppiumParallelMethodTestListener" );
171+ include (listeners , LISTENERS );
172+ suite .setListeners (listeners );
173+ for (Map .Entry <String , List <Method >> mapElement : methods .entrySet ()) {
174+ XmlClass xmlClass = new XmlClass (mapElement .getKey ());
175+ for (String testName : testCases ) {
176+ for (Method methodName : mapElement .getValue ()) {
177+ if (methodName .getName ().equalsIgnoreCase (testName )) {
178+ List <XmlInclude > includedMethodsList = new ArrayList <>();
179+ XmlInclude includedTestMethod = new XmlInclude (testName );
180+ includedMethodsList .add (includedTestMethod );
181+ xmlClass .setIncludedMethods (includedMethodsList );
182+ classes .add (xmlClass );
183+ }
184+ }
185+ }
186+ }
187+
188+ test .setXmlClasses (classes );
189+ writeTestNGFile (suite );
190+ return suite ;
191+
192+ }
193+
166194 public boolean testNGParallelRunner () {
167195 TestNG testNG = new TestNG ();
168196 List <String > suites = Lists .newArrayList ();
@@ -194,7 +222,7 @@ private Set<Method> getMethods(String pack) throws MalformedURLException {
194222 a ++;
195223 }
196224 Reflections reflections = new Reflections (new ConfigurationBuilder ().setUrls (newUrls )
197- .setScanners (new MethodAnnotationsScanner ()));
225+ .setScanners (new MethodAnnotationsScanner ()));
198226 return reflections .getMethodsAnnotatedWith (Test .class );
199227 }
200228
@@ -224,7 +252,7 @@ private List<XmlClass> writeXmlClass(List<String> testCases, Map<String,
224252
225253 private void writeTestNGFile (XmlSuite suite ) {
226254 try (FileWriter writer = new FileWriter (new File (
227- getProperty ("user.dir" ) + PARALLEL_XML_LOCATION ))) {
255+ getProperty ("user.dir" ) + PARALLEL_XML_LOCATION ))) {
228256 writer .write (suite .toXml ());
229257 writer .flush ();
230258 } catch (IOException e ) {
@@ -243,9 +271,9 @@ public Map<String, List<Method>> getTestMethods(Set<Method> methods) {
243271 Map <String , List <Method >> listOfMethods = new HashMap <>();
244272 methods .forEach (method -> {
245273 List <Method > methodsList = listOfMethods .computeIfAbsent (
246- method .getDeclaringClass ().getPackage ().getName ()
247- + "." + method .getDeclaringClass ()
248- .getSimpleName (), k -> new ArrayList <>());
274+ method .getDeclaringClass ().getPackage ().getName ()
275+ + "." + method .getDeclaringClass ()
276+ .getSimpleName (), k -> new ArrayList <>());
249277 methodsList .add (method );
250278 });
251279 return listOfMethods ;
0 commit comments