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 Extensions.sc 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:

using Logger;

using OpenQA.Selenium;

using System;

using System.Configuration;

using System.Globalization;

using System.IO;

using System.Text.RegularExpressions;

namespace Mapping


/// <summary>

/// stores static extension methods for ISearcContext and other entities

/// </summary>

public static class Extensions


/// <summary>

/// creates screenshot of opened browser window and saves it as a file

/// </summary>

/// <param name=”driver”>Current IWebDriver or IWebElement</param>

/// <param name=”screenshotDescription”>Description of the screenshot</param>

/// <returns>Path to saved picture file or empty string if an exception has occurred</returns>

public static string TakeScreenshot(this IWebDriver driver, string screenshotDescription)


string screenshotFolder = Report.ReportFolderPath + “\\ScreenShots”;

string screenshotFileName = string.Empty;



var screenshot = ((ITakesScreenshot)driver).GetScreenshot();


screenshotFileName = screenshotFolder + “\\” + Regex.Replace(screenshotDescription, @”\s+”, ” “).Replace(” “, “-“) + “_” + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace(“:”, “-“).Replace(“/”, “-“).Replace(” “, “_”) + “.jpeg”;

screenshot.SaveAsFile(screenshotFileName, System.Drawing.Imaging.ImageFormat.Jpeg);


catch (Exception e)


Report.AddWarning(“Getting screenshot”, “Screenshot picture is not saved with path ‘” + screenshotFileName + “‘”, e.Message);


return screenshotFileName;




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:

using System;

using NUnit.Framework;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using OpenQA.Selenium;

using OpenQA.Selenium.Firefox;

using OpenQA.Selenium.IE;

using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.Support.UI;

using nUnit = NUnit.Framework;

using System.Collections.Generic;

using Logger;

using System.Reflection;

using Mapping;

namespace Tests



public class Example : BaseTest



public void GoogleSearch()


string searchTerm = “selenium”;

Dictionary<string, string> scope = new Dictionary<string, string>();

scope.Add(“Navigate to Google search page”, “Google search page is loaded”);

scope.Add(“Do nothing”, “”);

scope.Add(“Perform search on the term ” + searchTerm, “Title of search result page starts with ” + searchTerm);

Report.StartTestCase(this.GetType().Name, MethodBase.GetCurrentMethod().Name, “Google Search”, scope);



Report.AddInfo(“Navigation to Google Search page”, string.Empty, driver.TakeScreenshot(“Google Search page”));


Report.AddInfo(“No action”);


IWebElement query, btnSearch;

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

query = driver.FindElement(By.Name(“q”));



btnSearch = driver.FindElement(By.Name(“btnG”));


Report.AddInfo(“Search is performed”, “Search result page is opened”, driver.TakeScreenshot(“Search button is clicked result”));

wait.Until(d => { return d.Title.StartsWith(“selenium”); });


Report.AddInfo(“Verification”, screenshotPath: driver.TakeScreenshot(“Search result”));





[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Interfaces [/column]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Table Of Content
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Global variables

Leave a Reply


Notice: Undefined variable: font_family in /home/chyqruxavcln/public_html/learnseleniumtesting.com/wp-content/plugins/gdpr-cookie-compliance/moove-modules.php on line 282