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

4 comments:

  1. hi

    what should be added in Execution file name

    ReplyDelete
    Replies
    1. it is just file name of your attaching document that will be seen in the mail as a download file name.

      Delete
  2. Great Job Thank You Very Much

    ReplyDelete