E-mail Distribution of Test Results Obtained from Selenium + TestNG

E-mail Distribution of Test Results Obtained from Selenium + TestNG
February 24 12:20 2011 Print This Article

If you have to run a bunch of automated tests using Selenium + TestNG, the results of running the tests can be easily sent to all interested parties automatically.

It is very convenient in terms of informing participants about the current status of the project’s main functionality. You can also make a small package of automated tests for the product version of your project and run it several times a day. In this case, you will learn about problems encountered one of the first and not get angry letter from a customer.

Test Results

It will be sufficient to simply view the reports in your email client. Running of the automated tests can be configured using Scheduler. This I will explain later.

The results run the automated tests are stored in the file /test-output/emailable-report.html. This report is designed specifically for e-mailing to all parties involved.

Also the report can include screenshots of all failed tests. In my opinion, a visual representation of the result makes it easier to understand the reason of test failure. Opening the screenshot, you can immediately see whether it is a software bug or is it error in test data.

The screenshots in this example, during the run automated tests are placed in the folder

/ test-output/screenshots and have a pretty informative name.

For example, DailyPack.Administrator_Department.Admin_Department_5_Clients_
Sorting.testDeleteClients_07_01_2009_17_58.png

DailyPack.Administrator_Department – package name

Admin_Department_5_Clients_Sorting – class name

testDeleteClients – method name

07_01_2009_17_58 – a time when a screenshot was obtained

To send the results I’m using JavaMail 1.4.2. This set of classes for working with the mail system.

[sourcecode language=”plain”]
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Calendar;

public final class DemoMailer {
public static void main( String… aArguments ) throws IOException{
Emailer emailer = new Emailer();

// Получаем текущую дату

Calendar c = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy kk:mm");

// от кого будет письмо

String from = "tester@qa-testlab.com";

// получатель 1

String to1 = "tester2@qa-testlab.com";

// получатель 2

String to2 = "developer@qa-testlab.com";

// получатель 3

String to3 = "pm@qa-testlab.com";

// результаты прогона тестов для какого проекта

String project = "Book Shop";

// путь к отчету emailable-report.html

String path1 = "C:\\B2B_Pack\\test-output\\emailable-report.html";

// путь к скриншотам

String path2 = "C:\\B2B_Pack\\test-output\\screenshots";

emailer.sendEmail(

from, to1, to2, to3,

"Test Results for " + project + " " + sdf.format(c.getTime()), path1, path2);

}

public void sendEmail(

String aFromEmailAddr, String aToEmailAddr, String aToEmailAddr2, String aToEmailAddr3,

String aSubject, String attach, String screenpath

) throws IOException{

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

MimeMessage message = new MimeMessage( session );

try {

// добавляем получателя 1

message.addRecipient(

Message.RecipientType.TO, new InternetAddress(aToEmailAddr)

);

// добавляем получателя 2

message.addRecipient(

Message.RecipientType.TO, new InternetAddress(aToEmailAddr2)

);

// добавляем получателя 3

message.addRecipient(

Message.RecipientType.TO, new InternetAddress(aToEmailAddr3)

);

// тема письма
message.setSubject( aSubject );

// от кого письмо
message.setFrom(new InternetAddress(aFromEmailAddr));
MimeBodyPart attachFilePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attach);
attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(attachFilePart, "text/html");
Multipart mp = new MimeMultipart();
mp.addBodyPart(attachFilePart);
// аттачим все скриншоты
File dir = new File(screenpath);
String[] children = dir.list();
if (children == null) {
System.out.println("dir does not exist");
} else {
for (int i=0; i
String filename = children[i];
System.out.println("Adding: " + filename);
attachFilePart = new MimeBodyPart();
fds = new FileDataSource(screenpath+"\\"+filename);
attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
mp.addBodyPart(attachFilePart);
}
}
message.setContent(mp);
Transport.send( message );

System.out.println("Mail was sent to " + aToEmailAddr + ", " + aToEmailAddr2 + ", " + aToEmailAddr3);
}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}

public static void refreshConfig() {
fMailServerConfig.clear();
fetchConfig();
}

// PRIVATE //
private static Properties fMailServerConfig = new Properties();
static {
fetchConfig();
}

private static void fetchConfig() {
InputStream input = null;
try {
// конфигурационный файл
input = new FileInputStream("C:\\B2B_Pack\\mail.txt" );
fMailServerConfig.load( input );
}

catch ( IOException ex ){
System.err.println("Cannot open and load mail server properties file.");
}

finally {
try {
if ( input != null ) input.close();
}
catch ( IOException ex ){
System.err.println( "Cannot close mail server properties file." );
}
}
}
}
[/sourcecode]

Related Posts:

  Article "tagged" as:
  Categories:

About Article Author

view more articles
Nataliia Vasylyna
Nataliia Vasylyna

View More Articles

11 Comments

write a comment
  1. naveen prakash
    January 31, 08:33 #1 naveen prakash

    Problem in this code is, it is not attached the current version of testout emialble out. that means this reports generated once u refreshed the output folder otherwsie it will fetch the previous execution report.

    Reply to this comment
  2. pooja
    March 08, 12:37 #2 pooja

    Hey ,
    I m unable to run this code can you please let me know how to use Java Mail

    Reply to this comment
  3. Pooja
    November 15, 09:04 #3 Pooja

    i m getting lot of errors and unable to run the code can u just send the zip file of same to my id…….plz

    Reply to this comment
  4. KARTIK
    July 05, 15:29 #4 KARTIK

    CAN YOU TELL ME WHAT DETAILS NEED TO BE INCLUDED IN THE TEXT FILE..

    Reply to this comment
  5. Plethora
    November 14, 17:14 #5 Plethora

    Hi! I have been struggling to convert the time on the Total Time column on the emailable report from seconds to minute. Any Help, please!!!

    Reply to this comment

Add a Comment

Your data will be safe! Your e-mail address will not be published. Also other data will not be shared with third person.
All fields are required.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.