screenshotselenium-extent-report

extent report version 4 screenshot not generating


I simply do not understand why this error occurs. Unable to generate the extent report and capture screenshot. I have appended the Test, AfterMethod & AfterTest and screenshot method. I want to capture screenshots under a different folder.

The error message i am getting is as following.

FAILED CONFIGURATION: @AfterMethod getReport([TestResult name=checkTitle status=SUCCESS method=Google.checkTitle()[pri:0, instance:Google@3224f60b] output={null}]) java.io.IOException: The filename, directory name, or volume label syntax is incorrect hod.

Following is my selenium test code.

@Test
public void checkTitle() {
    test = extent.createTest("Title Check test");
    Actual_Title = driver.getTitle();
    System.out.println(Actual_Title);
    Assert.assertEquals(Actual_Title, Expected_Tite);
    test.log(Status.PASS, "This Test is Passed!");
}

@AfterMethod
public void getReport(ITestResult result) throws IOException {
    if (result.getStatus() == ITestResult.FAILURE) {
        test.log(Status.FAIL,MarkupHelper.createLabel(result.getMethod().getMethodName(),ExtentColor.RED));
        test.fail("TestFailed due to"+"  "+result.getThrowable().getMessage());
        String imagePath =Google.captureScreen(driver,result.getName());
        test.addScreenCaptureFromPath(imagePath);
    }
    else if (result.getStatus() == ITestResult.SKIP) {
        test.log(Status.SKIP,MarkupHelper.createLabel(result.getMethod().getMethodName(),ExtentColor.AMBER));
        test.skip(result.getThrowable().getMessage());
    } else {
        test.log(Status.PASS,MarkupHelper.createLabel(result.getMethod().getMethodName(),ExtentColor.GREEN));
        String imagePath =Google.captureScreen(driver, result.getName());
        test.addScreenCaptureFromPath(imagePath);
    }

}

@AfterTest
public void endTest() {
    extent.flush();
    driver.quit();
}

public static String captureScreen(WebDriver driver,String screenshotname)throws IOException {
    String date = new SimpleDateFormat("MM-dd-yy,hh:mm:ss").format(new Date());
    TakesScreenshot ts = (TakesScreenshot)driver;
    File src= ts.getScreenshotAs(OutputType.FILE);
    String path = System.getProperty("user.dir")+"/ScreenShots/"+screenshotname+date+".png";
    File destination = new File(path);
    FileUtils.copyFile(src, destination);
    return path;
}

Solution

  • I could reproduce your problem. For below code, I am getting exact same error as you mentioned.

            String date = new SimpleDateFormat("MM-dd-yy,hh:mm:ss").format(new Date());
            String screenshotname = "MyScreenshot";
            String path = System.getProperty("user.dir")+"/test- 
            output/"+screenshotname+date+".png";
            logger.info("In failedTCTakeScreenshot:: ******path***** " + path);
    

    Error in log

          [INFO ] 2020-07-28 23:09:52.281 [main] com.resources.BaseTest -In 
          failedTCTakeScreenshot:: ******path***** 
          E:\Automation\EclipseWorkspace\AutomationPrj/test-output/MyScreenshot07-28- 
          20,11:09:52.png
    
          [ERROR] 2020-07-28 23:09:52.285 [main] com.resources.BaseTest - 
          BaseTest::failedTCTakeScreenshot::***In Catch Block *** java.io.IOException: The 
          filename, directory name, or volume label syntax is incorrect
    

    Root cause Analysis:-

    On analysis , I found that, you are storing your screenshot having name MM-dd-yy,hh:mm:ss date format in it. Windows does not accept any : or , in filename. You need to change your screenshotname so that it does not contains : , etc.

    When I change DateFormat in my code to MM-dd-yyyy , I could successfully copy screenshot and no error thrown.

            String date = new SimpleDateFormat("MM-dd-yyyy").format(new Date());
            String screenshotname = "MyScreenshot";
            String path = System.getProperty("user.dir")+"/test- 
            output/"+screenshotname+date+".png";
            logger.info("In failedTCTakeScreenshot:: ******path***** " + path);
    

    Log :-

          [INFO ] 2020-07-28 23:24:27.457 [main] com.resources.BaseTest - In 
          failedTCTakeScreenshot:: ******path***** 
          E:\Automation\EclipseWorkspace\AutomationPrj/test-output/MyScreenshot07-28- 
          2020.png
    
          [INFO ] 2020-07-28 23:24:27.463 [main] com.resources.BaseTest - In 
          failedTCTakeScreenshot:: ******path destination***** 
          E:\Automation\EclipseWorkspace\AutomationPrj\test-output\MyScreenshot07-28- 
          2020.png
    

    So you just need to change Screenshotname so that it should not contains special characters like ; and ,