Sameera De Silva
Sameera De Silva

Reputation: 1980

Screenshot is not attached to the extend report when ran in Jenkins only

When using remote-webdriver instance of BrowserStack, only when ran in Jenkins, the failed screenshot is not attached to the report. please help. folder structure is ExtentReport\Screenshots

screenshot is missing

I tried Extent Report: Not able to see the screenshots on other machine this but it's not resolve the issue.

public void onTestFailure(ITestResult result) {
        testMap.get().fail(result.getThrowable());
        //add screenshot for failed test.
        WebDriver driver= WebDriverFactory.getDriver();
        //experimental to get screenshot
        driver = new Augmenter().augment(driver);
        String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        TakesScreenshot ts = (TakesScreenshot) driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String destination = System.getProperty("user.dir") + "/ExtentReport/" + "/Screenshots/" + result.getMethod().getMethodName() + dateName + ".png";
        File finalDestination = new File(destination);
        try {
            FileUtils.copyFile(source, finalDestination);
        } catch (IOException e) {
            e.printStackTrace();
        }
        testMap.get().addScreenCaptureFromPath(destination,result.getMethod().getMethodName());

    }

Upvotes: 0

Views: 2113

Answers (2)

Sameera De Silva
Sameera De Silva

Reputation: 1980

Based on the answer of ravi creed,

HTML : Unable to view the base64 image in html report

String base64Screenshot ="data:image/png;base64," + ((TakesScreenshot) Objects.requireNonNull(driver)).getScreenshotAs(OutputType.BASE64);
        testMap.get().addScreenCaptureFromBase64String(base64Screenshot).getModel().getMedia().get(0);

I managed to solve this using above code.Once click on base64 img, it opens the actual screenshot. enter image description here

Upvotes: 0

Prophet
Prophet

Reputation: 33361

Different operating systems use different characters as file and path separators. When our application has to run on multiple platforms, we need to handle these correctly.
To handle this issue Java provide File.separator.
So, instead of

String destination = System.getProperty("user.dir") + "/ExtentReport/" + "/Screenshots/" + result.getMethod().getMethodName() + dateName + ".png";

Try this:

String destination = System.getProperty("user.dir") + File.separator + "ExtentReport" +  File.separator +"Screenshots" + File.separator + result.getMethod().getMethodName() + dateName + ".png";

To use it you will have to add this import

import java.io.File;

Upvotes: 1

Related Questions