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.

One Reply to “Selenium Hello World”

Leave a Reply

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