i

Selenium Step By Step Guide

TestNG, Ant & Report Generation through XSLT

TestNG

  • TestNG is an automation testing framework based on Java. Junit inspires TestNG (Test Next Generation) as it uses annotations as well.
  • TestNG helps in generating proper reports, which gives a close idea to analyze the failed, passed, and skipped test cases.
  • TestNG helps in executing the failed test cases efficiently. If we have five tests to run and after running the test cases suppose four tests got passed, but one got failed, then by using TestNG, we can run only the failed test case instead of rerunning the entire suite.
  • After running the test, we see a testng-failed.xml, which we can run to execute the only failed test case.
  • We can do parallel testing as well as using TestNG.
  • TestNG has a rich set of annotations to create a reliable testing framework.
  • We can group multiple numbers of test cases in TestNG easily.
  • A single test can be run multiple times by using the "invocation count" keyword.
  • The integration of the TestNG framework is effortless with Maven, Jenkins, and other tools.

Ant

Ant is a build tool that helps in taking care of third-party APIs, Classpath of the APIs, the Cleaning process of the previous executable binary file, Compiling, and execution of source code.

Ant helps us in the creation, execution, and automation of the entire process, as described in its configuration file build.xml.

Let’s install Ant:

Below are the steps to install Ant into your machine.

  1. Open, http://ant.apache.org/bindownload.cgi and download .zip file from apache-ant-1.9.14-bin.zip

  1. Now Unzip the zipped folder.

  1. Go to Start>Computer> Right-click and select properties and finally select Advanced System Settings> Click on Environment variables.
  2. User Variables> Click on New> Set the variable name as "ANT_HOME" and set variable value with the path of the Ant root folder without including bin.

 

  1. Now select the "Path" variable under "System Variable" and add the Ant root files path till bin folder after a semicolon ";"

;C:\Users\apache-ant-1.9.14\bin

Build.xml is an essential file for Ant-based build configurations for cleaning, compiling, and execution of the code.

Sample Build.xml file:

XSLT Reporting

Reporting is an essential part of a robust Selenium framework, and TestNG provides necessary HTML reports by default. For enhancing the report, we can use XSLT(Extensible Stylesheet Language Transformation). XSLT transforms the XML documents into other useful XML documents, such as XHTML, which are used by the browser.

We can utilize TestNG, Ant, and XSLT to create amazing HTML reports for our Selenium Test suite. Let us see how to form an XSLT reporting into our Selenium code with the help of the TestNG and Ant build tool. Let us see the steps to create the XSLT report.

  1. Create a TestNG project.
  2. Install the Ant.
  3. Copy XSLT files into your Project.
  4. Run the TestNG Project.
  5. Now run the build.xml

Create a TestNG Project:

Let’s see the sample code of a TestNG project:

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

public class XSLTReportingAntTestNG  {

                WebDriver driver ;          

                @BeforeMethod

                public void startDriver()

                {

        driver=new ChromeDriver();

        this.driver.get("https://selflearning.io/");   

}

  @Test

  public void passTest() {   

                  Assert.assertTrue(true);              

  } 

  @Test(priority=0)

  public void failTest()

  {

                  Assert.assertTrue(false);             

  }

}

Install the Ant:

We have seen earlier how to install the Ant. We need this to be installed to create the build for generating the XSLT report. We will use the build.xml file of Ant to generate the report.

Copy XSLT files into your Project:

I have put on my google drive link here. You can download the files from the drive and paste all the files to your project folder.

Now Run the TestNG Project:

Let us now run the TestNG test from eclipse:

 

Run the build.xml

Now we need to run the build.xml file. A sample of build.xml looks like this:

name="TestAutomation" basedir=".">

    name="LIB" value="${basedir}/lib" />

    name="BIN" value="${basedir}/bin" />

    id="master-classpath">

        location="${BIN}" />

        dir="${LIB}" includes="*.jar"/>

   

    name="generateReport">

        dir="${basedir}/testng-xslt">

       

        dir="${basedir}/testng-xslt">

       

        in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html">

            expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />

            expression="true" name="testNgXslt.sortTestCaseLinks" />

            expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />

            expression="true" name="testNgXslt.showRuntimeTotals" />

            refid="master-classpath">

 Now run the build.xml as “Ant Build”.

Once build gets successful, it will generate the output files under “testing-XSLT" folder for the XSLT reports as shown below:

 

Click in the index.html file in the "testing-XSLT" folder. XSLT report gets displayed in the web browser.