10b. Advanced WebDriver – Generating JUnit Report

Hiya champs! Reports, reports, reports everywhere. We have been dealing with these from the age of Harry Potter!! (Remember progress reports from school?!) Anyways, reports are really important especially in testing to get a quick understanding of how everything is working.

So today, we are going to use junitreport task of Apache ANT to generate one. Prepare to be lit up!

From my previous post, we have generated the buildfile with ‘junit’ as the default JUnit output directory. In plain English, it just means that all the files generated as part of the JUnit report task will be placed under a directory named ‘junit’.

Step 1:

Under the project ‘Selenium’, ANT buildfile named ‘build.xml’ is generated. I have created two JUnit test cases, RadioBtns_Checkboxes.java and SelectItems.java, under a package ‘com.blog.junitTests’ as shown in the below image. The code is available in the respective posts. Just click on the file names to get yourself navigated.

Package structure

Step 2:

Open the ‘build.xml’ and make sure ‘junitreport’ task is auto-generated. Below are the targets (init, clean, build, RadioBtns_Checkboxes, SelectItems and junitreport) for my project.

<project basedir="." default="build" name="Selenium">
    <property environment="env"/>
    <property name="junit.output.dir" value="junit"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.8"/>
    <property name="source" value="1.8"/>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
  
    <target name="clean">
        <delete dir="bin"/>
    </target>
  
    <target name="build" depends="init">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="Selenium.classpath"/>
        </javac>
    </target>
    
    <target name="RadioBtns_Checkboxes">
        <mkdir dir="${junit.output.dir}"/>
        <junit fork="yes" printsummary="withOutAndErr">
            <formatter type="xml"/>
            <test name="com.blog.junitTests.RadioBtns_Checkboxes" todir="${junit.output.dir}"/>
            <classpath refid="Selenium.classpath"/>
        </junit>
    </target>
    <target name="SelectItems">
        <mkdir dir="${junit.output.dir}"/>
        <junit fork="yes" printsummary="withOutAndErr">
            <formatter type="xml"/>
            <test name="com.blog.junitTests.SelectItems" todir="${junit.output.dir}"/>
            <classpath refid="Selenium.classpath"/>
        </junit>
    </target>
   
    <target name="junitreport">
        <junitreport todir="${junit.output.dir}">
            <fileset dir="${junit.output.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${junit.output.dir}"/>
        </junitreport>
    </target>
</project>

Step 3:

Build can be executed in any of the following ways,

1.Right click on the buildfile (build.xml) -> Run As -> Ant Build

Report Generation Method 1

2. Right click on the buildfile (build.xml) -> Run As -> External Tools Configuration -> Run

3. Shortcut Run icon in the eclipse toolbar

Report Generation Method 2

Let us take a stab at the second method and see how to modify the existing configuration using ‘External Tools Configuration’ option.

So right click on the buildfile -> Run As -> External Tools Configurations: This will open a popup. Select the ‘Main’ tab and make sure that the correct buildfile is chosen.

Main Configuration

Then click on ‘Targets’ tab.

  1. All the target names as defined in the buildfile will be listed and only the default target will be checked. Select all the targets that you wish to execute on clicking ‘Run’.
  2. In the ‘Target execution order’ box, all the selected targets from the above step will be listed in the order that they will be executed.
  3. If you wish to change the order then click on ‘Order…’. This will open ‘Order Targets’ popup.
  4. We can specify the target execution order in this popup by selecting the target and clicking on ‘Up’ or ‘Down’ buttons accordingly. Click ‘OK’ to confirm the final execution order.
  5. Once everything is finalized, click on ‘Apply’ and ‘Run’

ANT target configuration

Step 4:

Successful execution can be verified by looking at the console view. It shows all the targets executed in the configured order along with logs based on the logging level mentioned in the buildfile. Total time taken to run the build along with a message ‘BUILD SUCCESSFUL’ or ‘BUILD FAILED’ will be displayed as shown.

Console Output

Step 5:

In the ‘Package Explorer’ view of the eclipse, right click on the project and click ‘Refresh’ or F5. The ‘junit’ folder (the name given in ‘JUnit output directory’ box while generating the build file) would have been created. This is where the JUnit Report (index.html) will be available along with an XML for every test case that is executed showing its success or failure.

Report in JUnit folder

Alternatively, navigate to the project path in the file explorer and double click on the ‘junit’ folder.

Step 6:

Open ‘index.html’ in the browser and check the default report generated. This shows the packages involved and all the java files that were executed in the left pane.

The right pane shows a summary of the test results which includes the total number of tests, failures, errors, skipped, success rate and the execution time. A list of packages is also shown with similar details.

For an in-depth view of all the tests executed or failures etc., click on the displayed numbers as shown in the below image,

Generated report

Isn’t generating a JUnit report with Apache ANT a cakewalk? Guess that’s all that we have for today.

See you again in another post. Happy Reporting!

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.