如何在Selenium WebDriver中针对失败的测试案例生成电子邮件?

时间:2018-08-02 15:20:44

标签: selenium selenium-webdriver

无法在Selenium Web驱动程序中的失败测试用例上生成电子邮件,已经生成了通用电子邮件(通过/失败测试用例),并且它将在TestNG框架中的测试套件末尾发送电子邮件,但是这里的要求是仅在测试用例失败的情况下发送电子邮件。

这是电子邮件的代码段:

    String to="smimran@macrosoftinc.com";
          final String username = "username";
          final String password = "password";

          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", "smtp.gmail.com");
          props.put("mail.smtp.port", "587");

          Session session = Session.getInstance(props, new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }
          });

          try {

              Message message = new MimeMessage(session);
              message.setFrom(new InternetAddress("syed.imran3411@gmail.com"));
              message.setRecipients(Message.RecipientType.TO,
                  InternetAddress.parse(to));
              message.setSubject("A testing mail header !!!");
              message.setText("Dear Mail Crawler,"
                  + "\n\n No spam to my email, please!");


              MimeBodyPart messageBodyPart = new MimeBodyPart();

              Multipart multipart = new MimeMultipart();

              messageBodyPart = new MimeBodyPart();
              String file = "F:\\Java Selenium\\WorkPlace\\Velco Automation Testing\\test-output\testng.css";
              String fileName = "testng.css";
              DataSource source = new FileDataSource(file);
              messageBodyPart.setDataHandler(new DataHandler(source));
              messageBodyPart.setFileName(fileName);
              multipart.addBodyPart(messageBodyPart);

              Thread.sleep(5000);

              message.setContent(multipart);

              Transport.send(message);

              System.out.println("Done");
}
}

请给我建议解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

我建议使用Jenkins并通过email plugin发送邮件。