TestNG 未发送最新的可发送电子邮件报告 .html

TestNG is not sending the latest emailable-report.html

提问人:Ahmed Bilal 提问时间:12/24/2019 更新时间:1/9/2020 访问量:888

问:

@Aftertest @AfterSuite @AfterClass 就我而言,当我们谈论通过电子邮件发送最新报告时,没有任何帮助。

@AfterSuite
    public void statusupdate() throws Exception 
    {
                SendMail.execute();
    }

这就是我正在做的事情,每次我得到我的 emailable-report.html 的旧版本时,我也在为 JAVA 而苦苦挣扎,所以希望有人可以帮助我理解这里的问题。我的假设是,我在生成新报告之前发送电子邮件,但不知道下一步该怎么做。感谢您的耐心等待和回复。

Selenium 测试 浏览器自动化

评论

0赞 Rodrigo Vaamonde 12/24/2019
嗨,艾哈迈德,欢迎来到 SO。SendMail.execute() 有什么作用,它从何而来?
0赞 Ahmed Bilal 12/26/2019
嗨,Rodrigo,这是我框架中的另一个类,其中编写了电子邮件发送代码,因此在我的测试类中,在aftersuite中,我调用了SendMail的Execute方法。

答:

1赞 Rodrigo Vaamonde 12/27/2019 #1

你能做的最好的事情就是用 Listeners for reports 来帮助自己,而不是@afterXxxx方法。

因此,您可以执行以下操作:

导入 org.testng.annotations.Listeners;

@Listeners(TestListener.class)
public class MyTestClass {

    @Test
    public void myTest {
        doTest();
    }

}

侦听器在 onFinish 方法中具有 SendMail 操作的位置:

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener {

    @Override
    public void onTestStart(ITestResult result) {
    }

    @Override
    public void onTestSuccess(ITestResult result) {
    }

    @Override
    public void onTestFailure(ITestResult result) {
    }

    @Override
    public void onTestSkipped(ITestResult result) {
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    }

    @Override
    public void onStart(ITestContext context) {
    }

    @Override
    public void onFinish(ITestContext context) {
        SendMail.execute();
    }

}

我希望这能满足您的需求,如果没有让我知道。

评论

2赞 Ahmed Bilal 12/27/2019
谢谢罗德里戈,我也会尝试这样做,同时暂时我已经管理了一个解决方案,其中我已经从@AfterSuite中删除了电子邮件发送方法调用,因为它总是向我发送旧报告,现在我正在运行 SendMail.java在测试类执行完成后。通过在测试执行后单独运行这个类,向我发送了最新的 emailable-report.html.感谢您联系我 Rodrigo :)
1赞 Ahmed Bilal 1/1/2020
Rodrigo,正如你所说,如果听众没有帮助,我会添加我的评论,因为我已经创建了侦听器类,并且@Override公共void onFinish(ITestContext context) { System.out.println(“测试完成”);字符串路径=“mypath\\emailable-report.html”;SendMail.execute(路径);} 但这仍然向我发送了旧报告,所以我只是想与您分享,这仍然将其视为测试执行的一部分。希望您能回到这里进行建设性的讨论:)因为我仍然依赖单独执行 sendmail 类的旧解决方案。
0赞 Rodrigo Vaamonde 1/2/2020
将在今天和明天之间查看,但我想知道为什么要给你一个旧报告,因为该方法是在所有测试完成后执行的。在所有测试之后,是否发生了一些未报告的事情?
0赞 Ahmed Bilal 1/2/2020
当然,谢谢,同时希望这能帮助您更好地了解这里出了什么问题 github.com/ahmedbilal-code/TestAutomation/tree/master/NerdBox1 非常感谢您抽出宝贵时间!
0赞 Ahmed Bilal 1/9/2020 #2

最后,我能够通过在我的框架中实现范围报告和 TestNG 侦听器来获取最新的 .html 报告。我正在使用 7.0 TestNG,它的局限性是它不会更新 emailable-report.html,因此为了克服这个问题,您将不得不实现范围报告,您将始终获得最新的 .html 花哨报告,您可以在侦听器类的 onFinish 方法中通过电子邮件发送。但在跳进去之前,你必须了解听众和范围报告,休息在下面给出。也感谢@rodrigo给我一个方向:)

公共类 TestClass{

WebDriver driver=null;
public ExtentHtmlReporter htmlReporter;
 public ExtentReports extent;
 public ExtentTest test;


 public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
      String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date(0));
      TakesScreenshot ts = (TakesScreenshot) driver;
      File source = ts.getScreenshotAs(OutputType.FILE);

      // after execution, you could see a folder "FailedTestsScreenshots" under src folder
      String destination = System.getProperty("user.dir") + "/Screenshots/" + screenshotName + dateName + ".png";
      File finalDestination = new File(destination);
      FileUtils.copyFile(source, finalDestination);
      return destination;
     }

@BeforeTest()
@Parameters("browser")
public void setup(String browsername) {
    if(browsername.equalsIgnoreCase("chrome")) 
    {
        WebDriverManager.chromedriver().setup();
        driver=new ChromeDriver();

    }
    else if(browsername.equalsIgnoreCase("firefox"))
    {
        WebDriverManager.firefoxdriver().setup();
        driver=new FirefoxDriver();

    }

     htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir")+"\\test-output\\myReport.html");

      htmlReporter.config().setDocumentTitle("Automation Report"); // Tile of report
      htmlReporter.config().setReportName("Functional Testing"); // Name of the report
      htmlReporter.config().setTheme(Theme.DARK);

      extent = new ExtentReports();
      extent.attachReporter(htmlReporter);

      // Passing General information
      extent.setSystemInfo("Host name", "NerdBox");
      extent.setSystemInfo("Environemnt", "QA");
      extent.setSystemInfo("user", "AB");
}



@Test(priority=1,enabled=true)
public void SubmitQuestions() throws Exception {


    driver.manage().window().maximize();
    driver.get("https://com/questions");
    driver.findElement(By.xpath("/html/body/div/div/div[3]/div/div/div[1]/div/div/div[2]/div")).click();
}
 @AfterMethod(alwaysRun=true)
 public void end(ITestResult result) throws IOException
 {
     test= extent.createTest("The Test is "+result.getName());


        if (result.getStatus() == ITestResult.FAILURE) {

          test.log(Status.FAIL, "TEST CASE FAILED IS " + result.getName()); // to add name in extent report
          test.log(Status.FAIL, "TEST CASE FAILED IS " + result.getThrowable()); // to add error/exception in extent report
          String screenshotPath = NerdboxJobSubmit.getScreenshot(driver, result.getName());
           test.addScreenCaptureFromPath(screenshotPath);// adding screen shot
          } else if (result.getStatus() == ITestResult.SKIP) {
           test.log(Status.SKIP, "Test Case SKIPPED IS " + result.getName());
          }
          else if (result.getStatus() == ITestResult.SUCCESS) {
           test.log(Status.PASS, "Test Case PASSED IS " + result.getName());
          }


 }

@AfterTest(alwaysRun=true)
public void flush() throws Exception 
{
    System.out.println("After test");
    extent.flush();

    driver.quit();

}