【问题标题】:How to automate sending attachments through GMail using Selenium java如何使用 Selenium java 通过 GMail 自动发送附件
【发布时间】:2019-08-01 00:39:56
【问题描述】:

我是第一次进行自动化测试,我希望能够自动化 gmail 并发送带有附件的电子邮件。我正在使用 selenium 网络驱动程序、黄瓜和谷歌浏览器来运行测试。我的 IDE 是 IntelliJ。我的测试一直有效,直到我必须附加文件:

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 

这是我尝试附加桌面上的文件的地方,但它似乎不起作用

 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
}

有谁知道我在附加文件时做错了什么?

【问题讨论】:

    标签: maven selenium gmail email-attachments apache-commons-email


    【解决方案1】:

    这应该是您的 autoit .exe 路径而不是 .jpg 路径。你需要为你的 autoit 脚本创建可执行的(.exe) 并按照我提到的那样通过。

    Runtime.getRuntime().exec("path of Autoit exe"); // like "C:\\AutoIt3\\new.exe"
    

    【讨论】:

    • 但是没有mac版autoit?
    【解决方案2】:

    即使不使用 Selenium,也有几种简单的方法可以自动发送带有附件的电子邮件,如下所示:

    • 如果您使用的是,则有单独的插件用于GMailsmtp 设置。
    • 如果您使用的是 Maven,则可以使用 postman 插件。
    • 直接从您的测试代码使用commons email api

    在这个答案中,我将解释如何通过 Maven 使用 commons email api


    公共电子邮件

    Commons Email 旨在提供一个用于发送电子邮件的 API。它建立在 Java Mail API 之上,旨在简化。

    提供的一些邮件类如下:

    • SimpleEmail - 此类用于发送基于文本的基本电子邮件。
    • MultiPartEmail - 此类用于发送多部分消息。这允许带有内嵌或附加附件的文本消息。
    • HtmlEmail - 此类用于发送 HTML 格式的电子邮件。它具有 MultiPartEmail 的所有功能,允许轻松添加附件。它还支持嵌入图像。
    • ImageHtmlEmail - 此类用于发送带有内嵌图像的 HTML 格式的电子邮件。它具有 HtmlEmail 的所有功能,但会将所有图像引用转换为内联图像。
    • EmailAttachment - 这是一个简单的容器类,可以轻松处理附件。它用于 MultiPartEmail 和 HtmlEmail 的实例。

    • Maven 依赖

      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-email</artifactId>
          <version>1.5</version>
      </dependency>
      
    • 代码块:

      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===");
          }
      }
      
    • 控制台输出:

      ===Test for Sending CommonsEmail started===
      ===Test for Sending CommonsEmail ended===
      
    • 快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2018-11-21
      相关资源
      最近更新 更多