Code & QA

Extension Methods

I want to have a method in the framework which can take screenshots during my tests execution. Screenshots may be added to the summary reports for better understanding of a test execution flow. The IWebDriver can take screenshots, but I want all its actions being wrapped into a custom method for easier usage. I decided to implement the method as an extension method of IWebDriver interface because it will allow invoking the method such as if the IWebDriver actually had this method compiled inside its own native code.

Extension methods must be static and they must be located in a static class. I added new class to the Mapping project and named it for the purpose.

For my screenshot-making method I have to add references to the following libraries:

● Selenium tool files: Selenium.WebDriverBackedSelenium.dll, ThoughtWorks.Selenium.Core.dll, WebDriver.dll, WebDriver.Support.dll;

● project Logger: file Logger.dll located in Logger/bin/Debug folder;

● reference to System.Drawing library

Below is the method that takes screenshots:

[gist id=5b8a90961bd0bc438cc5b538e4e9b1af]

Considering that taking a screenshot has a sense only for reporting, the best way for using the method is passing it as a parameter to the Report class methods which log tests execution events.

So, I built the Mapping project and added the reference to it in the Tests project.

Now TakeScreenshot method may be invoked in the tests:

[gist id=2ea3a48772e5707869c8d3eb814133aa]

Interfaces       Table Of Content     Global variables

Leave a Reply

Your email address will not be published. Required fields are marked *