我想在selenium TestNG java中执行Suite后发送电子邮件?

问题描述 投票:0回答:1

我有一个testSuite,我在其中运行ForgotPasswordTest.java类。在套件执行结束时,我想发送一封使用AfterSuite方法生成的可通过电子邮件发送报告的电子邮件。我该怎么做?

这是我的套房:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="false" verbose="2">
  <listeners>
    <listener class-name="utility.ScreenshotOnFailure" />
    <listener class-name="org.uncommons.reportng.HTMLReporter" />
  </listeners> <test name="Run on Chrome">
    <parameter name="browser" value="chrome"/>
    <parameter name="grid" value="yes"></parameter>
    <parameter name="baseURL" value="https://dev.miride.com/"/>
    <classes>
      <!-- <class name="test.LoginPageTest"/> -->
      <!-- <class name="test.SignUpPageTest"/> -->
      <class name="test.ForgotPasswordTest"/>
     <!--  <class name="test.miAccountPageTest"/> -->
    </classes>
  </test>
</suite>

我的ForgotPasswordTest.java类如下:

package test;
import org.testng.annotations.Test;
import core.OpenAndCloseBrowser;
import dataProvider.DataProviderForForgotPasswordPage;
import email.emailReport;
import pages.ForgotPasswordPage;
import pages.HomePage;
import pages.LoginPage;
import org.testng.annotations.AfterSuite;
public class ForgotPasswordTest extends OpenAndCloseBrowser{

    //TC#1 - Test case to request for new password reset link
    @Test(dataProviderClass=DataProviderForForgotPasswordPage.class,dataProvider="ForgotPassword_TC1")
    public void ForgotPassword_TC1(String emailaddress) throws Exception{
    HomePage homePage = new HomePage(getDriver());
    LoginPage loginPage = homePage.clickLoginLink();
    ForgotPasswordPage fppage = loginPage.clickForgotPasswordLink();
    fppage.enterEmailAddress(emailaddress);
    fppage.clickResetPasswordBtn();
    }

    //TC#2 - Test case to verify validation message in case of clearing email address field
    @Test(dataProviderClass=DataProviderForForgotPasswordPage.class,dataProvider="ForgotPassword_TC2")
    public void ForgotPassword_TC2(String emailaddress, String validation) throws Exception{
    HomePage homePage = new HomePage(getDriver());
    LoginPage loginPage = homePage.clickLoginLink();
    ForgotPasswordPage fppage = loginPage.clickForgotPasswordLink();
    fppage.enterEmailAddress(emailaddress);
    fppage.clear_EmailAddress(emailaddress);
    fppage.verify_ValidationMessage(validation);
    }

    //TC#3 - Test case to verify validation message in case of unregistered email address
    @Test(dataProviderClass=DataProviderForForgotPasswordPage.class,dataProvider="ForgotPassword_TC3")
    public void ForgotPassword_TC3(String emailaddress, String validation) throws Exception{
    HomePage homePage = new HomePage(getDriver());
    LoginPage loginPage = homePage.clickLoginLink();
    ForgotPasswordPage fppage = loginPage.clickForgotPasswordLink();
    fppage.enterEmailAddress(emailaddress);
    fppage.clickResetPasswordBtn();
    fppage.verify_ValidationMessage(validation);
    }

    //TC#4 - Test case to verify validation message in case of invalid email address
    @Test(dataProviderClass=DataProviderForForgotPasswordPage.class,dataProvider="ForgotPassword_TC4")
    public void ForgotPassword_TC4(String emailaddress, String validation) throws Exception{
    HomePage homePage = new HomePage(getDriver());
    LoginPage loginPage = homePage.clickLoginLink();
    ForgotPasswordPage fppage = loginPage.clickForgotPasswordLink();
    fppage.enterEmailAddress(emailaddress);
    fppage.verify_ValidationMessage(validation);
    }

    @AfterSuite
    public void sendMail(){
        emailReport.execute();
    }
}

另外,我的Email Report.java如下:

package email;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.testng.annotations.AfterSuite;

public class emailReport {


public static void execute() {

    // 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("[email protected]", "xyz");

                }

            });

    try {

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

        // Set the from address
        message.setFrom(new InternetAddress("[email protected]"));

        // Set the recipient address
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]"));

                    // Add the subject link
        message.setSubject("Testing Subject");

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

        // Set the body of email
        messageBodyPart1.setText("This is message body");

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

        // Mention the file which you want to send
        String filename = "./test-output/Suite/emailable-report.html";

        //String filename = System.getProperty("emailable-report.html") + "./test-output/Suite/emailable-report.html";//"./test-output/Suite/emailable-report.html";
     /* \\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) {
        System.out.println("=====Email failed=====");
        throw new RuntimeException(e);

    }

}

}

对不起长代码。最后,控制台显示:

Suite
Total tests run: 4, Failures: 0, Skips: 0
Configuration Failures: 1, Skips: 0

我有什么办法可以纠正我的代码吗?

java selenium email testng
1个回答
0
投票

内置的TestNG报告即emailable-report.html由TestNG中的报告监听器生成。

默认情况下,TestNG执行报告阶段(即,仅在所有套件运行完成后调用org.testng.IReporter#generateReport接口实现的org.testng.IReporter方法。

使用@AfterSuite注释的方法在套件运行完成之前执行,这意味着它在emailable-report.html生成之前执行。

这也许就是为什么你看到配置失败,即你的@AfterSuite注释方法失败。

要解决你的问题,你应该创建一个新的记者,实现org.testng.IExecutionListener接口,并在其org.testng.IExecutionListener#onExecutionFinish中调用emailReport.execute();

这应该可以帮助您完成所需的工作。

© www.soinside.com 2019 - 2024. All rights reserved.