...18import org.testng.xml.XmlClass;19import org.testng.xml.XmlGroups;20import org.testng.xml.XmlPackage;21import org.testng.xml.XmlRun;22import org.testng.xml.XmlSuite;23import org.testng.xml.XmlSuite.ParallelMode;24import org.testng.xml.XmlTest;2526/*import com.surveymonkey.lib.Constant;27import com.surveymonkey.lib.Grid;*/2829public class DriverScript {3031 /**32 * Copyright © 1999 - 2017 SurveyMonkey All rights reserved.33 *34 * This software is the confidential and proprietary information of35 * SurveyMonkey, Inc. ("Confidential Information"). You shall not disclose such36 * Confidential Information and shall use it only in accordance with the terms37 * of the license agreement you entered into with Sun.38 * 39 */4041 /**42 * This driver script for Mobile Automation project, it is main class of this43 * project. It gets following details from Jenkins job and create TestNG.xml44 * file programmatically 1. Platform : Android / iOS 2. TestSuite : Smoke /BVT /45 * SmallRegression / Regression 3. Feature: User/Billing etc 4. Module: SignIn,46 * Create Survey etc. 5. Env: MT1 / MT2 / MT3 / PROD 6. TestRunID: TestRail test47 * run id like 10661, 8747 Created TestNG.xml would be available with name48 * programmedTestNG.xml in root directory of the project. It contains details of49 * test cases for execution. This driver script also load the OR properties and50 * test data.51 * 52 * @author pankaj.shukla53 * @version 1.054 * @since 2017-03-3155 * 56 * Ref link :57 * //http://toolsqa.com/mobile-automation/appium/appium-parallel-execution-using-testng/58 */59 public static String platform = null;60 public static final Logger logger = Logger.getLogger(DriverScript.class.getName());6162 public static void main(String[] args) throws JSONException, InterruptedException, IOException {6364 logger.info("Env : " + System.getProperty("Env"));65 logger.info("Browser :" + System.getProperty("Browser"));66 logger.info("TestSuite : "+System.getProperty("TestSuite"));6768 /*Properties envUsed = new Properties();69 envUsed.setProperty("env", System.getProperty("Env"));70 envUsed.store(71 new FileWriter(72 System.getProperty("user.dir") + "\\src\\main\\resources\\properties\\envconfig.properties"),73 "autogenerated");*/7475 // Set IAS Automation Suite76 XmlSuite xmlSuite = new XmlSuite();77 xmlSuite.setName("IAS Automation Suite");78 xmlSuite.setParallel(ParallelMode.TESTS);79 xmlSuite.setThreadCount(1);80 xmlSuite.setVerbose(1);8182 // Add listeners83 84 List<String> listeners = new ArrayList<String>();85 listeners.add("com.ias.util.LoggingListener");86 /*listeners.add("com.surveymonkey.lib.ReportingListner");87 listeners.add("com.surveymonkey.lib.RetryListener");88 //listeners.add("com.surveymonkey.lib.SuiteListener");89 listeners.add("org.uncommons.reportng.HTMLReporter");90 listeners.add("org.uncommons.reportng.JUnitXMLReporter");*/91 xmlSuite.setListeners(listeners);92 9394 String testName = null;95 testName = System.getProperty("Browser");96 97 Map<String,String> parameters = new HashMap<String, String>();98 parameters.put("testName", testName);99100 createTest(xmlSuite, testName,parameters);101 102 createTestNGXmlFile(xmlSuite, "programmedTestNG.xml");103104 // Configure and Run TestNG105 TestNG testNG = new TestNG();106 List<XmlSuite> suites = new ArrayList<XmlSuite>();107 suites.add(xmlSuite);108 testNG.setXmlSuites(suites);109 testNG.run();110 }111 112 /*public static ArrayList<XmlPackage> addPackages(String [] featureNames){113 ArrayList<XmlPackage> packages = new ArrayList<XmlPackage>(); 114 for(String featureName : featureNames){115 XmlPackage xmlPackage = new XmlPackage("com.surveymonkey.testScripts."+featureName.toLowerCase());116 packages.add(xmlPackage);117 }118 XmlPackage xmlPackage = new XmlPackage("com.surveymonkey.tests");119 packages.add(xmlPackage);120 return packages;121 }*/122 123 124 public static XmlSuite createTest(XmlSuite xmlSuite,String testName,Map<String, String> parameters){125 //Set Smoke or Regression Test Suite126 XmlTest xmlTest = new XmlTest(xmlSuite);127 xmlTest.setName(testName);128 //xmlTest.addParameter("testName", testName);129 xmlTest.setPreserveOrder(true);130 xmlTest.setParameters(parameters);131 xmlTest.setSuite(xmlSuite);132 133 134 /*if(featureNames != null && moduleNames != null){135 //if user selects multiple modules under a feature136 List<XmlClass> classes = new ArrayList<XmlClass>();137 classes.addAll(DriverScript.addClasses(feature.toLowerCase(), moduleNames));138 xmlTest.setClasses(classes);139 } else if(featureNames != null){140 //if user selects multiple features141 List<XmlPackage> xmlPackages = new ArrayList<XmlPackage>();142 xmlPackages.addAll(DriverScript.addPackages(featureNames));143 xmlTest.setPackages(xmlPackages);144 } else {145 //if user doesn't select any feature or module then by default add all the test cases146 logger.info("By Default including all packages !!");*/147 List<XmlPackage> xmlPackages = new ArrayList<XmlPackage>();148 XmlPackage xmlPackage = new XmlPackage("com.ias.tests");149 xmlPackages.add(xmlPackage);150 xmlTest.setPackages(xmlPackages);151 152 153 List<XmlTest> tests = new ArrayList<XmlTest>();154 tests.add(xmlTest);155 xmlSuite.setTests(tests);156 157 158 //Group: Smoke or Regression159 XmlGroups group = new XmlGroups();160 XmlRun xmlRun = new XmlRun();161 xmlRun.onInclude(System.getProperty("TestSuite"));162 group.setRun(xmlRun);163 xmlTest.setGroups(group);164 165 return xmlSuite;166 }167 168 public static void createTestNGXmlFile(XmlSuite xmlSuite, String fileName){169 //Generate TestNG file created by programmatically.170 File file = new File(fileName);171 FileWriter writer;172 try {173 writer = new FileWriter(file);174 writer.write(xmlSuite.toXml());175 writer.close();176 logger.info("Generated "+fileName+" file: "+file.getAbsolutePath());177 } catch (IOException e) {178 logger.info("Failed to generate "+fileName+" file");179 e.printStackTrace();180 }181 }182
...