1

I am using the MSpec test framework/runner and the Selenium Web Driver. I want to take a screenshot whenever a test fails and saves it so that I can see the error later. I am thinking there can be two possibilities:

  • Does MSpec have an assertion-failed event? Something like the cleanup interfaces?
  • Does Selenium have some kind of failure event that can automatically take the screenshot?

Can anyone please tell me if you have done something similar and how?


There is a similar question answered already here but my scenario is different. I don't want to modify my tests and put a try catch there in every test.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Afraz Ali
  • 2,672
  • 2
  • 27
  • 47

1 Answers1

0

Although Selenium happily supports taking screenshots (see this answer on taking screenshots in C#), it will have no knowledge of the test runner you are using it within. In fact, as a multi-purpose browser driver, it may be used for a lot of other tasks in addition to testing.

You'll need to add some kind of hook to your MSpec runner on failure to trigger this code. I have done something similar which worked great with JUnit in Java (an @Rule for failures), I'm afraid I'm unfamiliar with MSpec to suggest what to use here. I do suggest you have your screenshots published as a build test artifact for easy access in a CI environment.

Community
  • 1
  • 1
Ross Taylor-Turner
  • 3,687
  • 2
  • 24
  • 33
  • Here's an example: https://github.com/agross/mspec-samples/tree/master/WebSpecs/LoginApp.Selenium.Specs – Alexander Groß Jun 26 '14 at 17:14
  • @AlexanderGroß, I don't see where you're taking the screenshot on failure. – Anthony Mastrean Jun 26 '14 at 18:07
  • @AnthonyMastrean MSpec does that automatically because we have a ResultSupplementer in our specs assembly. https://github.com/agross/mspec-samples/blob/master/WebSpecs/LoginApp.Selenium.Specs/SeleniumSupport.cs – Alexander Groß Jun 27 '14 at 09:43