How can I include a failure screenshot to the testNG report

前端 未结 2 826
长情又很酷
长情又很酷 2021-01-02 13:20

currently I am taking screenshots of my test failures this way:

@AfterMethod(alwaysRun=true)
public void catchExceptions(ITestResult result){
    Calendar ca         


        
2条回答
  •  半阙折子戏
    2021-01-02 13:39

    Customizing a bit of ExtentReport can give extremely useful report having exception+screenshot captured exactly at time of test failure . Screenshot can be placed alongside exception which user can use to know what was website doing when error occurred.

    Report Example Test

          @Test (enabled=true)                           
            public void verifySearch() {
           extentlogger = extent.createTest("To verify verifySearch");
          //Your other code here.....
            soft.assertEquals("xxx", "xxxx");
            soft.assertAll();
          }
    

    AfterMethod

         @AfterMethod
         public void getResult(ITestResult result) throws Exception{
          if(result.getStatus() == ITestResult.FAILURE)
         {
            extentlogger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + 
            " - Test Case Failed", ExtentColor.RED));
        
            try {
            // get path of captured screenshot using custom failedTCTakeScreenshot method
            String screenshotPath = failedTCTakeScreenshot( result);
            extentlogger.fail("Test Case Failed Snapshot is below " + 
          extentlogger.addScreenCaptureFromPath(screenshotPath));
           } catch (InterruptedException e) {
            e.printStackTrace();
            }
          }
         }
    

提交回复
热议问题