Thursday, May 13, 2010

Generating selenium reports using TestNG-xslt through Ant

TestNG-xslt generates user friendly reports using the TestNG results output (testng-results.xml). Its uses the pure XSL for report generation and Saxon as an XSL2.0 implementation.

Most of the material is taken from the original site.
 http://code.google.com/p/testng-xslt/


Alternative link to download. Please open the said link and download it by going to File -> Download option.

I will tell in this blog how to implement this report for your project. This implementation will tell you how to generate the testng-xslt report using ant. If your current project does not use ant build then you can use ant only for the report generation purpose.

If you dont know ant please check the Apache ant website http://ant.apache.org/.

For generating testng-xslt report for your project do the following:
1. Download the testng-xslt
2. Unzip and copy the testng-results.xsl from the testng-xslt folder(testng-xslt-1.1\src\main\resources) to your own project folder.
3. Now copy the saxon library from (testng-xslt-1.1\lib\saxon-8.7.jar)to your project lib folder.
4. Modify your build.xml of ant and add the following target to it.


<project name="test" basedir=".">
    <property name="LIB" value="${basedir}/libs" />
    <property name="BIN" value="${basedir}/bin" />
    <path id="master-classpath">
        <pathelement location="${BIN}" />
        <fileset dir="${LIB}">
            <include name="**/*.jar" />
        </fileset>
    </path>
    
    <target name="testng-xslt-report">
        <delete dir="${basedir}/testng-xslt">
        </delete>
        <mkdir dir="${basedir}/testng-xslt">
        </mkdir>
        <xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html">
            <param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />

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

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

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

            <classpath refid="master-classpath">
            </classpath>
        </xslt>
    </target>
</project>


The XSL transformation can be configured using the parameters described below.

  • testNgXslt.outputDir - Sets the target output directory for the HTML content. This is mandatory and must be an absolute path. If you are using the Maven plugin this is set automatically so you don't have to provide it.
  • testNgXslt.cssFile - Specifies and alternative style sheet file overriding the default settings. This parameter is not required.
  • testNgXslt.showRuntimeTotals - Boolean flag indicating if the report should display the aggregated information about the methods durations. The information is displayed for each test case and aggregated for the whole suite. Non-mandatory parameter, defaults to false.
  • testNgXslt.reportTitle - Use this setting to specify a title for your HTML reports. This is not a mandatory parameter and defaults to "TestNG Results".
  • testNgXslt.sortTestCaseLinks - Indicates whether the test case links (buttons) in the left frame should be sorted alphabetically. By default they are rendered in the order they are generated by TestNG so you should set this to true to change this behavior.
  • testNgXslt.chartScaleFactor - A scale factor for the SVG pie chart in case you want it larger or smaller. Defaults to 1.
  • testNgXslt.testDetailsFilter - Specified the default settings for the checkbox filters at the top of the test details page. Can be any combination (comma-separated) of: FAIL,PASS,SKIP,CONF,BY_CLASS
You need to provide the testng-xslt stylesheet the TestNG results xml(testng-results.xml) , the path to the style sheet testng-results.xsl and the output index.html path.

Also dont forget to add the saxon library to your target classpath else you will get an error. In my case it is the master-classpath.

Noe run the ant target for report generation (in my case "testng-xslt-report
") and check the ouput folder configured by you for testng-xslt report.

Friday, May 7, 2010

Using TestNG for parallel execution on Selenium Grid

Following I will mention how to integrate TestNG with Selenium-Grid code and use it for parallel execution of test cases.
For this you create a Base class where you define the following two functions.


protected
void startSession(String seleniumHost, int seleniumPort, String browser, String webSite)
throws Exception {


startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);

session().setTimeout(TIMEOUT);

}

protected void closeSession() throws Exception {

closeSeleniumSession();

}

The above two functions are for the starting and closing the session in Selenium Grid. The references of it can be found in my earlier posts.

Now import the testng annotation class “org.testng.annotations.*” onto the Base Class.

Add @BeforeClass before the “startSession” method and @AfterClass before the “closeSession” method.

Now write your test cases in a way that all dependent test cases are in one class.


So whenever you execute the testNG it will call the “startSession” and “closeSession” before and after each test class execution. The “startSession” will create a Selenium session for test execution and return it to the test class. Where as the “closeSession” will close the session.

For parallel execution define inside the testng.xml file the attributes “suite” tag as “parallel="classes" thread-count="2" ”.
This will tell TestNG that parallel execution needs to be done for classes and max. Thread count for it is “2”.

Define 2 different test classes both extending the Base Class which defines the “startSession” and “closeSession” methods. Start a Selenium Grid Hub and register two Selenium remote control to the Hub. Now try running the package containing these classes using the TestNG.

You will see 2 different tests running in parallel in Selenium Grid each for Selenium Remote Control registered to the Hub.
The “startSeleniumSession” inside the “startSession” method create a unique selenium session for each of the test class and use that session for test class execution. Creating a session,registering the session with a Remote control and maintaining the session is done by Selenium Grid where as using TestNG framework we define how many sessions we need to create and how the test execution should be. So using the features of both TestNG and Selenium Grid we achieve Parallel execution of our automation testcases.

Currently here I had suggested to use @Before/AfterClass annotations in Base Class. You can use any of the other available @Before/After annotations available in TestNG depending upon your tests.