Selenium UI Testing

Let's check links on a web page using C#, Visual Studio, NUnit, and Selenium Web Driver.

Visual Studio -> File -> New Project

Select C# Class Library

Name it StewShack.Web.UI.Tests

Rename Class1.cs to DefaultTest.cs

Let's add the [TestFixture] attribute to our DefaultTest class.

DefaultTest
namespace StewShack.Web.UI.Tests.Tests
{
    [TestFixture]
    public class DefaultTest
    {       
    }
}

Adding the TestFixture attribute causes a compile-time error. To fix it, we need to bring in the NUnit package using NuGet. Right-click on the project and choose:

Manage NuGet Packages…

Search online for NUnit. Install it. You should now have references to the NUnit framework.

Now you can add the using statement for the NUnit.Framework namespace. Just put your cursor over the TestFixture attribute and press CTRL+. (control + period). This will pull up the Generate from Usage menu. Choose the first option to add the using statment.

DefaultTest

using NUnit.Framework;

namespace StewShack.Web.UI.Tests.Tests
{
    [TestFixture]
    public class DefaultTest
    {
    }
}

Now we can add a test. I like to name my tests, [What is being tested]_[The action being taken]_[The expected result].

DefaultTest

using NUnit.Framework;

namespace StewShack.Web.UI.Tests.Tests
{
    [TestFixture]
    public class DefaultTest
    {
        [Test]
        public void Default_OpensInFirefox_NoBrokenLinks()
        {
            var driver = new FirefoxDriver();
            driver.Quit();
        }
    }
}

We cannot compile because we added code for Selenium without a reference. Manage Nuget packages and add Selenium WebDriver and Selenuim WebDriver Support Classes.

We added a Firefox driver, we need this driver to go to a page.

DefaultTest

using NUnit.Framework;

using OpenQA.Selenium.Firefox;

namespace StewShack.Web.UI.Tests.Tests
{
    [TestFixture]
    public class DefaultTest
    {
        [Test]
        public void Default_OpensInFirefox_NoBrokenLinks()
        {
             var driver = new FirefoxDriver();
             
             driver.Navigate().GoToUrl( "http://www.stewshack.com" );

             driver.Quit();
        }
    }
}

Running this test launches the home page of StewShack and immediately closes the browser.

We want to open each link on the home page in a new browser window to make sure it is still valid.

DefaultTest

using System.Threading;

using NUnit.Framework;

using OpenQA.Selenium.Firefox;

namespace StewShack.Web.UI.Tests.Tests
{
    public class DefaultTest
    {
        [Test]
        public void Default_OpensInFirefox_NoBrokenLinks()
        {
            var driver = new FirefoxDriver();
            driver.Navigate().GoToUrl( "http://www.stewshack.com" );
            var links = driver.FindElementsByTagName( "a" );

            foreach ( var link in links )
            {
                var linkDriver = new FirefoxDriver();
                var url = link.GetAttribute( "href" );

                if ( url.Contains( "http" ) )
                {
                    linkDriver.Navigate().GoToUrl( url );
                }
                else
                {
                    if ( url.IndexOf( "/" ) != 1 )
                    {
                        url = "/" + url;
                    }

                    linkDriver.Navigate().GoToUrl( "http://www.stewshack.com" + url );
                }

                // Give myself 2 seconds to look at the page before closing it.
                Thread.Sleep( 2000 );

                linkDriver.Quit();
            }

            driver.Quit();
        }
    }
}

Running this test opens a browser and views the home page. Then it begins to open new browsers for each link on the page.

Site back, rest your carpal tunnel, and watch the show. Have a pen ready because you will only have 2 seconds to catch a bad address.

Doing this I caught a bad link on my home page. It works!

Further Refactoring

You can put http://www.stewshack.com in a configuration setting along with how many seconds to wait. This would be helpful if you need to test other environments like beta.stewshack.com.

You can extract the method for viewing pages in Firefox so other tests can share it. All they have to do is pass in the Url.