Does any of you know possible solution for taking screenshots on test failures and exceptions?
I've added following code in TearDown()
but as a result it also makes screenshots on passed tests, so it is not the best solution:
DateTime time = DateTime.Now;
string dateToday = "_date_" + time.ToString("yyyy-MM-dd") + "_time_" + time.ToString("HH-mm-ss");
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile((settings.filePathForScreenShots + "Exception" + dateToday + ".png"), System.Drawing.Imaging.ImageFormat.Png);
I've already found that idea: http://yizeng.me/2014/02/08/take-a-screenshot-on-exception-with-selenium-csharp-eventfiringwebdriver/, to use WebDriverExceptionEventArgs
, but for some reasons it makes also some random screenshots without any reasonable explanation.
Other ideas I found are for Java and not for NUnit which I use with Selenium, so they are pretty useless.