Sunday, May 11, 2014

How to send a mail through gmail with attachment.



import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

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

public class SendMailWithAttachment{
    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner kb = new Scanner(System.in);
        System.out.println("enter ur id: ");
        String id = kb.nextLine();
        System.out.println("enter ur pass: ");
        String password = kb.nextLine();
        System.out.println("enter email-id to whom u want to send the mail: ");
        String toId = kb.nextLine();
        System.out.println("enter subject for mail: ");
        String subject = kb.nextLine();
        System.out.println("enter the content of the mail: ");
        String content = kb.nextLine();
       
        //open gamil
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&hl=en&emr=1&elo=1");
       
        //login to gmail
        driver.findElement(By.id("Email")).sendKeys(id);
        driver.findElement(By.xpath("//input[@id='Passwd']")).sendKeys(password);
        driver.findElement(By.xpath("//input[@type='checkbox']")).click();
        driver.findElement(By.name("signIn")).click();
       
        //click on compose and add the to mail id, and subject
        driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();
        driver.findElement(By.xpath("//form[1]//textarea[1]")).sendKeys(toId);
        driver.findElement(By.xpath("//div[@class='aoD az6']//input[@class='aoT']")).sendKeys(subject);
       
        //wirte the mail body, that mail body is under frame so 1st switch the control to frame then write
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@tabindex='1']")));
        driver.findElement(By.xpath("//body[@class='editable LW-avf']")).sendKeys(content);
        driver.switchTo().defaultContent(); // again switch back to main page
       
        //click on attachment
        driver.findElement(By.xpath("//div[@class='a1 aaA aMZ']")).click();
        //use autoit tool to attach a file
        Runtime.getRuntime().exec("C:\\selenium\\AutoIT\\fileUpload.exe");
        Thread.sleep(10000); //wait for 10sec to upload file
       
        //click on send
        driver.findElement(By.xpath("//div[text()='Send']")).click();
        String msg = driver.findElement(By.xpath("//div[contains(text(),'Your message has been sent.')]")).getText();
        String exp = "Your message has been sent. View message";
        Assert.assertEquals(msg, exp);
        System.out.println("pass");
        driver.close();
    }
}



Note- Here Autoit tool has been used to attach the file because by using sendKeys() method we can't upload the file in gmail. Below is the script used to upload the file thru autoit-

WinWaitActive("File Upload")
Send("path of the file")
ControlClick("","&Open","Button1")

ex- path of the file = C:\java learn\important cmd for Java.txt

6 comments:

  1. Replies
    1. I want to automate of file attaching in gmail using selenium in java in selenium webdriver.Robot class is giving me the output but it attaches in gmail, the pasted file in clipboard.I want to attach ANY file in gmail...how Should i proceed..???

      Delete
  2. Thank you for sharing the script. It is easy to understand and gives an example of how web automation test can be done.

    In the script, the following two lines don't seem to automate the action of clicking the file attachment button and typing the attached file path smoothly because the code waits at "driver..click()" after the file attachment dialog opens and "Runtime.." is not executing.

    driver.findElement(By.xpath("//div[@class='a1 aaA aMZ']")).click();
    Runtime.getRuntime().exec("C:\\selenium\\AutoIT\\fileUpload.exe");

    A way of working around is to make a thread that runs AutoIt script after the file attachment button is clicked in the main test thread. I.e.

    Code of a thread that runs a AutoIt script:

    class AutoItThread extends Thread {
    public void run() {
    try {
    // Delay for waiting the main thread to click the file attachment button
    Thread.sleep(2000);
    // Use AutoIt to attach a file
    Runtime.getRuntime().exec("C:\\selenium\\AutoIT\\fileUpload.exe");
    } catch (Exception e) {
    }
    }
    }

    Code of the thread to replace the two lines:

    AutoItThread thread = new AutoItMethod();
    thread.run();
    driver.findElement(By.xpath("//div[@class='a1 aaA aMZ']")).click();


    ReplyDelete
  3. hi guys ,how to add multiple mail ids in gmail recipient

    ReplyDelete