Wednesday, 19 February 2014

E-mail Selenium Test Reports


E-mailing selenium Test Reports become even easier for you to notify your clients.

Create a class Lib

1| Create a class library in your package.
2| Just copy & paste the below code;  Do few edits that are highlighted in ORANGE


package packagename;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMailSSL {

public static void execute(String reportFileName) throws Exception {

final String username = "yourusername@gmail.com";
final String password = "yourpassword";

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("yourusername@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("yourrecepientusername@gmail.com"));
message.setSubject("Reports Availalbe!");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");

MimeBodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();

messageBodyPart = new MimeBodyPart();
String file = "C:\\Users\\prashanth_sams\\workspace\\jxl\\test-output\\";
String fileName = reportFileName;
DataSource source = new FileDataSource(file + fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}


Test Class

Add a small snippet [SendMailSSL.execute("report.html");] after your test execution.


@AfterTest
public void tearDown() throws Exception {
driver.quit();
Thread.sleep(3000);
SendMailSSL.execute("emailable-report.html");
}




8 comments:

  1. Is it required to import any jar files to execute this script?

    ReplyDelete
  2. Sending
    FAILED CONFIGURATION: @AfterTest tearDown
    java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
    nested exception is:

    ReplyDelete
  3. Just what I was looking for.

    Many thanks

    ReplyDelete
  4. Hi, the testng report file is not getting generated even after driver.quit(). any way to wait for report like emailable-report.html to get generated first before moving ahead to send them ??

    thanks,
    Rahul

    ReplyDelete
  5. Cannot inject @Test annotated Method [execute] with [class java.lang.String].

    getting the above error while executing the code

    ReplyDelete
  6. i am getting the following issue Must issue a STARTTLS command first. r138sm24776114pgr.12 - gsmtp

    on execution of the above code .Please Help

    ReplyDelete
  7. Am getting the below errors : not executing

    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:230)
    at javax.mail.Session.(Session.java:214)
    at javax.mail.Session.getInstance(Session.java:251)
    at com.mercury.tours.testcases.SendEmailSSL.execute(SendEmailSSL.java:32)
    at com.mercury.tours.testcases.SendEmailSSL.main(SendEmailSSL.java:73)
    Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

    ReplyDelete
  8. This will be sent as an attachment. Is there a way that the css and html are embedded properly in the body of the email

    ReplyDelete