Selenium Junit Suite




To know basic understanding of Selenium please see http://sudhiexperience.blogspot.in/2012/07/hello-world-selenium.html

First question What is Junit suite and Why do you need it?


  1. Suite is just a class which acts like trigger point and allows you to configure what test classes needs to be run. 
  2. Suite is required if u want to configure which testcase classes will run, or if u want to run the testccases in order or if u want to single triggering point to execute all testcases.


lets see an example,

we have 2 testcase classes LoginTest.java and SearchTest.java, then i will create Main suite like below 



 package sudhi.selenium;  
 import org.junit.runner.RunWith;  
 import org.junit.runners.Suite;  
 @RunWith(Suite.class)  
 @Suite.SuiteClasses(  
           LoginTest.class,  
           SearchTest.class  
           )  
 public class MainSuite {  

        //All BeforeClass, Before, After, AfterClass methods will hold good here.

 }  

In above example LoginTest will be executed first and then SearchTest. Hence it keeps the order of testcase execution. You can right click and execute as Junit on MainSuite class, which inturn execute all test classes defined in the Suite.SuiteClass annotations.


Next example shows how to configure MainSuite to read test classes from configuration file rather than hard coding in Suite.SuiteClasses annotations.

Custom Suite Class


Below example shows how to write custom class to read which testcase classes needs to be run from properties file. This u need if non-java people want to configure test cases to be executed at run time.

For example i have written custom SeleniumSuite class, which needs to be used in MainSuite

  
 package sudhi.selenium;  
 import org.junit.runner.RunWith;  
 @RunWith(SeleniumSuite.class)  
 public class MainSuite {  
 }  


In above class u can see the difference, instead of @RunWith (Suite.class) i have used @RunWith(SeleniumSuite.class)

And here is source code for SeleniumSuite.java




 package sudhi.selenium;  
 import java.io.BufferedReader;  
 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileReader;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 import java.util.List;  
 import org.junit.runner.Description;  
 import org.junit.runner.Runner;  
 import org.junit.runner.notification.RunNotifier;  
 import org.junit.runners.ParentRunner;  
 import org.junit.runners.model.InitializationError;  
 import org.junit.runners.model.RunnerBuilder;  

 public class SeleniumSuite extends ParentRunner<Runner> {  
      private static Class<?>[] getAnnotatedClasses(Class<?> klass) throws InitializationError{  
           File file = new File("");  
           List<Class> classes = new ArrayList<Class>();  
           FileReader reader = null;  
           BufferedReader bufferedReader = null;  
           try {  
                reader = new FileReader("TestClasses.properties"); //path of the properties file  
                bufferedReader = new BufferedReader(reader);  
                String line = "";  
                while((line=bufferedReader.readLine())!=null){  
                     String[] values = line.split("=");  
                     String className = values[0].trim();  
                     if(Boolean.valueOf(values[1].trim())){  
                          try {  
                               Class foundClass = Class.forName(className);  
                               classes.add(foundClass);  
                               System.out.println(foundClass);  
                          } catch (ClassNotFoundException e) {  
                               System.out.println("Class not found: "+className);  
                          }  
                     }  
                }  
           } catch (FileNotFoundException e) {  
                throw new InitializationError("TestClasses.properties File not found\n"+e.getMessage());  
           } catch (IOException e) {  
                throw new InitializationError("Error while trying to access TestClasses.properties File\n"+e.getMessage());  
           }finally{  
                try {  
                     if(reader!=null){  
                          reader.close();  
                     }  
                     if(bufferedReader!=null){  
                          bufferedReader.close();  
                     }  
                } catch (IOException e) {  
                }  
           }  
           Class[] testCaseList = new Class[classes.size()];  
           for(int i=0;i<classes.size(); i++){  
                testCaseList[i] = classes.get(i);  
           }  
           return testCaseList;  
      }  
      private final List<Runner> fRunners;  
        
      public SeleniumSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {  
           this(builder, klass, getAnnotatedClasses(klass));  
      }  
        
      protected SeleniumSuite(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {  
           this(klass, builder.runners(klass, suiteClasses));  
      }  
        
      protected SeleniumSuite(Class<?> klass, List<Runner> runners) throws InitializationError {  
           super(klass);  
           fRunners = runners;  
      }  
      @Override  
      protected List<Runner> getChildren() {  
           return fRunners;  
      }  
      @Override  
      protected Description describeChild(Runner child) {  
           return child.getDescription();  
      }  
      @Override  
      protected void runChild(Runner runner, final RunNotifier notifier) {  
           runner.run(notifier);  
      }  
 }  



And properties file is shown below
TestClasses.properties



 sudhi.selenium.LoginTest=true  
 sudhi.selenium.SearchTest=false  


So when we run MainSuite, SeleniumSuite will pick up only LoginTest to run since SearchTest is set to false.



Comments