Reputation: 91
I am doing test automations for the first time, and I want to be able to automate gmail and send an email with an attachment. I am using selenium web driver, cucumber and google chrome to run the tests. My IDE is intelliJ. My tests work up until I have to attach the file:
public void givenOnAmazonProductPage() throws Throwable {
setupSeleniumWebDrivers();
goTo(PRODUCT_URL);
driver.findElement(By.id("identifierId")).sendKeys("username");
driver.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();
Thread.sleep(3000);
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();
Thread.sleep(4000);
goTo(PRODUCT_URL);
//driver.wait().until(ExpectedConditions.elementToBeClickable(By.xpath(".//textarea[contains(@aria-label, 'To')]")));
driver.findElement(By.xpath(".//textarea[contains(@aria-label, 'To')]")).click();
driver.findElement(By.xpath(".//textarea[contains(@aria-label, 'To')]")).sendKeys("[email protected]");
driver.findElement(By.name("subjectbox")).click();
driver.findElement(By.name("subjectbox")).sendKeys("efgh");
driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).click();
driver.findElement(By.xpath("(.//*[@aria-label='Message Body'])[2]")).sendKeys("This is an auto-generated mail");
//driver.findElement(By.xpath("//span[@class='T-I J-J5-Ji T-I-KE L3']")).click();
//driver.close();
//click on attachment
driver.findElement(By.xpath("//div[@class='a1 aaA aMZ']")).click();
//use autoit tool to attach a file
this is where i try to attach the file that is on my desktop but it doesnt seem to work
Runtime.getRuntime().exec("C:Desktop/6c3bfdec92fad54896275802f938bd83.29.jpg");
// enter the file path onto the file-selection input field
Thread.sleep(10000); //wait for 10sec to upload file
}
does anyone know what I am doing wrong to attach the file?
Upvotes: 0
Views: 2695
Reputation: 193088
There are several simple ways to automate sending of an email with an attachment even without using Selenium as follows:
smtp
settings.In this answer I will explain about using the commons email api through Maven.
Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.
Some of the mail classes that are provided are as follows:
SimpleEmail
- This class is used to send basic text based emails.MultiPartEmail
- This class is used to send multipart messages. This allows a text message with attachments either inline or attached.HtmlEmail
- This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.ImageHtmlEmail
- This class is used to send HTML formatted emails with inline images. It has all of the capabilities as HtmlEmail but transform all image references to inline images.EmailAttachment
- This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
Maven Dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
Code Block:
package SendEmailAttachments;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
public class EmailAttachments {
public static void main(String[] args) throws EmailException {
System.out.println("===Test for Sending CommonsEmail started===");
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("C:\\Users\\AtechM_03\\Desktop\\Screenshots\\bad_indentation.png");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of bad indentation");
attachment.setName("BadIndentation");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("[email protected]", "Matthew_Zoltak"));
email.setSSLOnConnect(true);
email.setFrom("[email protected]");
email.setSubject("CommonsEmail Test");
email.setMsg("CommonsEmail test mail ... :-)");
email.addTo("[email protected]");
// add the attachment
email.attach(attachment);
// send the email
email.send();
System.out.println("===Test for Sending CommonsEmail ended===");
}
}
Console Output:
===Test for Sending CommonsEmail started===
===Test for Sending CommonsEmail ended===
Snapshot:
Upvotes: 0
Reputation: 33384
This should be your autoit
.exe path not .jpg path. You need to create executable (.exe)
of your autoit scrips and pass as i have mentioned.
Runtime.getRuntime().exec("path of Autoit exe"); // like "C:\\AutoIt3\\new.exe"
Upvotes: 1