简体   繁体   中英

Screenshots not created under test-output folder in TestNG, Selenium

I am new to Selenium WebDriver. I am using the following method to take screenshots of failed tests in Selenium, TestNG. After execution, the testNG report shows the failed tests perfectly and also the imageFile outputfile is successfully created, but no screenshots appear under test-output folder. When I manually checked, I found that the screenshots were being created outside the test-output folder, in the project folder. How do I rectify this, so that the screenshots appear under test-output folder?

@AfterMethod 
public void closeBrowser(ITestResult result) throws IOException {
    if (!result.isSuccess()){
        File imageFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String failureImageFileName = result.getMethod().getMethodName()+new SimpleDateFormat("MM-dd-yyyy_HH-ss").format(new GregorianCalendar().getTime()) + ".png";
        File failureImageFile = new File(failureImageFileName);
        FileUtils.copyFile(imageFile, failureImageFile);

    }
public void closeBrowser(ITestResult result) throws IOException {
    if (!result.isSuccess()){
        File imageFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String failureImageFileName = result.getMethod().getMethodName()+new SimpleDateFormat("MM-dd-yyyy_HH-ss").format(new GregorianCalendar().getTime()) + ".png";

        // It will store all the screenshots in test-output/screenshots folder
        String destDir = System.getProperty("user.dir")) + "/" + "test-output/screenshots";
        new File(destDir ).mkdirs();
        String destFile = destDir + "/" + failureImageFileName;
        FileUtils.copyFile(imageFile, new File(destFile ));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM