Setup Selenium, NUnit, and Visual Studio

Update

This article is still relevant but a more comprehensive guide exists at http://timothycope.com/c-selenium-automation. This new guide also covers Page Object Design Pattern (POM) and Selenium Server (Grid).

Background

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.
http://docs.seleniumhq.org/

NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 2.6, is the seventh major release of this xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages.
http://www.nunit.org/

Install NUnit

You will need NUnit to execute your automated tests. Download the win MSI file and run it. Select a complete installation when prompted.

Setup Visual Studio

  1. Open Visual Studio
  2. Click File, click New, and select Project…
  3. In the left navigation pane, expand Templates, then Visual C#, and then select Class Library in the center window pane.
  4. In Package Manager Console, type Install-Package Selenium.WebDriverBackedSelenium
  5. In Package Manager Console, type Install-Package Selenium.Support
  6. In Package Manager Console, type Install-Package NUnit

Creating Your First Test

I renamed Class1.cs to AutomatedTest01.cs and that is reflected in the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;

namespace SeleniumClassLibrary
{
    [TestFixture]
    public class AutomatedTest01
    {

        IWebDriver driver;

        [TestFixtureSetUp] // http://www.nunit.org/index.php?p=testFixture&r=2.5
        public void TestSetUp()
        {
            // Set the IWebDriver to use FireFox
            driver = new FirefoxDriver();

            // Set the timeout to 30 seconds
            driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));

        }

        [Test] // http://www.nunit.org/index.php?p=test&r=2.5
        public void NavigateToGoogle()
        {
            // Navigate to URL
            driver.Navigate().GoToUrl("http://www.google.com");

            // Assert that the IWebDriver window's title is "Google"
            Assert.AreEqual("Google", driver.Title);
        }

        [TestFixtureTearDown] // http://www.nunit.org/index.php?p=fixtureTeardown&r=2.5
        public void FixtureTearDown()
        {
            // Closes all windows associated with the IWebDriver
            driver.Quit();
        }
    }
}

Save and build the project.

Run the Automated Test

  1. Open NUnit
  2. Click File and select Open Project…
  3. Find the .dll from your Visual Studio Project
  4. Click Run in the center window pane

Importing Code from Selenium IDE for Firefox

You can download the Firefox add-on that allows you to record-and-click basic webpage tasks.

  1. In Selenium IDE:
    1. Click File, select Export Test Case As…, and select C# / NUnit / WebDriver
  2. You can copy+paste the code into a new .cs file in your project or import the cs file
  3. Save and Build your project

Note: You may have to make some manual changes to the code to have it playback correctly in NUnit.

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumClassLibrary
{
    // Genereated by Selenuim IDE for Firefox
    [TestFixture]
    public class AutomatedTest02
    {

        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://timothycope.com/";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void The01Test()
        {
            driver.Navigate().GoToUrl(baseURL + "/");
            Thread.Sleep(2500);
            driver.FindElement(By.LinkText("ABOUT TIMOTHY COPE")).Click();
            Thread.Sleep(2500);
            for (int second = 0; ; second++)
            {
                if (second >= 60) Assert.Fail("timeout");
                try
                {
                    if (IsElementPresent(By.XPath("//img[@alt='logo']"))) break;
                }
                catch (Exception)
                { }
                Thread.Sleep(1000);
            }
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

Solution on GitHub

You can download the project I made from GitHub:
https://github.com/kirbycope/SeleniumClassLibrary

Q&A with a Software Tester

What is a QA software analyst?

Develops, publishes, and implements test plans. Writes and maintains test automation. Develops quality assurance standards. Defines and tracks quality assurance metrics such as defect densities and open defect counts. Requires a bachelor’s degree and 0-2 years of experience coding in C, C++, Java. Must have a working knowledge of quality assurance methodologies. Familiar with NT, UNIX and/or Solaris environments. Relies on experience and judgment to plan and accomplish goals. Performs a variety of tasks. Works under general supervision; typically reports to a manager. A certain degree of creativity and latitude is required.
Salary.com

What is a typical day in QA like?

Typically, a QA member will:

What is the career path?

Note: There is no formal system or naming convention.

  1. Software Tester –  Entry level testing positions (testers) that do black-box testing.
    1. Requires an aptitude for PC troubleshooting
    2. Executes Test Plans
  2. Analyst – More experienced testing positions do white-box testing.
    1. Requires knowledge of programming logic and design
    2. May also require security testing proficiency
    3. Writes and Executes Test Plans
  3. Automation Specialist  – Uses white-box testing to design for test automation.
    1. Requires knowledge of Computer Science, generally a Bachelor of Science
    2. Converts Test Plans to automated tests
  4. Team Lead – Provides peers guidance.
    1. Can be a Tester, Analyst, or Automation Specialist
  5. Manager – Guides the efforts of the team and makes process adjustments

What are the differences between a developer and an automation specialist?

Both require programming knowledge, however a developer has more. A developer is responsible for writing clean and efficient code to run in production environments. The automation specialist’s code is often only seen and used by QA. That is like how the unit tests are only seen and used by developers. QA’s code is written in large nets of error catching and handling. The program might just try to get record 13 but the tests would have to make sure 13 exists, the call to delete was made, the record 13 was deleted, and no errors were returned.

How do I become a tester?

Like most jobs the education requirements are to scare off people with no confidence. I have seen people hired with no prior QA experience. As long as you show yourself capable of critical thinking you will be fine.

Should I get certifications?

If you want, sure. Will it help? It shouldn’t. You can learn a great deal about testing and even show you retained some of that information for a short while. The problem is that you can take these tests and pass by simply remembering the vocabulary. This does not equate to proficiency.

Instead, I recommend doing some reading on testing. Also, it never hurts to get an online Master of Science in Computer Science for $6,600 from Georgia Tech.

How do I hire a good tester?

There are tests on ProveIt, but those kinds of tests mark you off for clicking a button instead of going to File->Open.

I recommend making your own test:

  • Use a VM snapshot that you can revert to with each interview
  • Give them the tools they need – Internet access and/or pre-installed software that is used by your team
  • Give them 30 minutes to take the test
  • Have a number of bugs ranging in difficulty
    • Easy Bugs – Ones found during Happy Path Testing
      • Example: Clicking ‘submit’ on login form causes 404 error
    • Avg Bugs – Ones found during Negative Testing
      • Example: Entered a letter in a number field
    • Hard Bugs – Ones found using tools
      • Example: Javascript errors in Developer Tools