Capture Screenshot On Failure Or Pass - WebDriver Data Driven Framework

In our WebDriver data driven framework creation, We have Implemented calc software tests In all calc test cases of WebDriver data driven framework In STEP 30. Now supposing, You wants to capture screenshot of software web page when your test fails. It will helps you to analyse the test failure reason. Based on web page screenshot,
you can easily find out the test failure reason. Same way, Supposing you wants to capture screenshot when your test pass. This step will describe you how to capture screenshot on test failure or software automation test pass.

STEP 31
Prerequisite : All previous data driven framework creation steps should be Implemented. Find links of all previous steps on THIS PAGE.

What we will do In this step
As described above, We wants to capture screenshot on software test case fail or test case pass or both. We will Implement this functionality In such a way so that you can easily set which screenshot you wants to capture. For that you have to set flags In Param.properties file and then framework will capture screenshots based on your flag choice.

How To Do It
Do you have heard about testng listeners? We will use testng ITestListener Interface methods onTestFailure and onTestSuccess to solve our purpose. Question Is for what these methods are?
  • onTestFailure : This method will be Invoked each time a test succeeds.
  • onTestFailure : This method will be Invoked each time a test fails.
Both these testng ITestListener Interface methods will help us to capture screenshots In our data driven framework.

Download Required Files
You need new created ScreenshotUtility.java file for capturing screenshot on software test failure or success. We need to define listener In testng.xml file and also you need Param.properties to define screenShotOnFail or screenShotOnPass or both. So you need modified version of both these files too. You will get all these three files by downloading bellow given zip folder.
Extract the folder and Add/Replace files as bellow In eclipse.
  • Paste Param.properties File on com.stta.property Package.
  • Paste ScreenshotUtility.java File on com.stta.utility Package.
  • Paste testng.xml File on WDDF Project folder.
Your folder structure will looks like bellow.

If you open  testng.xml file, You will see that I have added some extra code In It to define listener. ScreenshotUtility.java file contains testng listener methods and captureScreenShot method to capture screenshot on pass or failure. I have added two new key In testng.xml File with values as bellow.

screenShotOnFail=yes
screenShotOnPass=yes

  • screenShotOnFail=yes means you wants to capture screenshot when your test case fails. 
  • Same way screenShotOnPass=yes will capture screenshot when test case pass.
  • screenShotOnFail=no means screenshot will be not captured even test case fails.
  • screenShotOnPass=no means screenshot will be not captured when test case pass.
You can set both these flags as per your requirement.

Run Test And Verify Output
Run test from "Shortcut to Run Test.bat" file. Right now screenShotOnFail and screenShotOnPass are set to yes so It will capture screenshots In both the cases. Means, We have total 8 data set lines for total 4 test cases so It will capture total 8 screenshots. 

When test execution completed, Refresh "WDDF" project folder. You will see that It has created new folder "screenshots" with 2 sub folders "Failures" and "Success". Expand all those folders. Our 2 tests are failed and 6 tests are pass so you will find total 2 .png files under "Failures" folder and 6 .png files under "Success" folder as shown In bellow given Image.


Now you can open all those files to analyse results. Basically screenshots are required only for failure conditions to see the software web page status when test was failed.

21 comments:

  1. I have completed all 31 steps successfully, awesome explanation. Thanks allot.

    Everything is fine but I am worrying about only coding part... Learnt from this blog on what Selenium WebDriver can do with regard web application testing and need to know how it is doing in-terms of code logic. I think for this I need to learn Java more.

    Aravind, can you please suggest on further???

    ReplyDelete
  2. i get error upon on testng.xml execution

    java.lang.NullPointerException
    at utility.ScreenshotUtility.captureScreenShot(ScreenshotUtility.java:68)
    at utility.ScreenshotUtility.onTestFailure(ScreenshotUtility.java:45)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1895)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1879)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1292)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1220)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


    =-=-=-
    on line 68
    File scrFile = ((TakesScreenshot) SuiteBase.driver).getScreenshotAs(OutputType.FILE);

    any idea what happen?

    ReplyDelete
  3. I had implemented all steps,modified according to my requirements, excellent blog thanks for sharing such stuff and i really appreciate your opinion
    "HELPING NEEDY PEOPLE IS ONLY WAY TO RESIDE IN HEART OF GOD. STAY HAPPY AND HEALTHY BY MAKING PEOPLE HAPPY AND HEALTHY"

    ReplyDelete
  4. Thanks a lot for your effort Bro...
    I have implemented all the steps without any errors.
    Once again thanks a lot for your help... :)

    ReplyDelete
  5. Wow very good...But I have found one defect in calc application using your framework..If one clicks on 4 the result displayed is 5....:)

    ReplyDelete
  6. java.lang.NullPointerException
    at com.stta.utility.ScreenshotUtility.captureScreenShot(ScreenshotUtility.java:65)
    at com.stta.utility.ScreenshotUtility.onTestFailure(ScreenshotUtility.java:44)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1895)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1879)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1292)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1220)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

    ReplyDelete
  7. How to capture the error message for example " Mobille no is already exist" "Email is not valid" so, how to handle the error handling for the above example?

    ReplyDelete
  8. Hi..Thanks a ton for this amazing blog :) It was a really helpful.

    ReplyDelete
  9. Hi,
    Thanks for sharing such a great knowledge. I have successfully implemented all the steps. the only issue i am facing is that i am retrieving the string data from the excel file and my 'Expected Result" is also a string and is different for different scenarios hence its not working. can you please help me out on this.


    Many Thanks

    ReplyDelete
  10. Hello,
    Thank for the effort, I have learnt a lot from step 1 to till. I need help. could you please suggest or post code, how to add this screen short in XSLT report.

    ReplyDelete
  11. Thanks a lot Arvind. Learnt a lot from your blog being a beginner.Your opininon is served with your great work. Keep it up !!!!!!!!!!

    ReplyDelete
  12. Great work Aravind, Could you please help me out how make single file java with multiple cases.Were your framework one class for one test cases.

    ReplyDelete
  13. Awesome tutorial......This is the kind of the tutorial I was looking for.... Fantastic !!!!! Great work !!!!!!

    ReplyDelete
  14. Thank you for your execelent tutorial. I have completed successfully all the 31 steps. I think that this project can be helpful to do other interesting projects that I hace in mind.

    Thanks for your good job!!!

    ReplyDelete
    Replies
    1. in step 12, my TestSuiteList.xlsx getting corrupt just after running project. in step 13 i am getting another exception.
      Can you please send your full code file on my mail id in attachment ?
      Please send

      Delete
  15. thanks boss. i have done all.
    Please suggest for .xlsx file.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Awesome!!! :) All steps are executed successfully and learnt lots of things about Selenium DDF. Thanks a lot for sharing this beautiful tutorial. Appreciate your explanation and work.

    ReplyDelete
  18. Thanks a lot for your step by step and detailed tutorial!!! It helps me a lot in learning selenium a lot

    ReplyDelete
  19. Thanks Aravind for the nicely compiled code, learned a lot from this page.

    ReplyDelete