3

I'm trying to take screenshots when a test fails and then add them to my report. Currently it takes a screenshot when it opens up the application again to do the extent.flush(); in my afterMethod().

My screenshot class is here:

public class CaptureScreenShot {
    private static final DateFormat dateFormat = new SimpleDateFormat("yyy_MM_dd SSS");

        public static String captureScreen(WebDriver driver, String screenName) throws IOException {

        TakesScreenshot screen = (TakesScreenshot) driver;
        File src = screen.getScreenshotAs(OutputType.FILE);

        String dest = System.getProperty("user.dir") + "Test-ScreenShots" + screenName + ".png";

        File target = new File(dest);
        FileUtils.copyFile(src, target);

        return dest;
    }

    public static String generateFileName(ITestResult results) {
        Date date = new Date();
        return results.getName() + "_" + dateFormat.format(date);
    }
}

The report building class is here:

public class ExtentTestNGReportBuilder {

public static ExtentReports extent;
public static ThreadLocal<ExtentTest> parentTest = new ThreadLocal<>();
public static ThreadLocal<ExtentTest> test = new ThreadLocal<>();

private String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());

@BeforeSuite
public void beforeSuite() {
    extent = ExtentManager.createInstance("MobileCustomerCare " + fileName + ".html");
    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("C:\\Users\\tom.cockram\\Documents");
    extent.attachReporter(htmlReporter);
}

@BeforeClass
public synchronized void beforeClass() {
    ExtentTest parent = extent.createTest(getClass().getName());
    parentTest.set(parent);
}

@BeforeMethod
public synchronized void beforeMethod(Method method) {
    ExtentTest child = parentTest.get().createNode(method.getName());
    test.set(child);
}

@AfterMethod
public synchronized void afterMethod(ITestResult result) throws IOException {
    AppiumDriver<MobileElement> driver = MetricellTest.setupTests();
    String screenShot = CaptureScreenShot.captureScreen(driver, CaptureScreenShot.generateFileName(result));

    if (result.getStatus() == ITestResult.FAILURE) {
        test.get().log(Status.FAIL, result.getName());
        test.get().log(Status.FAIL, result.getThrowable());
        test.get().fail("Screen Shot : " + test.get().addScreenCaptureFromPath(screenShot));
        test.get().fail(result.getThrowable());
    } else if (result.getStatus() == ITestResult.SKIP) {
        test.get().skip(result.getThrowable());


    } else
        test.get().pass("Test passed");

    extent.flush();
}
Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
Tom Cockram
  • 217
  • 2
  • 16
  • Would highly suggest using the adapter, minimal setup and all reporting code is separated from your tests. https://github.com/extent-framework/extentreports-testng-adapter – foursyth Mar 21 '19 at 02:26
  • Have a look at this: https://github.com/Ardesco/Selenium-Maven-Template/blob/master/src/test/java/com/lazerycode/selenium/listeners/ScreenshotListener.java. and the associated https://github.com/Ardesco/Selenium-Maven-Template/blob/master/src/test/java/com/lazerycode/selenium/DriverBase.java. The general rule of thumb is make sure that you do not tear down your driver object in the AfterMethod block, but instead do it in the AtferSuite block – Ardesco Mar 21 '19 at 15:40
  • @foursyth I have just implemented the adapter and it was very simple. It doesn't seem as powerful though. How would I add screenshots with this? How would I change the file name to include todays date and time? – Tom Cockram Mar 22 '19 at 09:31
  • @TomCockram see [this example](https://github.com/extent-framework/examples/blob/master/extentreports-testng-adapter-example/src/test/java/com/aventstack/extentreports/adapter/testng/tests/ScreenshotTests.java#L43). You can use the adapter and still utilize all the flexibility of the API to customize it however you want. `ExtentTestManager.getTest(result)` will use the test in the current context to add the screencapture. – foursyth Mar 22 '19 at 14:48

1 Answers1

0

I'm not sure what your question is, but as per my understanding you are just not able to attach the screenshot.Assuming you have to attach it in case of test failures only. Put it in the method onTestFailure.

For screenshots name to append the date and time:

public static String getCurrentDateAndTime(String format) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        LocalDateTime now = LocalDateTime.now();
        return dtf.format(now).toString();
    }
Ankit
  • 1
  • 1