Take Screenshot on Test Failure

Let’s see, How can you Take Screenshot on Test Failure in Appium. In previous tutorials, You have seen, How can you handle Alerts in Android using Appium. Now we will see, How can we Take ScreenShot on Test pass or Test fail in Andriod Application.


We have learned how to capture the screenshot of Android Apps.  Now supposing you want to capture screenshot on Test Failure or Test Pass. In this case,  ITestListener Interface of TestNG can help us to capture screenshots on certain occurrence like test failure or software test pass.


ITestListener Interface in TestNG

ITestListener is an Interface of TestNG which can Invoke its specific method on test failure or test pass or test skips, etc. There is bellow given useful methods available In ITestListener Interface.


  • onFinish(ITestContext context) – Invoked after all the software tests have run.

  • onStart(ITestContext context) –  Invoked after the test class is instantiated.

  • onTestFailure(ITestResult result) – Invoked each time a test fails.

  • onTestSkipped(ITestResult result) – Invoked each time a test is skipped.

  • onTestStart(ITestResult result) –  Invoked each time before a test will be invoked.

  • onTestSuccess(ITestResult result) – Invoked each time a test succeeds.


You can view more detail on ITestListener Interface on Listener in TestNG.

To implement Test Script to capture the Screen on Test Pass/Fail, We will separate class file to write logic of capture screenshot on test fail or pass.


Inside It, We will create separate method captureScreenShot() to write logic to capture the screenshot. We will use TestNG assertion to verify our software test is fail or pass and use ITestListener Interface to Invoke onTestFailure() method on assertion failure and onTestSuccess() on assertion pass and then call captureScreenShot method to take the screenshot.


In test class file, We will write two test methods TestForFail() and TestForPass(). Test method TestForFail() is written In such a way to fail It Intentionally.

Screenshot of TestForFail() method will be stored Inside Failure folder and screenshot of TestForPass() method will be stored Inside TestPass folder.


Take Screenshot On Test Failure or Pass

We have implemented a simple example to explain,  how to take Screenshot on test failure or pass.

TakeScreenshotUtility.java file contains logic to capture screenshot on test failure or pass.


In above example, We have implemented the logic to capture the screenshot only.


We will write our Test Logic in ScreenshotOnPassFail.java file . You can see In bellow given test, We have used @Listeners annotation. It mansion that TestNG listeners are Implemented In TakeScreenshotUtility class file to listen.

You can execute above code on your local machine.


  • TestForFail() method : Here assertion will fail so onTestFail method will be called fromTakeScreenshotUtility.java. It will capture the screenshot and store It in Failures folder under screenshots folder.

  • TestForPass() method : Here assertion will pass so onTestSuccess method will be called from TakeScreenshotUtility.java. It will capture the screenshot and store It in Success folder under TestPass screenshots folder.


Using this way, You can capture screenshot on failure or pass of any Android Appium Automation Test Script.

0 Comment

Leave a Reply