Pages

Wednesday, April 26, 2017

Manage the execution of Test classes/methods at run time-Selenium/TestNG Automation framework

Requirement :How to manage the Test Methods to be included in run time once the Suite Execution started?(Selenium/testNG Framework )Solution : TestNG provides a listener Interface "IAlterSuiteListener" which have method"alter" which have access of currently runnign suite files as methods parameters, by implementing alter method we can manage the testng suite file for current execution public void alter(List<XmlSuite> suites)Code Example:1.Sample Test Suite Here is the Sample file of TestNG Suite <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Sample Test Suite"><test name="DemoTestExecution"><parameter name="browserType" value="firefox"></parameter><!-- browser values can be either firefox, ie or Chrome--><parameter name="url" value="https://www.google.co.in/"></parameter><parameter name="seleniumHost" value="localhost" /><parameter name="seleniumPort" value="4444" /><parameter name="isRunOnRemote" value="false" /><classes><class name="com.sample.tests.LoginTest"></class></classes></test></suite>2. create a excel sheet with list of Test classes and with Execution Flag

ModuleName

Execution_Flag

flipkartLogin

y

flipKartTrackOrder

y

flipKartSearchHelp

n

3. Make sure we have a TestClass files in the same name provided in excel sheet Column "ModuleName"4. Using Apache POI read the excel sheet , create List , which have the list of module names which set execution flag as y List<String> testClasses;4. here is the code block to manage suite xml

//Define a Path where Test classes are saved String parantPath = "com.sample.tests"; List<String> testClasses; //This is the result of excel sheet reader//Fetch the Currently Running Test from current Suite List<XmlTest> currentTests=mySuite.getTests();//Fetch the first <Test > which is a XMlTest Object XmlTest myTest = currentTests.get(0);for(String newClass :testClasses){ //Create a Object of XmlClass; XmlClass classObj;try {//Instantiate the XMlClass Object with full class path of newly added Class classObj = new XmlClass(ClassLoader.getSystemClassLoader().loadClass(parantPath+"."+newClass));//Add the class in to XmlTest ObjectmyTest.getClasses().add(classObj);//Add the XmlTest in to XmlSuite ObjectmySuite.setTests(currentTests);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} } }}5. add this listener in to the actual testng.xml file <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Sample Test Suite"> <listeners> <listener class-name="com.sample.listeners.InjectSuiteListener"/> <listeners><test name="DemoTestExecution"><parameter name="browserType" value="firefox"></parameter><!-- browser values can be either firefox, ie or Chrome--><parameter name="url" value="https://www.google.co.in/"></parameter><parameter name="seleniumHost" value="localhost" /><parameter name="seleniumPort" value="4444" /><parameter name="isRunOnRemote" value="false" /><classes><class name="com.sample.tests.LoginTest"></class></classes></test></suite>6. Now the two Test cases which have Execution_Flag as Y , gets included in current suit and will execute the Test Methods inside the class. 7. The Same way , individual Test Methods also can be managed.List<String> testmethodForEachClass;//For the selected Class , list of methods testmethodForEachClass=TestDataReader.getTestMethodList(className);