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("abcd@gmail.com");
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?
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"
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("Matthew@Zoltak.in", "Matthew_Zoltak")); email.setSSLOnConnect(true); email.setFrom("CommonsEmail@gmail.com"); email.setSubject("CommonsEmail Test"); email.setMsg("CommonsEmail test mail ... :-)"); email.addTo("Matthew@Zoltak.in"); // 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.