Sunday, September 9, 2012

Using Selenium Web Driver to send e-mail with GMail

I'm trying to learn more about using Selenium Web Driver for testing web applications, so I've spent a good chunk of time this weekend doing all kinds of little things with websites, just to learn my way around.

The hardest one I had was sending an e-mail with GMail. That 'Compose' button was tricky for me at first.

Here is the result of my efforts, just in case anyone ever wants to automate GMail with Selenium, I hope it helps you out.



6 comments:

  1. Hi Jayson,
    I tried the code posted by you, but not able to execute it successfully.
    Getting error at : driver.findElement(By.cssSelector("textarea[class='Ak aXjCH']")).sendKeys("This is a test email sent via Selenium Web Driver");
    The test script does not enter body content and fails to find "textarea[class='Ak aXjCH']"
    Please help me.

    ReplyDelete
  2. rama sharma,
    Perhaps the Gmail form has changed since I wrote this. You should do like I did originally - fire up Firefox with Firebug and find the elements you need. It's possible it's been moved or renamed by the Gmail team at Google.

    ReplyDelete
  3. Hi Rama,

    The text box is within a iframe. Below xpath should work.

    driver.findElementByXPath(("//iframe[contains(@class, 'Am Al editable')]")).click();
    driver.findElementByXPath(("//iframe[contains(@class, 'Am Al editable')]")).sendKeys("This is a text email from selenium..testing successfull");

    ReplyDelete
  4. Hi can you please update code.... As per new compose changes in Gmail

    ReplyDelete
  5. import java.util.List;

    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.openqa.selenium.firefox.FirefoxDriver;


    public class sendmail {
    Thread t=new Thread();

    @ Test

    public void mailtest(String username,String password){
    System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

    WebDriver driver=new ChromeDriver();
    driver.get("http://www.gmail.com");
    driver.findElement(By.xpath("//*[@id='Email']")).sendKeys(username);
    driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(password);
    driver.findElement(By.xpath("//*[@id='signIn']")).click();
    driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")).click();
    try{
    t.currentThread().sleep(10000);
    }
    catch(Exception e){

    }
    driver.findElement(By.cssSelector("textarea[class='vO']")).sendKeys("shariq.abbas@laitkor.com");
    driver.findElement(By.cssSelector("input[class='aoT']")).sendKeys("Mission Successfull");
    driver.findElement(By.cssSelector("div[class='Am Al editable LW-avf']")).sendKeys("What you were working for u have done that.Great!!");

    driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji aoO T-I-atl L3']")).click();


    }

    }

    Here is new code as per current gmail new compose introduced this year'13.

    ReplyDelete
  6. This is the simple selenese code:

    open | /mail/u/0/?shva=1#inbox?compose=new |
    click | class=aoD hl |
    type | //table/tbody/tr[1]/td[2]/div/div/textarea | McKiran@example.com
    type | name=subjectbox | Test
    type | class=editable LW-avf | Hi All<br/>Hope the test is successful. <br/><br/><i> Lovingly, </i></br><b>McKiran.</b>
    click | class=T-I J-J5-Ji aoO T-I-atl L3 |

    ReplyDelete