i

Selenium Step By Step Guide

Configuring ANT, Generating TestNG & XSLT Reports

Configuring 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.

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:

<project name="TestAutomation" basedir=".">

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

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

    <path id="master-classpath">

        <pathelement location="${BIN}" />

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

    </path>

    

    <target name="generateReport">

        <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>

TestNG Reports:

As we have discussed that the TestNG is one of the most used automation testing frameworks which works on a range of annotations. Let us see a sample TestNG class that generates the test reports once the TestNG suite completes the execution.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class TestNGParameterization {

                WebDriver driver;

                @BeforeMethod

                void setup()

                {

                                driver= new ChromeDriver();

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

                }

                @Test(priority=2)

                void test1()

                {

                }

 

                @Test(priority=1)

                void test2()

                {

                }

 

                @Test(priority=0)

                void test3()

                {

                }

                @Parameters({"username","password"})

                @Test

                void parameterized(String username, String password) throws InterruptedException

                {

                                Thread.sleep(3000);

                                driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input")).sendKeys(username);

        driver.findElement(By.xpath("//*[@id='admin_login']/div[2]/input")).sendKeys(password);

       

                }

                @AfterMethod

                void cleanup()

                {

                                driver.quit();

                }

 

}

Let us see the “testing.xml” file to run the tests from xml file:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" >

  <test name="Test">

  <parameter name="username" value="admin"/>

  <parameter name="password" value="adminpassword"/>

    <classes>

      <class name="SeleniumTest.SeleniumTest.TestNGParameterization"/>

    </classes>

  </test> <!-- Test -->

</suite> <!-- Suite -->

 

Once we run the above sample TestNG class, then we get an output folder for TestNG results under the project directory, as shown below:

 

Click on the “emailable-report.html” to see the test result report of TestNG:

 

 

XSLT Reports:

Let us know how the combination of Ant and TestNG helps us to give the XSLT reports.

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 data 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:

<project name="TestAutomation" basedir=".">

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

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

    <path id="master-classpath">

        <pathelement location="${BIN}" />

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

    </path>

    

    <target name="generateReport">

        <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>

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.