Hi,
I am trying to write a script to Send email through Gmail using Web
driver.
I am having a tough time with xpaths and need some help on how xpaths
should be specified when using Selenium2. Please see below the code I
have written. The code may not be perfect as I am totally new to
selenium and trying my hands for the first time at this. Any help
would be greatly appreciated.
Code:
package com.selenium.scripts;
import
org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class gmail {
static WebDriver driver;
static Wait wait;
private static final String USER_NAME = “xxx″;
private static final String PASSWORD = “xxx″;
private static final String EMAIL = “xxx”;
private static final String SUBJECT = “Subject & Body only mail”;
private static final String MAIL_BODY = “Hi, How r u doing”;
public static void main(String[] args) {
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 30);
driver.get(“
https://www.google.com/accounts/ServiceLogin?
service=mail&passive=true&rm=false&continue=http%3A%2F
%
2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy
%3Dl&bsv=llya694le36z&scc=1<mpl=default<mplcache=2&from=login”);
driver.switchTo().defaultContent();
boolean result;
try {
result = Signin();
} catch(Exception e) {
e.printStackTrace();
result = false;
} finally {
driver.close();
}
System.out.println(“Test ” + (result? “passed.” : “failed.”));
if (!result) {
System.exit(1);
}
}
private static boolean Signin() {
//type search query
driver.findElement(By.name(“Email”)).sendKeys(USER_NAME);
// click search
driver.findElement(By.name(“Passwd”)).sendKeys(PASSWORD);
// Wait for search to complete
//driver.findElement(By.name(“signIn”)).click();
driver.findElement(By.name(“signIn”)).click();
driver.switchTo().defaultContent();
wait.until(new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {
// webDriver.switchTo().defaultContent();
webDriver.getPageSource().contains(“Compose mail”);
return Boolean.valueOf(true);
}
});
//wait.until(driver.getPageSource().contains(“Compose mail”));
if(driver.getPageSource().contains(“Compose mail”))
{ System.out.println(“Successful login to Gmail”); }
//Clicking on Compose mail button
driver.switchTo().defaultContent();
driver.switchTo().frame(“canvas_frame”);
WebElement element = driver.findElement(By.xpath(“/html/body/div[1]/
div[2]/div/div[2]/div[1]/div[1]/div[1]/div[3]/div[@id=':k5']/div[1]/
div[@id=':k6']/div/div”));
element.click();
//driver.switchTo().defaultContent();
wait.until(new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {
webDriver.getPageSource().contains(“Discard”);
return Boolean.valueOf(true);
}
});
driver.switchTo().defaultContent();
driver.switchTo().frame(“canvas_frame”);
System.out.println(“passed compose button”);
driver.switchTo().defaultContent();
//Entering the destination email address in “To” field
WebElement element1 = driver.findElement(By.id(“:g8″));
element1.sendKeys(EMAIL);
//Entering the subject in “Subject” field
driver.findElement(By.xpath(“//*[@id=':g5']“)).sendKeys(SUBJECT);
//Entering email content
driver.findElement(By.xpath(“//html/body[@class='editable LW-
yrriRe']“)).sendKeys(MAIL_BODY);
//Sending the email by clicking on Send button
driver.findElement(By.xpath(“//div[@role='button']/b”)).click();
//Look for mail sent message
return driver.findElement(By.tagName(“body”)).getText().contains(“Your
message has been sent”);
}
}