In this post, we would be learning how to send Email for failing Selenium Script which includes all the stack trace. For this, we would be using Java Mail API and In one of the post we have written a detailed post about uses of JavaMail API, So if you want to read the post then read it
Reading e-mail using Javamail api
1- Pre-requisite
First, we need to download two jar files
1- javaee-api-5.0.3.jar
2- javamail1_4_7.zip
download both file from here
[the_ad_placement id=”incontent”]
In this Java Mail API we use mainly two packages
1) javax.mail.internet.
2) javax.mail
In general we use three steps to send emails using JavaMail API
1- Session Object creation that stores all the information like Hostname, username and password
like this
Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("usermail_id","password"); } })
2- Setting Subject and Message body like this
message.setSubject("Testing Subject"); //this line is used for setting Subject line message.setText("your test has failed <============================>"+ExceptionUtils.getStackTrace(e) );
3- Sending mail that could be done with this line of code
Transport.send(message);
[the_ad_placement id=”incontent”]
2- Now I am posting here my code of Mail sending class
package test_Scripts; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.lang3.exception.ExceptionUtils; public class SendMail{ public SendMail(String fromMail,String tomail, Exception e ) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("Email_Id","password"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromMail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(tomail)); message.setSubject("Script failed"); message.setText("your test has failed for script name:Name of your scipt <============================>"+ ExceptionUtils.getStackTrace(e) ); Transport.send(message); System.out.println("Done"); } catch (MessagingException ex) { throw new RuntimeException(ex); } } }
I have written one class SendMail with one constructor with three parameters. that I would call in my WebDriver script. and When My code would fail somewhere then It will send the Stack Trace.
3- My script of WebDriver
package test_Scripts; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class Except { WebDriver d; @BeforeMethod public void launch() { System.setProperty("webdriver.chrome.driver", "E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe"); d = new ChromeDriver(); } @Test public void test() { d.get("www.gmail.com"); try{ WebElement element= d.findElement(By.xpath("//div/span")); element.sendKeys("dwarika"); } catch(Exception e) { e.printStackTrace(); new SendMail("Sender_Mailid","Receiver_Mailid",e); } } }
I have tried it to send an email for failing Selenium Script. So hope you also enjoy this post . would be very happy if you share your advice to optimize this code.
Leave a Reply