【问题标题】:TestNG is not sending the latest emailable-report.htmlTestNG 没有发送最新的 emailable-report.html
【发布时间】:2020-04-15 12:05:50
【问题描述】:

@Aftertest @AfterSuite @AfterClass 当我们谈论在电子邮件中发送最新报告时,对我来说没有任何帮助。

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

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

【问题讨论】:

  • 嗨,艾哈迈德,欢迎来到 SO。 SendMail.execute() 是做什么的,它是从哪里来的?
  • 嗨 Rodrigo,它是我框架中的另一个类,其中编写了电子邮件发送代码,所以在我的后套件的测试类中,我正在调用 SendMail 的 Execute 方法。

标签: selenium testing testng browser-automation


【解决方案1】:

您能做的最好的事情是使用Listeners 来帮助自己进行报告,而不是使用@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();
    }

}

我希望这能满足您的需要,如果不告诉我。

【讨论】:

  • 谢谢罗德里戈,我也会尝试尝试这个,同时我暂时管理了一个解决方案,其中我从@AfterSuite 删除了电子邮件发送方法调用,因为它总是向我发送旧报告,现在我' m 在测试类执行完成后运行 SendMail.java。通过在测试执行后单独运行此类将向我发送 emailable-report.html 的最新副本。再次感谢您联系我 Rodrigo :)
  • Rodrigo 正如你所说,让你知道听众是否没有帮助,所以我添加我的评论,因为我已经创建了监听器类并做了这个 @Override public void onFinish(ITestContext context) { System.out. println("测试结束");字符串路径="mypath\\emailable-report.html"; SendMail.execute(路径); } 但这仍然给我发送了旧报告,所以我只是想与你分享它,这仍然认为它是测试执行的一部分。希望您能回到这里进行建设性的讨论 :) 因为我仍然依赖于单独执行 sendmail 类的旧解决方案。
  • 今天和明天之间会看看,但我想知道为什么给你一个旧报告,因为该方法是在所有测试完成后执行的。在所有未报告的测试之后发生了什么事吗?
  • 当然感谢,同时希望这将帮助您更好地了解这里出了什么问题github.com/ahmedbilal-code/TestAutomation/tree/master/NerdBox1非常感谢您的宝贵时间!
【解决方案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();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2019-09-07
    • 2022-01-09
    相关资源
    最近更新 更多