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.
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.
Now we can add a test. I like to name my tests, [What is being tested]_[The action being taken]_[The expected result].
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.
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.
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!
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.