1

Please find the code below i am using ExtentReport 3.0.6 and selenium 3.4.0. Screenshots are not saving but i am not getting any exceptions. It's not saving any screenshots. I am not sure why it's happening it was working for me couple of days back.

ExtentHtmlReporter htmlReporter;
ExtentReports extent;
ExtentTest test;
WebDriver driver;


@BeforeTest
public void setUp()
{
    //where we need to generate the report
    htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir")+"/test-output/MyReport.html");
    extent = new ExtentReports();
    extent.attachReporter(htmlReporter);

    // Set our document title, theme etc..
    htmlReporter.config().setDocumentTitle("My Test Report");
    htmlReporter.config().setReportName("Test Report");
    htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
    htmlReporter.config().setTheme(Theme.DARK); 
}

@Test
public void demoReportPass()
{
    test = extent.createTest("demoScreenshotTest");
    System.setProperty("webdriver.chrome.driver","C:\\SeleniumServer\\chromedriver.exe");
    driver=new ChromeDriver();      
    driver.get("https://www.google.com/");
    String title = driver.getTitle();
    Assert.assertEquals(title, "Goo");

}

@AfterMethod
public void getResult(ITestResult result) throws IOException
{
    if(result.getStatus()==ITestResult.FAILURE)
    {
        String screenshotPath = capture(driver, "screenshotname");
        //String screenshotPath = GetScreenshot.captureFullPage(driver, "screenshotname");
        test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + "Test Case failed due to below issues", ExtentColor.RED));
        test.fail(result.getThrowable());
        test.fail("Snapshot below: " + test.addScreenCaptureFromPath(screenshotPath));          
    }               
}

@AfterSuite
public void tearDown()
{
    extent.flush();
    driver.quit();
}

public String capture(WebDriver driver, String screenShotName) throws IOException
{
    TakesScreenshot ts = (TakesScreenshot)driver;
    File source = ts.getScreenshotAs(OutputType.FILE);      
    String dest = "../test-output/"+screenShotName+".png";
    File destination = new File(dest);
    FileUtils.copyFile(source, destination);                          
    return dest;        
}
Craig
  • 7,471
  • 4
  • 29
  • 46
testerwaitz
  • 103
  • 2
  • 9

1 Answers1

0

You are returning dest (destination path) from capture method. And your dest is defined as

     String dest = "../test-output/"+screenShotName+".png"; 

This will return hard coded "../test-output/" and does not resolve .. as 'one directorty above current directory'

Use getCanonicalPath() method which returns the canonical pathname string of this abstract pathname. getCanonicalPath() method typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

In your case, you can use getCanonicalPath() to return dest as below -

     public String capture(WebDriver driver, String screenShotName) throws IOException
     {
       TakesScreenshot ts = (TakesScreenshot)driver;
       File source = ts.getScreenshotAs(OutputType.FILE);  

       File file = new File("../test-output/"+screenShotName+".png");
       String dest = file.getCanonicalPath();
       File destination = new File(dest);
       FileUtils.copyFile(source, destination);                          
       return dest;        
     } 
Vikas Piprade
  • 272
  • 2
  • 7