How to send Selenium Webdriver testing summary report in a Simple or an HTML or Attachment E-mail using java.

We use Selenium Webdiver automation tool for Web functional and regression testing. We run many test cases using that tool. Sometime We need to send summary testing report which reports include how many test cases are successfully passed or failed. In this tutorial I will show how to send Selenium Webdriver testing summary report in a Simple or an HTML or Attachment E-mail.


Download mail.jar and add desired project
Send a Simple E-mail
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.util.*;  
 import java.util.concurrent.TimeUnit;  
 import javax.mail.*;  
 import javax.mail.internet.*;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Sendsimpleemail {  
   static Properties prop = null;  
   static String urls = "";  
   public static void main(String[] args) throws IOException {  
     List<WebElement> urllist = new ArrayList();  
     // Initialize driver     
     WebDriver driver = new FirefoxDriver();  
     //Maximize browser window      
     driver.manage().window().maximize();  
     //Go to URL     
     driver.get("https://www.yahoo.com/");  
     //Set timeout     
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     urllist = driver.findElements(By.tagName("a"));  
     try {  
       for (WebElement url : urllist) {  
         if ((url.getAttribute("href").trim().length() > 4) && (url.getAttribute("href").trim().contains("yahoo.com")) && !(url.getAttribute("href").trim().contains("javascript:void(0)"))) {  
           urls = urls + url.getAttribute("href").trim() + "\n";  
         }  
       }  
     } catch (NullPointerException e) {  
 //      e.printStackTrace();  
     }  
     System.out.println(urls);  
     //close firefox browser   
     driver.close();  
     // Recipient's email ID needs to be mentioned.  
     String to = "hiromia006@gmail.com";  
     // Assuming you are sending email from localhost  
     String host = "localhost";  
     // Create FileInputStream Object   
     FileInputStream fileInput = new FileInputStream(new File("data.properties"));  
     // Create Properties object      
     prop = new Properties();  
     //load properties file   
     prop.load(fileInput);  
     // Get system properties  
     Properties properties = System.getProperties();  
     // Setup mail server  
     properties.setProperty("mail.smtp.host", host);  
     properties.put("mail.smtp.starttls.enable", "true");  
     properties.put("mail.smtp.host", "smtp.gmail.com");  
     properties.put("mail.smtp.user", prop.getProperty("From_email")); // User name  
     properties.put("mail.smtp.password", prop.getProperty("From_email_password")); // password  
     properties.put("mail.smtp.port", "587");  
     properties.put("mail.smtp.auth", "true");  
     Authenticator authenticator = new Authenticator() {  
       public PasswordAuthentication getPasswordAuthentication() {  
         return new PasswordAuthentication(prop.getProperty("From_email"), prop.getProperty("From_email_password"));//userid and password for "from" email address   
       }  
     };  
     // Get the default Session object.  
     Session session = Session.getDefaultInstance(properties, authenticator);  
     try {  
       // Create a default MimeMessage object.  
       MimeMessage message = new MimeMessage(session);  
       // Set From: header field of the header.  
       message.setFrom(new InternetAddress(prop.getProperty("From_email")));  
       // Set To: header field of the header.  
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       // Set Subject: header field  
       message.setSubject("Yahoo Home page internet links");  
       // Now set the actual message  
       message.setText(urls);  
       // Send message  
       Transport.send(message);  
       System.out.println("Sent message successfully....");  
     } catch (MessagingException mex) {  
       mex.printStackTrace();  
     }  
   }  
 }  

Send an HTML E-mail
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.util.*;  
 import java.util.concurrent.TimeUnit;  
 import javax.mail.*;  
 import javax.mail.internet.*;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Sendanhtmlemail {  
   static Properties prop = null;  
   static String urltexts = "";  
   public static void main(String[] args) throws IOException {  
     List<WebElement> urllist = new ArrayList();  
     // Initialize driver  
     WebDriver driver = new FirefoxDriver();  
     //Maximize browser window  
     driver.manage().window().maximize();  
     //Go to URL  
     driver.get("https://www.google.com/");  
     //Set timeout  
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     urllist = driver.findElements(By.tagName("a"));  
     try {  
       for (WebElement url : urllist) {  
         if ((url.getText().trim().length() > 1) && !(url.getText().trim().contains("null"))) {  
           urltexts = urltexts + "<h1>"+url.getText().trim() + "<h1>\n";  
         }  
       }  
     } catch (NullPointerException e) {  
       //   e.printStackTrace();  
     }  
     System.out.println(urltexts);  
     //close firefox browser  
     driver.close();  
     // Recipient's email ID needs to be mentioned.  
     String to = "hiromia006@gmail.com";  
     // Assuming you are sending email from localhost  
     String host = "localhost";  
     // Create FileInputStream Object  
     FileInputStream fileInput = new FileInputStream(new File("data.properties"));  
     // Create Properties object  
     prop = new Properties();  
     //load properties file  
     prop.load(fileInput);  
     // Get system properties  
     Properties properties = System.getProperties();  
     // Setup mail server  
     properties.setProperty("mail.smtp.host", host);  
     properties.put("mail.smtp.starttls.enable", "true");  
     properties.put("mail.smtp.host", "smtp.gmail.com");  
     properties.put("mail.smtp.user", prop.getProperty("From_email")); // User name  
     properties.put("mail.smtp.password", prop.getProperty("From_email_password")); // password  
     properties.put("mail.smtp.port", "587");  
     properties.put("mail.smtp.auth", "true");  
     Authenticator authenticator = new Authenticator() {  
       public PasswordAuthentication getPasswordAuthentication() {  
         return new PasswordAuthentication(prop.getProperty("From_email"), prop.getProperty("From_email_password"));//userid and password for "from" email address  
       }  
     };  
     // Get the default Session object.  
     Session session = Session.getDefaultInstance(properties, authenticator);  
     try {  
       // Create a default MimeMessage object.  
       MimeMessage message = new MimeMessage(session);  
       // Set From: header field of the header.  
       message.setFrom(new InternetAddress(prop.getProperty("From_email")));  
       // Set To: header field of the header.  
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       // Set Subject: header field  
       message.setSubject("Google Home page internet links text");  
       // Send the actual HTML message, as big as you like  
       message.setContent(urltexts, "text/html" );  
       // Send message  
       Transport.send(message);  
       System.out.println("Sent message successfully....");  
     } catch (MessagingException mex) {  
       mex.printStackTrace();  
     }  
   }  
 }  

Send Attachment in E-mail
 import java.io.*;  
 import java.util.*;  
 import java.util.concurrent.TimeUnit;  
 import javax.mail.*;  
 import javax.mail.internet.*;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import javax.activation.*;  
 public class SendAttachmentemail {  
   static Properties prop = null;  
   static String urls = "";  
   static File file;  
   public static void main(String[] args) throws IOException {  
     List<WebElement> urllist = new ArrayList();  
     // Initialize driver  
     WebDriver driver = new FirefoxDriver();  
     //Maximize browser window  
     driver.manage().window().maximize();  
     //Go to URL  
     driver.get("https://www.yahoo.com/");  
     //Set timeout  
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     urllist = driver.findElements(By.tagName("a"));  
     try {  
       for (WebElement url : urllist) {  
         if ((url.getAttribute("href").trim().length() > 4) && (url.getAttribute("href").trim().contains("yahoo.com")) && !(url.getAttribute("href").trim().contains("javascript:void(0)"))) {  
           urls = urls + url.getAttribute("href").trim() + "\n";  
         }  
       }  
     } catch (NullPointerException e) {  
       //   e.printStackTrace();  
     }  
     try {  
       file = new File("Internetlinks.txt");  
       // if file doesnt exists, then create it  
       if (!file.exists()) {  
         file.createNewFile();  
       }  
       // Write text on txt file.  
       FileWriter fw = new FileWriter(file, true);  
       BufferedWriter bw = new BufferedWriter(fw);  
       bw.write(urls);  
       bw.close();  
     } catch (IOException e) {  
       e.printStackTrace();  
     }  
     System.out.println(urls);  
     //close firefox browser  
     driver.close();  
     // Recipient's email ID needs to be mentioned.  
     String to = "hiromia006@gmail.com";  
     // Assuming you are sending email from localhost  
     String host = "localhost";  
     // Create FileInputStream Object  
     FileInputStream fileInput = new FileInputStream(new File("data.properties"));  
     // Create Properties object  
     prop = new Properties();  
     //load properties file  
     prop.load(fileInput);  
     // Get system properties  
     Properties properties = System.getProperties();  
     // Setup mail server  
     properties.setProperty("mail.smtp.host", host);  
     properties.put("mail.smtp.starttls.enable", "true");  
     properties.put("mail.smtp.host", "smtp.gmail.com");  
     properties.put("mail.smtp.user", prop.getProperty("From_email")); // User name  
     properties.put("mail.smtp.password", prop.getProperty("From_email_password")); // password  
     properties.put("mail.smtp.port", "587");  
     properties.put("mail.smtp.auth", "true");  
     Authenticator authenticator = new Authenticator() {  
       public PasswordAuthentication getPasswordAuthentication() {  
         return new PasswordAuthentication(prop.getProperty("From_email"), prop.getProperty("From_email_password"));//userid and password for "from" email address  
       }  
     };  
     // Get the default Session object.  
     Session session = Session.getDefaultInstance(properties, authenticator);  
     try {  
       // Create a default MimeMessage object.  
       MimeMessage message = new MimeMessage(session);  
       // Set From: header field of the header.  
       message.setFrom(new InternetAddress(prop.getProperty("From_email")));  
       // Set To: header field of the header.  
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       // Set Subject: header field  
       message.setSubject("Yahoo Home page internet links attached text field");  
       // Create the message part  
       BodyPart messageBodyPart = new MimeBodyPart();  
       // Fill the message  
       messageBodyPart.setText("have sent Yahoo home page url list by attached file");  
       // Create a multipar message  
       Multipart multipart = new MimeMultipart();  
       // Set text message part  
       multipart.addBodyPart(messageBodyPart);  
       // Part two is attachment  
       messageBodyPart = new MimeBodyPart();  
       DataSource source = new FileDataSource("Internetlinks.txt");  
       messageBodyPart.setDataHandler(new DataHandler(source));  
       messageBodyPart.setFileName("Internetlinks.txt");  
       multipart.addBodyPart(messageBodyPart);  
       // Send the complete message parts  
       message.setContent(multipart );  
       // Send message  
       Transport.send(message);  
       System.out.println("Sent message successfully....");  
     } catch (MessagingException mex) {  
       mex.printStackTrace();  
     }  
   }  
 }  

How can open and close browser tabs in selenium Webdriver on Mac using java.

 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.Keys;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Openandclosetabhandler {  
   public static void main(String[] a) throws InterruptedException {  
     // Initialize driver   
     WebDriver driver = new FirefoxDriver();  
     //Maximize browser window    
     driver.manage().window().maximize();  
     //Go to URL   
     driver.get("http://www.google.com");  
     //Set timeout   
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     // Open new tab   
     driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "t");  
     //Go to URL   
     driver.get("http://www.yahoo.com/");  
     //Set new tab timeout   
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     // Do some operation       
     driver.findElement(By.partialLinkText("Sign in")).click();  
     Thread.sleep(2000);  
     // Switch first tab   
     driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "w");  
     driver.switchTo().defaultContent();  
     Thread.sleep(2000);  
     // Operation   
     driver.findElement(By.partialLinkText("Gmail")).click();  
     Thread.sleep(2000);  
     // Browser close    
     driver.close();  
   }  
 }  

How to get HTML tag Attribute values in Selenium Webdriver using Java.

Sometime We need to get the attribute values to perform some operation. In this tutorial I show how to get tag attribute values using Selenium Webdriver.

In the below HTML code, You see input tag which has multiple attribute 'type', 'class', 'name', 'id', 'autocomplete', 'autocomplete', 'value' and 'title' and hash values for each attribute. To get the attribute value, We need to use below selenium webdriver syntax.

Syntax: webelement_select+tag.getAttribute(attributeName).

We try to get the attribute value that doesn't exists for the tag, it will return null value.
 <!DOCTYPE html>  
 <html>  
 <head>  
 <title>Get Attribute values</title>  
 </head>  
 <body>  
 <h1>Get Attribute values</h1>  
 <form name="domf1" method="post">  
 <div>  
  <label for="Email" class="class-name1 class-name2"> Enter your email</label>  
  <input type="email" class="class-name3 class-name1 class-name2 class-name4" name="btnK" id="gbqfba" autocomplete="off" value="" title="Search">  
 </div>  
 </form>   
  </body>  
 </html>   
Implementation Selenium Webdriver Source code for Upper HTML Code
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Getattributevalueshandler {  
   public static void main(String[] args) throws InterruptedException {  
     // create objects and variables instantiation     
     WebDriver driver = new FirefoxDriver();  
     // maximize the browser window     
     driver.manage().window().maximize();  
     // launch the firefox browser and open the application url    
     driver.get("file:///C:/Users/Hiro%20Mia/Desktop/Blog%20content/Attribute.html");  
     //Set timeout     
 //    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     // identify input tag by proper selector  
     WebElement inputtag = driver.findElement(By.id("gbqfba"));  
     // Print all attribute value under unput tag  
     System.out.println("The Atribute of type value     =: " + inputtag.getAttribute("type"));  
     System.out.println("The Atribute of class value    =: " + inputtag.getAttribute("class"));  
     System.out.println("The Atribute of name value     =: " + inputtag.getAttribute("name"));  
     System.out.println("The Atribute of autocomplete value =: " + inputtag.getAttribute("autocomplete"));  
     System.out.println("The Atribute of title value    =: " + inputtag.getAttribute("title"));  
     System.out.println("The Atribute of href value     =: " + inputtag.getAttribute("href"));  
     // close browser  
     driver.close();  
   }  
 }  
After running the above code the output should look something like below: