Want to create a generic (reusable method) to take the screenshot on failure


I am adding  extent report feature and screenshot on failure in my framework.
If I use @aftermethod using ITestresult in each test script then it is not reusable method. I want to create “screenshotonfailure()” methed in such a way so that I can reuse it in each test script whenever test fails.
I have created a superclass with “screenshotonfailure()” method using @aftertest annotation and test script class extends this superclass but it is not working (no Report, no screenshot), giving configuration failure.
Please tell me the way to achieve this.

Below are 2 classes-:
Superclass.java in one package and HomePageTests.java in other package

public class Superclass {
WebDriver driver;

@AfterSuite(alwaysRun = true)
public void screenshotonfailure(ITestResult result) {
System.out.println(“Check it aftermethod”);
if (result.getStatus() == ITestResult.FAILURE) {
String screenshot_path = Utility.Screenshot.getscreenshot(driver, “screenshot1”);
String image = Utility.ExtentReport.logger.addScreenCapture(screenshot_path);
Utility.ExtentReport.logger.log(LogStatus.FAIL, “testverification”, image);


public class HomePageTests extends Superclass {
WebDriver driver;

@Test(groups = “P1”)
public void verifyMyAccuntLinkPresence() {


driver = BrowserFactory.getbrowser();
Utility.ExtentReport.logger.log(LogStatus.INFO, “browser is up and running”);

Utility.ExtentReport.logger.log(LogStatus.INFO, “Application is up and running”);

HomePagePOM homepage = PageFactory.initElements(driver, HomePagePOM.class);

if (homepage.isMyAccountDisplayed() && homepage.isMyAccountClickable()) {
Assert.assertTrue(true, “MyAccount link is not displayed & clickable”);
Utility.ExtentReport.logger.log(LogStatus.PASS, “MyAccount link is present”);


Default Asked on January 26, 2016 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)


    You need to override onTestFailure method of ITestListener(Testng)
    and call your screenshotmethod in this method.

    Vice Professor Answered on January 27, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.