Take a screenshot on exception with Selenium C#'s EventFiringWebDriver
Apart from concrete browser's WebDriver implementations like FirefoxDriver, ChromeDriver, PhantomJSDriver, etc., Selenium C# binding also provides one other type of driver called EventFiringWebDriver
, which wraps around any WebDriver instance and supports registering for events, e.g. for logging things that WebDriver has been doing.
This class inherits from IWebDriver
interface but additionally provides events like Navigating
, ElementClicking
, ExceptionThrown
etc. It can be found within Selenium's support library WebDriver.Support.dll (under namespace "OpenQA.Selenium.Support.Events"). The source code is here and the related tests exist here.
Below gives an example of how to take a screenshot when an exception is thrown while executing WebDriver tests using Selenium C# binding's EventFiringWebDriver class. Since the driver instance is a type of EventFiringWebDriver
, whenever an exception is thrown, ExceptionThrown
event will be triggered and a screenshot should be taken.
A completed example solution has been created on GitHub in this repository. Code has been tested under environment Windows 7, Firefox 26 with Selenium 2.39.0.