2

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

4

3 回答 3

1

以下代码将允许您使用 JAVA 发送邮件。在 selenium webdriver 代码中创建一个函数并在 Pass/Fail 场景后调用它。

final String username = "YourEmail";
final String password = "YourPassword";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "mail.example.com");
props.put("mail.smtp.port", "25");
props.put("java.net.preferIPv4Stack", "true");
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("YourEmail"));
message.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse("Emailid to which you want to send Report"));
message.setSubject("Email Subject");



BodyPart messageBodyPart1 = new MimeBodyPart();  
messageBodyPart1.setText("Body text);  

//4) create new MimeBodyPart object and set DataHandler object to this object      
MimeBodyPart messageBodyPart2 = new MimeBodyPart();  

String filename = "File path if you want to attach in mail";//change accordingly  
DataSource source = new FileDataSource(filename);  
messageBodyPart2.setDataHandler(new DataHandler(source));  
messageBodyPart2.setFileName(filename);  


//5) create Multipart object and add MimeBodyPart objects to this object      
Multipart multipart = new MimeMultipart();  
multipart.addBodyPart(messageBodyPart1);  
multipart.addBodyPart(messageBodyPart2);  

//6) set the multiplart object to the message object  
message.setContent(multipart );  
Transport.send(message);

System.out.println("Mail Sent Successfully");

 } catch (MessagingException e) {
throw new RuntimeException(e);
}

注意:请根据您的电子邮件系统配置更改主机、电子邮件详细信息和端口。

于 2015-12-29T12:45:08.583 回答
0

在实用程序类文件中编写了一个方法,并从测试用例失败的情况下调用它。

public static void sendEmail(string message, string testCaseName)
{
MailMessage mail = new MailMessage();
mail.To.Add("your-to-email-address-goes-here");
mail.From = new MailAddress("your-from-email-address-goes-here ");
mail.Subject = "your-mail-subject-goes-here";
mail.Body = "Test Case Name: " + testCaseName;
mail.Body += Environment.NewLine;
mail.Body += message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Port = 25;
smtp.Send(mail);
}

当它应该失败时,您可以在测试用例中调用此方法。将通过电子邮件通知用户。

**使用正确的 SMTP 配置

于 2015-12-29T12:39:39.617 回答
0

这是我通过 javax.mail 发送电子邮件的示例:

pom依赖:

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.5.0-b01</version>
    </dependency>

1. STEP (model MailMessage) 你需要邮件消息的model类:

    public class MailMessage {
    private String from;
    private List<String> to;
    private List<String> cc;
    private List<String> bcc;
    private String subject;
    private Date sendDate;
    private String flags;
    private String content;
    private List<File> attachments;

    public MailMessage() {
    }


    public MailMessage(String from, String to, String subject, String content) 
        List<String> listTo = new ArrayList<>();
        List<String> listCC = new ArrayList<>();
        List<String> listBCC = new ArrayList<>();
        listTo.add(to);
        new MailMessage(from, listTo, listCC, listBCC, subject, content);
    }

    public MailMessage(String from, String to, String cc, String subject, String content) {
        List<String> listTo = new ArrayList<>();
        List<String> listCC = new ArrayList<>();
        List<String> listBCC = new ArrayList<>();
        listTo.add(to);
        listCC.add(cc);
        new MailMessage(from, listTo, listCC, listBCC, subject, content);

    }

    public MailMessage(String from, List<String> to, List<String> cc, List<String> bcc, String subject, String content) {
        setFrom(from);
        setTo(to);
        setCc(cc);
        setBcc(bcc);
        setSubject(subject);
        setContent(content);
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public List<String> getTo() {
        return to;
    }

    public void setTo(List<String> to) {
        this.to = to;
    }

    public List<String> getCc() {
        return cc;
    }

    public void setCc(List<String> cc) {
        this.cc = cc;
    }

    public List<String> getBcc() {
        return bcc;
    }

    public void setBcc(List<String> bcc) {
        this.bcc = bcc;
    }


    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public Date getSendDate() {
        return sendDate;
    }

    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate;
    }

    public String getFlags() {
        return flags;
    }

    public void setFlags(String flags) {
        this.flags = flags;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String fieldMessage) {
        this.content = fieldMessage;
    }

    public List<File> getAttachments() {
        return attachments;
    }

    public void setAttachments(List<File> attachments) {
        this.attachments = attachments;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("-------------------------------------\n");
        sb.append("FROM: " + getFrom() + "\n");
        sb.append("TO: " + getTo() + "\n");
        sb.append("Subject: " + getSubject() + "\n");
        sb.append("Send Date: " + getSendDate() + "\n");
        sb.append("Flags: " + getFlags() + "\n");
        sb.append("Messages: " + getContent() + "\n");
        sb.append("-------------------------------------\n");
        return sb.toString();
    }
}

2. STEP(创建消息,填充模型)

protected MailMessage createMessage(String to, String subject, String content) {
    List<String> listTo = new ArrayList<>();
    List<String> listCC = new ArrayList<>();
    List<String> listBCC = new ArrayList<>();
    listTo.add(to);
    return createMessage(listTo, listCC, listBCC, subject, content);
}

protected MailMessage createMessage(List<String> to, List<String> cc, List<String> bcc, String subject, String content) {
    MailMessage mailMessage = new MailMessage();
    mailMessage.setFrom(getUsername());
    mailMessage.setTo(to);
    mailMessage.setCc(cc);
    mailMessage.setBcc(bcc);
    mailMessage.setSubject(subject);
    mailMessage.setContent(content);
    return mailMessage;
}

3. 通过模型(MailMessage)发送消息的STEP方法:

public boolean sendMessage(MailMessage mailMessage) {

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(getSession());

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(mailMessage.getFrom()));

        // Set To: header field of the header.
        for (String fieldTo : mailMessage.getTo()) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(fieldTo));
        }

        for (String fieldCc : mailMessage.getCc()) {
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(fieldCc));
        }

        for (String fieldBcc : mailMessage.getBcc()) {
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(fieldBcc));
        }

        // Set Subject: header field
        message.setSubject(mailMessage.getSubject());

        // Now set the actual message
        message.setText(mailMessage.getContent());


        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(mailMessage.getContent(), "text/html");

        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // adds attachments
        if (mailMessage.getAttachments() != null && mailMessage.getAttachments().size() > 0) {
            for (File filePath : mailMessage.getAttachments()) {
                MimeBodyPart attachPart = new MimeBodyPart();

                try {
                    attachPart.attachFile(filePath.getAbsolutePath());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                multipart.addBodyPart(attachPart);
            }
        }

        // sets the multi-part as e-mail's content
        message.setContent(multipart);


        // Send message
        Transport.send(message);
        return true;

    } catch (MessagingException mex) {
        mex.printStackTrace();
        return false;
    }
}

希望这会有所帮助,

于 2018-08-02T09:19:43.897 回答