Taking Screenshots with Selenium

The Problem:

You have some automated Selenium tests that you run periodically. Maybe these tests run as part of a continuous deployment process. Occasionally these tests will fail and you’ll find yourself analyzing error messages or stack traces that may or may not be very helpful. Wouldn’t it be nice to have some other tool available to help you with your debugging?

A Solution:

This solution provides a way of capturing screenshots of your browser, but only when your test fails. This way you don’t have a mess of screenshots to go through when its time to see why your test failed.

We are going to continue adding functionality to the project we created in the Selenium Hello World! post. We will be adding a new class FailureTasks. Place the class before the last squiggly bracket in Google.cs.

public class FailureTasks
{

}

Failure tasks will have two global variables, a webdriver and a function.

private IWebDriver _webDriver;
private Action<IWebDriver> _function;

Next, lets add the mechanism which actually takes the screenshot. In addition to the OpenQA.Selenium library. We will also need to import the System.IO and System.Drawing.Imaging libraries.

using System.IO;
using System.Drawing.Imaging;
public void SaveScreenshot() {
    Screenshot ss = 1)ITakesScreenshot)_webDriver).GetScreenshot();

    ss.SaveAsFile(
        Path.Combine(
            Environment.CurrentDirectory,
            string.Format("{0} - {1}.png",
                "Hello World",
                "Google",
            ImageFormat.Png);
}

Now, lets build the mechanism that takes the screenshot, but only when something in the _function global variable fails. This is done easily with a try-catch.

public void TakeScreenShot() {
    try {
        _function(_webDriver);
    }
    catch {
        SaveScreenshot();

        throw;
    }
}

The last thing we need for the FailureTasks class is a constructor.

public FailureTasks(IWebDriver webDriver, Action<IWebDriver> function)
{
    _webDriver = webDriver;
    _function = function;
}

We now have everything we need to take our screenshot. We just need to wrap the code in SearchForHelloWorld in a FailureTask object by using a lambda expression and tack on the TakeScreenShot method.

public void SearchForHelloWorld()
{
    IWebDriver webDriver = new OpenQA.Selenium.Firefox.FirefoxDriver();
    new FailureTasks(webDriver, f =>
    {
        webDriver.Navigate().GoToUrl("http://www.google.com/");
        IWebElement textBox = webDriver.FindElement(By.Id("gbqfq"));
        textBox.SendKeys("Hello World!" + Keys.Enter);
        new WebDriverWait(webDriver, TimeSpan.FromSeconds(5)).Until<bool>(dr =>
        {
            try
            {
                IWebElement helpButton = dr.FindElement(By.XPath("//span[@id='fsl']/a"));
                Assert.AreEqual("Help", helpButton.Text);
                return true;
            }
            catch
            {
                return false;
            }
        });
    }).TakeScreenShot();
    webDriver.Dispose();
}

The test will now execute as it did before, but now, if the test fails, we will be able to find a screenshot of the browser at the time of failure in the HelloWorld project’s bin/Debug folder. You can make the test fail by removing its WebDriverWait.

Ninja Level

  • One of the things you can do to clean this up is to create a method that creates the FailureTasks object for you. If you do this, you can then use the screenshot mechanism like this:
webDriver.WhenThisFails(dr => {/*some function*/}).TakeScreenshot();

References   [ + ]

1. ITakesScreenshot)_webDriver).GetScreenshot(); ss.SaveAsFile( Path.Combine( Environment.CurrentDirectory, string.Format("{0} - {1}.png", "Hello World", "Google"

The WebDriverWait

It won’t take very long for an automated tester to realize that their test become brittle when dealing with dynamic web pages. Elements the tester is trying to manipulate will change in the middle of your tests causing exceptions.

Take, for example, the test we created in the Hello World! post. The Help button is one of the last things to load on the Google search page.

Lets test that the text of the Help button is indeed “Help”. Add the following to the HelloWorld test right before the WebDriver disposal.

IWebElement imagesLink = webDriver.FindElement(By.XPath("//a[[text()='Images for Hello World!'\]]"));
Assert.AreEqual(true, imagesLink.Displayed);

Run the test and you’ll find that you receive a StaleElementReferenceException.

You may be tempted to use something like c# threading functionality to “pause” the test and wait for a dynamic website to settle. This should be avoided at all cost. “Pauses” of a constant length will waste valuable resource time and you’ll find that your tests still fail occasionally.

Instead, perform a “pause” of dynamic length. The WebDriverWait can be used for this purpose.

The WebDriverWait class is contained in the OpenQA.Selenium.Support.UI library, so import it in addition to the traditional Selenium library. The Support.UI library is contained in the Selenium WebDriver Support Classes nuget package.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

Now, we can wrap the code we added to HelloWorld in a WebDriverWait.

new WebDriverWait(webDriver, TimeSpan.FromSeconds(5)).Until<bool>(dr =>
    {
        try
        {
            IWebElement imagesLink= dr.FindElement(By.XPath("//a[[text()='Images for Hello World!']]"));
            Assert.AreEqual(true, imagesLink.Displayed);
            return true;
        }
        catch
        {
            return false;
        }
    });

The WebDriverWait constructor takes two parameters.

  • IWebDriver webDriver – this is your WebDriver object.
  • TimeSpan Timeout – this is a time span which describes the maximum amount of time we will wait for the test to pass.

The Until method takes one parameter, a function. This syntax is referred to as a lambda expression.

The variable ‘dr’ becomes a reference to the calling object. In our case, the WebDriverWait.

What’s going to happen is code between the squiggly brackets is going to loop until either it returns true or the timeout is exceeded. We want to avoid throwing exceptions during this time so we catch all exceptions and return false instead.

With this technique, you can “wait” for an element to appear without wasting resource.

Ninja Level:
  • Make your timeout a global variable. This way if you need to increase/decrease your timeout, you only need to change it in one place.
  • You can change the object between the brackets to that of another object type. In this case, the function loops until a non-null value is returned.

Selenium Hello World

This is the first of a series of posts regarding Selenium.

This tutorial will be a step by step description of how to build a simple automated Selenium test.

Context:

  • C#

Prerequisites:

  • Anyone should be able to follow along with this tutorial.
  • Visual Studio (VS)

The Tutorial

Create the project

  • Open VS and on the menu bar: File -> New -> Project…
  • On the LHS select Visual C#.
More info
Selenium contains support for Java, C#, Ruby, Python, and Javascript. This tutorial deals specifically with C#.

 

  • Select “Class Library”.
  • Name the project “HelloWorld”, specify a location, and click OK.

Nuget

To avoid having to manually add the Selenium libraries to our project, we’ll use Nuget to automatically download them.

More info
Leveraging Nuget has several advantages. It allows you to not have to keep 3rd party libraries in source control and it allows for automatic updates when 3rd parties release new versions.

 

  • In the Solution Explorer, right-click References and select Manage Nuget Packages… from the right-click menu.
  • In the upper right corner of the new screen, search for Selenium.
  • Select Selenium WebDriver and click Install.

We’ll also need the VS quality tools library. Its library is downloaded by default, but you still need to add a reference to it.

  • In the Solution Explorer, right-click References and select Add Reference…
  • In the upper right corner of the new screen, search for UnitTest.
  • Check the check box for one of the Microsoft.VisualStudio.QualityTools.UnitTestFramework and click OK.

Hello World!

  • In the Solution Explorer, right-click Class1.cs and select Rename from the right-click menu.
    • Lets give the class a more descriptive name, Google.cs.
    • Upon changing the name, you will receive a popup asking if you would like to update all references to Class1. Click Yes.
More info
A class is a computer programming term that describes a file which contains code. The code contained within could serve many purposes.

 

  •  In the Solution Explorer, double-click Google.cs to view it.
  • The Google.cs file contains 3 parts: imports, namespace, and class.
Imports
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  • Imports consist of the keyword “using” and then the library you would like to use. To leverage Selenium we need to import it, as well as the unit testing library.
using OpenQA.Selenium;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Namespace

The namespace (in this case HelloWorld) is used to describe a library. If someone wanted to leverage the code contained in HelloWorld in their own code, they would import it by:

using HelloWorld;
Class

The class (in this case Google) is used to describe what the code in this file is for. Also, if another developer wanted to reference our code, he would use the class name.

HelloWorld.Google

Above the line which declares the Google class, add the following line so that it looks like this:

[TestClass]
public class Google

This signifies to VS this class contains tests.

Method

We will now create a method that navigates to http://www.google.com/, and searches for “Hello World!”.

Between the squiggly brackets under the Google class enter:

[TestMethod]
public void SearchForHelloWorld()
{
    IWebDriver webDriver = new OpenQA.Selenium.Firefox.FirefoxDriver();
    webDriver.Navigate().GoToUrl("http://www.google.com/");
    IWebElement textBox = webDriver.FindElement(By.Id("lst-ib"));
    textBox.SendKeys("Hello World!" + Keys.Enter);
    webDriver.Dispose();
}
[TestMethod]

This signifies to VS this method contains a test.

public void SearchForHelloWorld()

This is the method declaration.

  • public
    • This controls access. Public means anyone can use it.
  • void
    • This is the method’s return value. Void means it returns nothing.
  • SearchForHelloWorld
    • This is the name of the method.
  • ()
      If the method required any parameters, they would be contained between the parenthesis.
IWebDriver

IWebDriver is an object which contains all the accessors and methods you need to read and manipulate a web browser.

This line opens a new Firefox window.

IWebDriver webDriver = new OpenQA.Selenium.Firefox.FirefoxDriver();

This line navigates the browser to http://www.google.com/.

webDriver.Navigate().GoToUrl("http://www.google.com/");

This line gets the element from the browser who’s id is “gbqfg”. You can obtain an element’s id by right clicking on it in your browser and selecting Inspect Element from the right-click menu.

IWebElement textBox = webDriver.FindElement(By.Id("gbqfq"));

This line sends the text “Hello World!” to the text box and clicks Enter.

textBox.SendKeys("Hello World!" + Keys.Enter);

This line closed the Firefox browser and frees up its resources to other processes.

webDriver.Dispose();

Build and Run

You can now right-click on HelloWorld in the Solution Explorer and select Build from the right-click menu.

To view the Test Explorer, on the menu bar: Test -> Windows -> Test Explorer

After the project is done building, the test should be available in the test explorer. Right-click it and select run or debug and enjoy watching your test run.

Up next:

  • Building your Selenium test architecture.

Building Your First VS Ultimate Performance Test

I’ve been asked, how do you get started building performance tests in Visual Studio (VS). This post is an attempt at guiding some one with some VS experience in building a performance test using VS Ultimate.

Context:

  • API testing
  • C#

Prerequisites:

  • Some programming experience.
    • If you are familiar with some programming terminology and can write code, you should be able to follow along with this tutorial.
  • Dummy API is active.
    • The specific code in the tutorial relies on a dummy API build using Mockable’s online service. The free service relies on my interaction to keep the dummy alive.
  • VS Ultimate
    • If you don’t have VS Ultimate, you won’t be able to use this tutorial.
    • You also need a little experience using VS.

The Tutorial:

  • Create a Web Performance and Load Test Project.
    • On the VS menu bar: File -> New -> Project…
    • On the LHS of the new window: Installed -> Templates -> Visual C# -> Test -> Web Performance and Load Test Project
    • Enter a name.
    • Choose a location.
    • Click OK.
  • A web and load test project is created with one .webtest file already created for you.
  • Record the API call.
    • Double click the .webtest file to open it in VS.
    • Click the Add Recording button.
    • In the browser that is opened, enter this URL, http://demo3838757.mockable.io/.
    • In the browser, click Stop.
  • Create the Web Test Coded file.
    • Double click the .webtest file in the solution explorer to open it in VS.
    • Click the Generate Code button.
    • Name the file and click OK.
  • At this point you can delete the .webtest file. It is no longer needed.
  • Parametrize the API URL.
    • Edit the .cs file.
      • Add the following line to the constructor.
this.Context.Add("Url", "http://demo3838757.mockable.io/");

Change the following line, contained in the GetRequestEnumerator method, from:

WebTestRequest request1 = new WebTestRequest("http://demo3838757.mockable.io/");

To:

WebTestRequest request1 = new WebTestRequest(this.Context["Url"].ToString());

Validate Response:

  • In the .cs file, import the WebTestFramework library.
using Microsoft.VisualStudio.QualityTools.WebTestFramework;
  • In the .cs file, add an expected result parameter.
    • Add the following line to the constructor.
      • I’ll show you in a future post how to make this look prettier.
this.Context.Add("ExpectedMessage", "{\n\t\"msg\":\"hello world\"\n}");
  • In the .cs file, create a new class.
public class MessageValidation : ValidationRule
{
    public override void Validate(Object sender, ValidationEventArgs e)
    {
        WebTestContext context = e.WebTest.Context;
        string responseString = e.Response.BodyString;
        string statusCode = e.Response.StatusCode.ToString();
        string expectedMessage = context["ExpectedMessage"].ToString();

        if (responseString.Equals(expectedMessage) &&
        statusCode.Equals("OK"))
        {
            e.IsValid = true;
            return;
        }
        e.IsValid = false;
        e.Message = "Invalid response";
    }
}

This class compares the response of an HTTP request to the “ExpectedMessage” parameter, and if they are equal and the request’s status code is equal to “OK”, marks the request as valid. If they are not equal or the request’s status code does not equal “OK”, the request is marked as not valid and sets an error message.

  • Execute the validation rule.
    • Add the following lines of code to the GetRequestEnumerator method, after the WebTestRequest but before the return value.
MessageValidation validation = new MessageValidation();
request1.ValidateResponse += new EventHandler<ValidationEventArgs>(validation.Validate);

These lines trigger the validation rule to run.

You can now right click anywhere in the GetRequestEnumerator method and select Run Coded Web Performance Test from the right click menu to run your test.

Up Next:

  • Parsing JSON with data structures and Jayrock
  • Using the performance test to create a load test.