Automation test reports are one of the important deliverable of Automation executions. Getting those automation reports in the mail automatically is the another important concept. Here, I would like to share the information about how to send your automation reports as mail report with attachment while automation of your application with Java or C# languages.
Send automation test reports with Java language:
Prerequisites:
Need to add javax.mail dependency in pom.xml of your automation project (if it is a Maven project. Otherwise need to add javax.mail jar in your Java Build Path of your Java project). Following is the javax.mail dependency,
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
Following sendMailReport() method helps to send test automation reports with attachment,
public static void sendMailReport() throws AddressException, MessagingException { String subject = "", message = "", maillist = "", EMAIL_REPORT_FOLDER = ""; subject = "Automation Test Report_"; message = "Please find the attached report to get details."; maillist = "mail_lists_of_recipients"; EMAIL_REPORT_FOLDER = "path_to_automation_report_folder"; Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); final String userName; final String password; userName = "sender_mail_address"; password = "sender_mail_account_password"; properties.put("mail.user", userName); properties.put("mail.password", password); Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }; Session session = Session.getInstance(properties, auth); DateFormat dff = new SimpleDateFormat("EEE MMM dd, yyyy HH:mm:ss z"); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(maillist)); msg.setSubject(subject + " - " + dff.format(new Date()).toString()); msg.setSentDate(new Date()); // creates message part MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(message, "text/html"); // creates multi-part Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // adds attachments List<String> filesList = new ArrayList<String>(); File[] files = new File(System.getProperty("user.dir") + EMAIL_REPORT_FOLDER).listFiles(); for (File file : files) { if (file.isFile()) { filesList.add(System.getProperty("user.dir") + EMAIL_REPORT_FOLDER + "/" + file.getName()); } } if (filesList != null && filesList.size() > 0) { for (String filePath : filesList) { MimeBodyPart attachPart = new MimeBodyPart(); try { attachPart.attachFile(filePath); } catch (IOException ex) { ex.printStackTrace(); } multipart.addBodyPart(attachPart); } } // sets the multi-part as e-mail's content msg.setContent(multipart); // sends the e-mail Transport.send(msg); }
Send automation test reports with C# language:
The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery. Following Send_Report_In_Mail() method helps to send test automation reports with attachment,
public void Send_Report_In_Mail() { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("sender_mail_address"); mail.To.Add("mail_lists_of_recipients"); StringBuilder TimeAndDate = new StringBuilder(DateTime.Now.ToString()); TimeAndDate.Replace("/", "_"); TimeAndDate.Replace(":", "_"); mail.Subject = "Automation Test Report_"+ TimeAndDate; mail.Body = "Please find the attached report to get details."; string actualPath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "\\Test_Execution_Reports"); //Reports should stored in Test_Execution_Reports folder var mostRecentlyModified = Directory.GetFiles(actualPath, "*.html") .Select(f => new FileInfo(f)) .OrderByDescending(fi => fi.LastWriteTime) .First() .FullName; Attachment attachment; attachment = new Attachment(mostRecentlyModified); mail.Attachments.Add(attachment); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("sender_mail_address", "sender_mail_account_password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); }
You can use those method in @AfterSuite if you are using TestNG. In case of NUnit(C#), you can use those method in [OneTimeTearDown]. Try to use above code snippet in your automation scripts to send your automation test report as mail report automatically.
make it perfect !
Leave a Reply