Thursday, 24 January 2013

Ant script for generating a Junit report for Selenium Test execution


We know the advantages of using Selenium RC. But Selenium RC by default does not provide us with any reports for the test execution done through it. We need to explicitly implement a reporting framework for test reporting in RC. For implementing a Reporting framework you can write your own custom code or can use an already existing reporting framework available.

There are mainly two reporting framework available for Selenium RC they are Junit and Test-NG. There are two more frameworks Report-NG and testng-xslt but they are implemented over the Test-NG framework.
In this blog I will mention about generating a Junit Report for your Selenium RC test execution using Ant script. I had written a “build.xml” for Ant for executing Selenium cases and generating a Junit report for the said selenium execution. I will be discussing about the same in this blog. If someone doesn’t have an idea about Ant can follow my steps and can generate a Junit report for Selenium Execution.

Explaining the “build.xml”
  1. In Build.xml I am mainly defining 5 properties
o RELEASE_ROOT this has been set to the Java Project folder.
o SRC which defines the source folder of the project
o LIB which defines the lib folder of the project
o BIN which defines the binary folder where the classes needs to be pasted after compiling
o REPORT folder which defines the report folder where the test execution reports will be generated.
  1. There are 4 targets or tasks of ant inside the build.xml
o “init’ which deletes the binary folder and creates it again.
o “compile” which depends upon the “init” task and which compiles the java files under the source folder and paste the classes onto the binary folder.
o “run-single-test” which depends on “compile” and runs only a single Selenium Junit test mentioned in it. This task also generates a Junit html report under the report/html folder of Java project after Selenium Test execution is done.
o “run-batch-test” which depends on “compile” and runs multiple Selenium Junit test mentioned in it. This task also generates a Junit html report under the report/html folder of Java project after Selenium Test execution is done.
  1. In each of the compile, run-single-test and run-batch-test I am setting the classpath of the binary folder, the library folder and the source folder in case of “run-batch-test”.
  2. In targets “run-single-test” and “run-batch-test” I am using the “junit” task of test execution. This task provides two types of execution
o Single class execution which I had used in “run-single-test” target. There is a subtask “test” of “junit” that can used for single test class execution. It is written as follows:

<test name="test.TestClass" haltonfailure="no" todir="${REPORT}/xml" outfile="TEST-result">
<formatter type="xml" />
</test>

Here “name” is the test class name. “todir” is the directory where initial xml report needs to be generated. “outfile” is the xml file name for initial result xml file. “formatter” is the initial file format type.

o Batch test class execution which I had used in “run-batch-test” target. There is a subtask “batchtest” of “junit” that can used for single test class execution. It is written as follows:

<formatter type="xml" usefile="true" />
<batchtest fork="yes" todir="${REPORT}/xml">
<fileset dir="${SRC}">
<include name="**/Test*.java" />
</fileset>
</batchtest>

Here I am telling the “batchtest” what all test classes to include using the “fileset” and “include” task. Along with this if you want you can use the “exclude” task to exclude particular type of classes. In the following example the Junit will include all the test class starting from “Test” and exclude all the test class having “Base” word in their name.

<formatter type="xml" usefile="true" />
<batchtest fork="yes" todir="${REPORT}/xml">
<fileset dir="${SRC}">
<include name="**/Test*.java" />
<exclude name="**/*Base*.java" />
</fileset>
</batchtest>

5. For generating the test report I am using the “junitreport” task of ant. It takes all the xml file from the “report/xml” and generates an html report at “report/html”.

Setting up a Java Project for test execution using Ant.

In this I am just telling how to setup a basic java project for using the build.xml file attached with this blog and running the selenium test using Ant.

Preconditions:
1. Install Ant from apache.
2. Install Java JDK (not JRE) and add its bin folder to your “PATH” environment variable of your system. For checking JDK is mapped check whether "javac" is recognized in your system by typing "javac" in your command prompt.
3. Download the Selenium RC.
4. Test cases are already converted to Selenium Junit cases.

Steps:
  1. Create a Java project in your Java IDE. I am using Eclipse so I will refer to it.
  2. By default there will be the “src” folder inside your Java Project. Right click on the “src” folder and create a new package under it say”test”.
  3. Now copy your Selenium Junit classes to this new “test” package.
  4. Create a new folder called “lib” under your Java Project.
  5. Copy selenium-java-client-driver.jar, junit-4.5.jar(if you have another version of Junit 4 you can use that) and ant-junit.jar(can be found under the “lib” folder of your ant installation) to your “lib” folder of Java Project.
  6. Now create a folder called “build” under your Java Project and copy the “build.xml” file attached with this blog to the build folder you created.
  7. Now open the build.xml file for edit.
  8. Search for “run-single-test” in build.xml. Go to the “<test” under the “run-single-test” target .Replace the values for “name=” variable with your Selenium Junit class name. For ex if your class name is TestSelenium.java and it is under package “test” under “src” then replace the value with “test.TestSelenium” then save the build.xml file.
  9. Now start the Selenium RC server.
  10. Now go to the build folder of the Java Project using command prompt and run the command “ant run-single-test”
  11. It should execute your Selenium cases on selenium server and generate an html report onto the “report/html” folder of your Java project.

Similarly you can modify the “run-batch-test” target of build.xml and execute multiple Selenium Tests together.

No comments:

Post a Comment