Saturday, November 7, 2015

Sending mail from Gmail using WebDriver

Gmail is the mostly used mail client. Here is the logic to send a mail from gmail using WebDriver.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.concurrent.TimeUnit;

public class SendMailFromGmail {

 
 public static void main(String[] args) {
  
  String userName="<User name>";
  String password ="<password>";
  String toAddress="san******@***.com";
  String subject="WebDriver - Selenium Testing";
  
  //Initialising driver
  WebDriver driver = new FirefoxDriver();
  
  //setting timeout for page load & implicit wait
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
  
  //Call Url in get method
  driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en");
  
  //Login
  driver.findElement(By.id("Email")).sendKeys(userName);
  driver.findElement(By.id("Passwd")).sendKeys(password);
  driver.findElement(By.id("signIn")).click();
  
  //click on Compose button
  driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
  
  
  //enter TO address & Subject
  driver.findElement(By.xpath("//*[text()='To']/../../..//textarea")).sendKeys(toAddress);
  driver.findElement(By.name("subjectbox")).sendKeys(subject);
  
  //click Send button
  driver.findElement(By.xpath("//div[text()='Send']")).click();
  
  //Logout from Gmail
  driver.findElement(By.xpath("//a[contains(.,'"+userName+"')]")).click();
  driver.findElement(By.xpath("//div[contains(.,'Add account')]//a[contains(text(),'Sign out')]")).click();
  
 }
 
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.