Wednesday 10 July 2013

Send Reports after Selenium webdriver test execution

There might be situations, where you might be tasked by the management people or your clients to send email after your every test execution. Here is a solution for that
In this post we are going to look how we can send email to the clients
or stakeholders after the selenium test execution has been completed.

The program which we need include in Selenium Framework is,
Download here the Mail.jar
Download here the activation.jar
//The jar files which I have used are activation.jar and mail.jar Can be downloaded from Internet.

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

 public class SendMail

{
    //reportFileName = TestExecutionResultFileName
    public static void execute(String reportFileName) throws Exception

    {
        String path=<Report file path>;

        String[] to={"<stakeholder1>","<stakeholder2>"};
        String[] cc={};
        String[] bcc={"<AutomationTester>"};

        SendMail.sendMail("<AutomationTesterUserName>",
                            "<AutomationTesterPassword>",
                            "smtp.gmail.com",
                            "465",
                            "true",
                            "true",
                             true,
                            "javax.net.ssl.SSLSocketFactory",
                            "false",
                             to,
                             cc,
                             bcc,
                            "<Subject line>",
                            "<Contents if any>",
                            path,
                            reportFileName);
      }

      public  static boolean sendMail(String userName,
                String passWord,
                String host,
                String port,
                String starttls,
                String auth,
                boolean debug,
                String socketFactoryClass,
                String fallback,
                String[] to,
                String[] cc,
                String[] bcc,
                String subject,
                String text,
                String attachmentPath,
                String attachmentName){

        //Object Instantiation of a properties file.
        Properties props = new Properties();

        props.put("mail.smtp.user", userName);

        props.put("mail.smtp.host", host);

        if(!"".equals(port)){
        props.put("mail.smtp.port", port);
        }

        if(!"".equals(starttls)){
            props.put("mail.smtp.starttls.enable",starttls);
            props.put("mail.smtp.auth", auth);
        }

        if(debug){

        props.put("mail.smtp.debug", "true");

        }else{

        props.put("mail.smtp.debug", "false");

        }

        if(!"".equals(port)){
            props.put("mail.smtp.socketFactory.port", port);
        }
        if(!"".equals(socketFactoryClass)){
            props.put("mail.smtp.socketFactory.class",socketFactoryClass);
        }
        if(!"".equals(fallback)){
            props.put("mail.smtp.socketFactory.fallback", fallback);
        }

        try{

            Session session = Session.getDefaultInstance(props, null);

            session.setDebug(debug);

            MimeMessage msg = new MimeMessage(session);

            msg.setText(text);

            msg.setSubject(subject);

            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(attachmentName);
            multipart.addBodyPart(messageBodyPart);

            msg.setContent(multipart);
            msg.setFrom(new InternetAddress(userName));

            for(int i=0;i<to.length;i++){
                msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to[i]));
            }

            for(int i=0;i<cc.length;i++){
                msg.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc[i]));
            }

            for(int i=0;i<bcc.length;i++){
                msg.addRecipient(Message.RecipientType.BCC, new
InternetAddress(bcc[i]));
            }

            msg.saveChanges();

            Transport transport = session.getTransport("smtp");

            transport.connect(host, userName, passWord);

            transport.sendMessage(msg, msg.getAllRecipients());

            transport.close();

            return true;

        } catch (Exception mex){
            mex.printStackTrace();
            return false;
        }
    }
}
The above Source code will do the job of triggering email after every execution.
Where to how to use it?
Add the below snippet at the end of the test execution report creation.

SendMail.execute(ExecutionFileName);

Tuesday 2 July 2013

TestNG+Ant+eclipse XSLT Report Generation

XSL stands for Extensible Style sheet Language, and is a style sheet 
Language for XML documents. XSLT stands for XSL Transformations.
 In this tutorial you will learn how to use XSLT to transform XML 
Documents into other formats, like XHTML. XSLT which gives good graphical generated reports.

Steps for generating reports:
2. Configure environment variable for ant 
      User variable
         variable name: ANT_HOME
         variable value: C:\Program Files\Java\apache-ant-1.9.1
     System variable
         variable name:  Path
         variable value:
 C:\Program Files\Java\apache-ant-1.9.1\bin
           Check ANT version
            cmd > ant -version



1. Go to project directory 
2.Go to src folder
3. Create a folder name as "xslt"
4. Download https://github.com/prashanth-sams/testng-xslt-1.1.2
5.Unzip it
6.Go to fallowing directory "testng-xslt-1.1.1\testng-xslt-1.1.1\src\main\resources"

7. Copy the "testng-results.xsl" and keep in "xslt

8. Now copy the saxon.jar file from fallowing path "testng-xslt-1.1.1\testng-xslt-1.1.1\lib".

9. place it your project folder.

10. Modify your build.xml of ant and add the following target to it.

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="TestNG" default="usage" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
    <property environment="env"/>
    <property name="ws.home" value="${basedir}"/>
          <property name="ws.jars" value="F:\1"/>
    <property name="test.dest" value="${ws.home}/build"/>
    <property name="test.src" value="${ws.home}/src"/>
          <property name="ng.result" value="test-output"/>
    
    <!--target name="start-selenium-server">
        <java jar="${ws.home}/lib/selenium-server.jar"/>
    </target-->

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

    <target name="init" depends="setClassPath">
        <tstamp>
            <format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
        </tstamp>
        <condition property="ANT" 
            value="${env.ANT_HOME}/bin/ant.bat" 
            else="${env.ANT_HOME}/bin/ant">
            <os family="windows" />
        </condition>
        <taskdef name="testng" classpath="${test.classpath}"
               classname="org.testng.TestNGAntTask" />
    
    </target>
  
    <!-- all -->
    <target name="all">
    </target>

    <!-- clean -->
    <target name="clean">
        <delete dir="${test.dest}"/>
    </target>

    <!-- compile -->
    <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}" 
            target="1.5" 
            classpath="${test.classpath}"
        >
        </javac>
      </target>

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

    <!-- run -->
    <target name="run" depends="compile">
<testng classpath="${test.classpath}:${test.dest}" suitename="sivaprasad"> 
            <xmlfileset dir="${ws.home}" includes="testng.xml"/>
        </testng>
        <!--
        <testng classpath="${test.classpath}:${test.dest}" groups="fast">
            <classfileset dir="${test.dest}" includes="example1/*.class"/>
        </testng>
        -->
    </target>

    <target name="usage">
        <echo>
            ant run will execute the test
        </echo>
    </target>

          <path id="test.c">
                  <fileset dir="${ws.jars}" includes="*.jar"/>
          </path>
         
            <target name="makexsltreports">
                  <mkdir dir="${ws.home}/XSLT_Reports/output"/>

       <xslt in="${ng.result}/testng-results.xml" style="src/xslt/testng-results.xsl"
                       out="${ws.home}/XSLT_Reports/output/index.html"                classpathref="test.c" processor="SaxonLiaison">
                      <param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
       <param name="testNgXslt.showRuntimeTotals" expression="true"/>
                  </xslt>
              </target>

    <!-- ****************** targets not used ****************** -->
</project>




NOTE: 1. instead of "F:\1" give your own jar files path
                 2. Instead of "testng.xml" replace your xml 


6. Create testng.xml file in your project with the following script for TestNG execution




<?xml version="1.0" encoding="UTF-8"?>                       
<suite name="Ant Suite">                                               
       <test name="SP Kandua">                                      
        <classes>                                                               
          <class name="sample.Xlstreport" ></class>         
        </classes>                                                              
       </test>                                                                    
 </suite>                                                                         


7. Now run from the command prompt

 > Goto your project directory

    
   ant makexsltreports

   We will get results like below image







Monday 1 July 2013

Generating the build.xml file with screenshots


Hello Everyone,

now we are going to learn how to create a build.xml file creation.
1. Right click on project












2.Click on Export option
3.will get one popup window like

4.Click on General Tab













5.Click on Ant Build files.
6.Click on "Next" button.
7.If u want to generate build.xml file for different projects do select on particular created projects.

8.Click on "Finish" button.
9.We accomplished creation of build.xml file.see below image





Friday 28 June 2013

About XSLT

XSL stands for EXtensible Stylesheet Language, and is a style sheet language
 for XML documents.
XSLT stands for XSL Transformations. In this tutorial you will learn how to
use XSLT to transform XML documents into other formats, like XHTML.