by Nataliia Vasylyna | February 24, 2011 12:20 pm
If you have to run a bunch of automated tests[1] 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.
[2]
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[3]. 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]
Source URL: https://blog.qatestlab.com/2011/02/24/e-mail-distribution-of-test-results-obtained-from-selenium-testng/
Copyright ©2024 QATestLab Blog unless otherwise noted.