【问题标题】:How to write send email test case and read Excel file test case in Selenium如何在 Selenium 中编写发送电子邮件测试用例和读取 Excel 文件测试用例
【发布时间】:2018-05-15 07:52:14
【问题描述】:

这是我编写示例发送电子邮件测试用例的代码。运行代码时电子邮件不会被触发。详情请查看附件数据

  1. Login User details
  2. Invalid User Details
  3. Booking Data

任何人都可以帮助解决这个问题,因为我是硒自动化测试的新手。下面是我的 Java 代码的示例代码,用于配置和触发电子邮件。

如何使用 Java 在 Selenium webdriver 中发送电子邮件通知,每当某些场景在两者之间失败/传递时?


public class SendEmail {
public SendEmail() {
    }

    public void email() {

        // Create object of Property file
        Properties props = new Properties();

        // this will set host of server- you can change based on your
        // requirement
        props.put("mail.smtp.host", "smtp.gmail.com");

        // set the port of socket factory
        props.put("mail.smtp.socketFactory.port", "465");

        // set socket factory
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        // set the authentication to true
        props.put("mail.smtp.auth", "true");

        // set the port of SMTP server
        props.put("mail.smtp.port", "465");

        // This will handle the complete authentication
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("seleniumtest201@gmail.com", "Admin12!@");
            }
        });
        try {

            // Create object of MimeMessage class
            Message message = new MimeMessage(session);

            // Set the from address
            message.setFrom(new InternetAddress("seleniumtest201@gmail.com"));

            // Set the recipient address
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("aniketgupta1993@gmail.com"));

            // Add the subject link
            message.setSubject("Test Case Execution Report");

            // Create object to add multi media type content
            BodyPart messageBodyPart1 = new MimeBodyPart();

            // Set the body of email
            messageBodyPart1.setText("This is auto-generated test case execution report");

            // Create another object to add another content
            MimeBodyPart messageBodyPart2 = new MimeBodyPart();

            // Mention the file which you want to send
            String filename = "C://Users//aniket//sampleseleniumproject//test-output//emailable-report.html";

            // Create data source and pass the filename
            DataSource source = new FileDataSource(filename);

            // set the handler
            messageBodyPart2.setDataHandler(new DataHandler(source));

            // set the file
            messageBodyPart2.setFileName(filename);

            // Create object of MimeMultipart class
            Multipart multipart = new MimeMultipart();

            // add body part 1
            multipart.addBodyPart(messageBodyPart2);

            // add body part 2
            multipart.addBodyPart(messageBodyPart1);

            // set the content
            message.setContent(multipart);

            // finally send the email
            Transport.send(message);

            System.out.println("=====Email Sent=====");

        } catch (MessagingException e) {

            throw new RuntimeException(e);
        }
    }
}

【问题讨论】:

    标签: selenium-webdriver sendmail excel-reader


    【解决方案1】:

    您可以使用以下代码在执行完成时获取电子邮件并将其放入拆解中,这样您就可以在 selenium webdriver 中收到电子邮件通知,每当场景失败/在两者之间传递时

    public void tearDown()
    
    {
    
    private static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) 
    
    {
    
    Properties props = System.getProperties();
    
    String host = "smtp.gmail.com";
    
    props.put("mail.smtp.starttls.enable", "true");
    
    props.put("mail.smtp.host", host);
    
    props.put("mail.smtp.user", from);
    
    props.put("mail.smtp.password", pass);
    
    props.put("mail.smtp.port", "587");
    
    props.put("mail.smtp.auth", "true");
    
    Session session = Session.getDefaultInstance(props);
    
    MimeMessage message = new MimeMessage(session);
    
    try {
    
        //Set from address
    
    message.setFrom(new InternetAddress(from));
    
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    
    //Set subject
    
    message.setSubject(subject);
    
    message.setText(body);
    
    BodyPart objMessageBodyPart = new MimeBodyPart();
    
    objMessageBodyPart.setText("Please Find The Attached Report File!");
    
    Multipart multipart = new MimeMultipart();
    
    multipart.addBodyPart(objMessageBodyPart);
    
    objMessageBodyPart = new MimeBodyPart();
    
    //Set path to the pdf report file
    
    String filename = System.getProperty("user.dir")+"\\Default test.pdf";
    
    //Create data source to attach the file in mail
    
    DataSource source = new FileDataSource(filename);
    
    objMessageBodyPart.setDataHandler(new DataHandler(source));
    
    objMessageBodyPart.setFileName(filename);
    
    multipart.addBodyPart(objMessageBodyPart);
    
    message.setContent(multipart);
    
    Transport transport = session.getTransport("smtp");
    
    transport.connect(host, from, pass);
    
    transport.sendMessage(message, message.getAllRecipients());
    
    transport.close();
    
    }
    
    catch (AddressException ae) {
    
    ae.printStackTrace();
    
    }
    
    catch (MessagingException me) {
    
    me.printStackTrace();
    
    }
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多