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.