Pages

Sunday, June 21, 2015

build.xml+TESTNG+XSLT for selenium webdriver execution and xslt report


Note:-

Confirm Required Jar Files Added In Project's Build Path
Confirm that bellow given jar files are Included In your project's build path or not. You need bellow given files In your project's build path to generate XSLT reports.
  1. saxon-8.7.jar
  2. SaxonLiaison.jar
Confirm "testng-results.xsl" Is Added In Your Project

testng-6.8.5.jar should be configured to build.xml..

see below build.xml for testng-6.8.5.jar configuration









<?xml version="1.0" encoding="UTF-8"?>
<project name="projectName" default="clean" basedir=".">
<!-- dot indicates that basedir is pointing towards project root directory -->

<!-- ========== Initialize Properties =================================== -->
<property name="ws.home" value="${basedir}" />
<property name="ws.jars" value="C:/Users/bhanusi.ORADEV/workspace/RND/src/ri/JARS" />
<property name="test.dest" value="${ws.home}/build" />
<property name="test.src" value="${ws.home}" />
<property name="ng.result" value="test-output" />

<echo> value of base dir = ${basedir} </echo>

<!-- properties for copying the results -->
<tstamp>
<format property="year" pattern="yyyy" />
<format property="DSTAMP" pattern="yyyy-MM-dd" />
<format property="TSTAMP" pattern="HH:mm:ss" />
<format property="dateversion" pattern="yyyy.MM.dd.HH:mm:ss" />
<format property="time.stamp" pattern="yyyy-MM-dd_HH-mm-ss aa_" />
</tstamp>

<property name="testng.report.dir" value="${ws.home}/testngReports/${time.stamp}" />
<property name="testngXslt.report.dir" value="${ws.home}/testngXsltReports/${time.stamp}" />

<!-- ====== For setting the classpath ==== -->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.jars}" includes="*.jar" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>

<!-- ============ Initializing other stuff =========== -->
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM-dd-yyyy (HH-mm-ss)" />
</tstamp>
<condition property="ANT" value="$(env.ANT_HOME)/bin/ant.bat" else="$(env.ANT_HOME)/bin/ant">
<!-- os family="windows" /-->
<os family="mac" />
</condition>
<!-- download test NG jar file and provide path of test NG  -->
<taskdef name="testng" classpath="C:/Users/bhanusi.ORADEV/workspace/RND/src/ri/JARS/testng-6.8.5.jar" classname="org.testng.TestNGAntTask" />
</target>

<target name="all">
</target>

<!-- cleaning the destination folders -->
<target name="clean">
<echo message="deleting existing build directory" />
<delete dir="${test.dest}" />
</target>

<!-- target for compiling the java files -->
<target name="compile" depends="init, clean">
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*" />
</delete>
<echo message="making directory....." />
<mkdir dir="${test.dest}" />
<echo message="classpath-------: ${test.classpath}" />
<echo message="compiling....." />
<javac debug="true" destdir="${test.dest}" srcdir="${test.src}" classpath="${test.classpath}" includeantruntime="true" />
</target>

<!-- build -->
<target name="build" depends="init">
</target>

<!-- ========== Test executions & Generating reports using Testng utility for multiple suites ============== -->
<!-- run -->
<target name="run" depends="compile">
<!--suite begin -->
<testng classpath="${test.classpath}:${test.dest}" suitename="RiRegression" outputDir="test-output">
<!-- write the testNG suite file name if build.xml and testNG suite file at same level -->
<xmlfileset dir="." includes="testSuite.xml" />
</testng>

</target>

<target name="testngReportCopyAndReportParser">
<!-- Copy to TestNG report directory-->
<mkdir dir="${testngDir}">
</mkdir>
<!-- to copy previous suite result to TestNG report directory -->
<copy todir="${testngDir}">
<fileset dir="test-output" />
</copy>
<!-- end of Testng Report -->
</target>


<!--  This section will generate xslt report   -->
<!--   add  SaxonLiaison.jar and saxon-8.7.jar to the build path of project-->
<!-- For XSLT report add  "testng-results.xsl file to your project"-->
<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>

<property name="LIB" value="${basedir}/JARS" />
<property name="BIN" value="${basedir}/bin" />
<path id="master-classpath">
<pathelement location="${BIN}" />
<fileset dir="${LIB}">
<include name="**/*.jar" />
</fileset>
</path>

</project>







How to make custom listener class for selenium webdriver. This listener class will attach screen shot to the test NG report when ever there is failure













package ri.customListener;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import ri.testBase.TestBase;


public class listener extends TestBase implements ITestListener {
private long time;

@Override
public void onFinish(ITestContext itr) {
// TODO Auto-generated method stub
long time = System.currentTimeMillis();
Reporter.log(itr.getClass() + "--test finished");
}

@Override
public void onStart(ITestContext itr) {
// TODO Auto-generated method stub
Reporter.log(itr.getClass() + "--test started");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult itr) {
// TODO Auto-generated method stub

}

@Override
public void onTestFailure(ITestResult result) {
if (!result.isSuccess()) {
String failureImageFileName = new SimpleDateFormat(
"MM-dd-yyyy_HH-ss").format(new GregorianCalendar()
.getTime())
+ ".png";
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File(failureImageFileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String userDirector = System.getProperty("user.dir") + "/";
Reporter.log("<a href=\"" + userDirector + failureImageFileName
+ "\"><img src=\"file:///" + userDirector
+ failureImageFileName + "\" alt=\"\""
+ "height='100' width='100'/> " + "<br />");
Reporter.setCurrentTestResult(null);

}
}

@Override
public void onTestSkipped(ITestResult itr) {
// TODO Auto-generated method stub

}

@Override
public void onTestStart(ITestResult itr) {
// TODO Auto-generated method stub

}

@Override
public void onTestSuccess(ITestResult itr) {
// TODO Auto-generated method stub

}

}





Below is Test NG suit file configuration



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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test suite">
<!-- Default suite -->
<listeners>
<listener class-name="ri.customListener.listener" />
</listeners>
<test name="Regression test" verbose="2">
<classes>
<class name="ri.testCases.LoginTest" />
</classes>
</test>
<!-- Default test -->
</suite>