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

clip_image001

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;

try

{

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

Directory.CreateDirectory(screenshotFolder);

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.

clip_image002

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

{

[TestFixture][TestClass]

public class Example : BaseTest

{

[Test][TestMethod]

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.RunStep();

driver.Navigate().GoToUrl(“http://www.google.com”);

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

Report.RunStep();

Report.AddInfo(“No action”);

Report.RunStep();

IWebElement query, btnSearch;

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

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

query.Clear();

query.SendKeys(“selenium”);

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

btnSearch.Click();

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”); });

nUnit.Assert.IsTrue(driver.Title.StartsWith(“selenium”));

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

}

}

}

clip_image005

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

Leave a Reply

Archives



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