Sunday, September 9, 2012

Using Selenium Web Driver to send e-mail with GMail

Edit: 4/10/2014:

This is one of the most viewed posts on my blog, and it is very much out of date. Please do not try to use this code. It will not work. I'm sorry, but I'm neither using Selenium nor writing automated acceptance tests for anything right now as I have moved on to a very different role, so it's doubtful that any of these selenium related posts will be updated.

The general idea is still the same, but Gmail has changed very much, and if you are looking for a copy/past solution to send an e-mail via Gmail for some reason (and I'm not sure why you'd use selenium for that...it's really a testing framework) this isn't going to work. Sorry. I'll leave the original post below for historical purposes. Thanks!

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.



15 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
    Replies
    1. driver.findElement(By.cssSelector("div[class='Am Al editable LW-avf']")).sendKeys("What you were working for u have done that.Great!!");

      this line is not executed.
      I mean in the compose window the subject is not getting typed. Please help

      Delete
  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
  7. Not working, Unable to click on send button, used all method,cssSelector,xpath,className..

    ReplyDelete
  8. Use the following code

    Compose:

    fD.findElementByXPath("//div[contains(text(),'COMPOSE')]").click();

    To:

    fD.findElementByName(to).click();
    fD.findElementByName(to).sendKeys(testInput);


    Subject:

    fD.findElementByName("subject").click();
    fD.findElementByName("subject").sendKeys("Selenium Test");


    Send:

    fD.findElementByXPath("html/body/div[5]/div[2]/div/div[2]/div[1]/div[2]/div/div/div/div[1]/div[2]/div/div/div[1]/div[1]").click();

    ReplyDelete
    Replies
    1. I used your code, unable to enter the subject. I think the Gmail code is changed a bit.
      Check and help me please.

      Delete
  9. Try above code to click on send button.

    ReplyDelete
  10. the entire code itself is not working . not able to locate the webelement using cssSelector. please help

    ReplyDelete
  11. Sharon, this is quite old, and very much out of date. I'm not longer actively doing Selenium stuff, so I probably won't be updating any of these posts, sorry.
    Perhaps you could try using Firebug or the Chrome Developer tools to find your elements.

    ReplyDelete
  12. Hi,
    How to open the first unread mail from Gmail using webdriver?
    Plz help me to find the solution

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete