Wednesday, 19 November 2014

Email Test Result after completion of test in Selenium Web driver.



Email Test Result after completion of test in Selenium Web driver.

1.       Create a project in eclipse and download necessary jars for Selenium.

2.       Now download necessary jars for sending an email.
a)      javaee-api-5.0.3.jar
b)      javamail1_4_7.zip

3.       Add the jars to the build path.

4.       Now create a new class “sendmail.java” and copy paste the below code. This code will email the stack trace after failure of that testcase.

package Testcases;
                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("archana. @gmail.com","xyz123");
                }
                });
                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);
                }
                }
                }

 
5.       Now create another class “sendsuccess.java”. This file will send success mail in case test case has passed.
 package Testcases;

                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 sendsucess{
                public sendsucess(String fromMail,String tomail, String success )
                {
                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("archanagmail.com","xyz123");
                }
                });
                try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(fromMail));
                message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(tomail));
                message.setSubject("Script Pass");
                                message.setText("your test has passed for script name:Name of your scipt <============================>");
                Transport.send(message);
                System.out.println("Done");
                } catch (MessagingException ex) {
                throw new RuntimeException(ex);
                }
                }
                }

6.       Now Write a test case in Junit and use the above send mail functionality.

package Testcases;
import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
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;
@Before
public void launch()
{
System.setProperty("webdriver.chrome.driver", "D:\\Workspace\\DemoSiteAutomation\\Drivers\\chromedriver.exe");
d = new ChromeDriver();
}
@Test
public void test()
{
d.get("https://in.yahoo.com/?p=us");
try{
WebElement element= d.findElement(By.xpath("//*[@id='p_13838465-p']"));
element.sendKeys("automation");

String s="Pass";

new sendsucess("archanagmail.com","archana@gmail.com",s);
}
catch(Exception e)
{
e.printStackTrace();
new SendMail("archana@gmail.com","archana.@gmail.com",e);
}
}
}




No comments:

Post a Comment