Take screenshot for any method failure - @Test/@BeforeClass/@AfterMethod

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Take screenshot for any method failure - @Test/@BeforeClass/@AfterMethod

garvita girotra-2
Hi,

 I need to take screenshot in case of any exception or error in any method of framework - @Test/@BeforeClass/@AfterMethod

Current working :
TakeScreenshot method is in Browser class :

private final ThreadLocal<WebDriver> driver = new ThreadLocal<>();

public void setDriver(){
driver.set(DriverFactory.getInstance());
}

public WebDriver getDriver() {
return driver.get();
}

public byte[] takeScreenShot(String name) {
    String filename = makeScreenShotFileName(name); // return file name with date and time format
    String path = getRelativePath(filename); // directory path to save screenshots
    File screenShot = ((TakesScreenshot) getDriver())
            .getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenShot, new File(path));
    logger.info("Taking Screenshot of {} {}", getDriver().getCurrentUrl(), filename);
    return ((TakesScreenshot) getDriver()).getScreenshotAs(
            OutputType.BYTES);


All test classes extend BaseRun class. After each @AfterTest, take screenshot if test is failed :
@AfterMethod(alwaysRun = true)
protected void screenShotIfFail(ITestResult result) {
    if (!result.isSuccess()) {
        browser.takeScreenShot(result.getMethod());
    }
}

Driver is instantiated in DriverFactory class:
public WebDriver getDriver() {
    return setupDriver(); // Returns driver instance based on browser type
}


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/a547c0f3-1e05-4854-a9f4-a457db751006o%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Take screenshot for any method failure - @Test/@BeforeClass/@AfterMethod

Mark Collin-2
This may help:

https://github.com/Ardesco/Selenium-Maven-Template/blob/master/src/test/java/com/lazerycode/selenium/listeners/ScreenshotListener.java

The listener can then be applied to your base class like this:

https://github.com/Ardesco/Selenium-Maven-Template/blob/master/src/test/java/com/lazerycode/selenium/DriverBase.java#L15

On Monday, 29 June 2020 17:31:44 UTC+1, garvita girotra wrote:
Hi,

 I need to take screenshot in case of any exception or error in any method of framework - @Test/@BeforeClass/@AfterMethod

Current working :
TakeScreenshot method is in Browser class :

private final ThreadLocal<WebDriver> driver = new ThreadLocal<>();

public void setDriver(){
driver.set(DriverFactory.getInstance());
}

public WebDriver getDriver() {
return driver.get();
}

public byte[] takeScreenShot(String name) {
    String filename = makeScreenShotFileName(name); // return file name with date and time format
    String path = getRelativePath(filename); // directory path to save screenshots
    File screenShot = ((TakesScreenshot) getDriver())
            .getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenShot, new File(path));
    logger.info("Taking Screenshot of {} {}", getDriver().getCurrentUrl(), filename);
    return ((TakesScreenshot) getDriver()).getScreenshotAs(
            OutputType.BYTES);


All test classes extend BaseRun class. After each @AfterTest, take screenshot if test is failed :
@AfterMethod(alwaysRun = true)
protected void screenShotIfFail(ITestResult result) {
    if (!result.isSuccess()) {
        browser.takeScreenShot(result.getMethod());
    }
}

Driver is instantiated in DriverFactory class:
public WebDriver getDriver() {
    return setupDriver(); // Returns driver instance based on browser type
}


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/97613413-ec58-4719-b7e6-1e3c4e22b358o%40googlegroups.com.