In the first part of this series we discussed about integrating NUnit with Visual Studio 2012. One of NUnit's strong points is its extensibility which has been used to expand unit testing even further -- as far up as the user interface in fact. This is where Selenium comes in.
Selenium runs on top of NUnit so that it can run tests against instances of web browsers. With the new testing features of Visual Studio 2012 we can now use it to test web applications.
If you haven't read the first part of this series and haven't followed the steps given there, I suggest reading it now and following the steps to install NUnit on Visual Studio 2012 first. They are a prerequisite to the next steps in this article.
Integating Selenium into Visual Studio 2012
Since we've already integrated NUnit to our Visual Studio solution, why not go all the way and use Selenium as well? Selenium uses the NUnit Framework for its tests anyway -- it's just a matter of adding several more components to allow Selenium's Webdriver to fire up a web browser and start executing tests.
To do this we need to extend our project a bit:
- Open up the Manage NuGet Packages window again (right-click on References under the project -> Manage NuGet Packages) and in the search box type "Selenium"
- Select "Selenium Webdriver" and click Install to add Selenium references to your project.
- Select "Selenium Webdriver Support Classes" and click Install -- these are some additional references necessary to run Selenium tests in your Visual Studio solution.
- Download IE Webdriver from the Selenium download page (choose the appropriate 32 or 64 bit version) and unzip.
- Righ-click on the project name and click "Add existing item..."
- Browse to the folder containing IEDriverServer.exe and choose that file to add to the project
- Under the project tree right-click on the file and click Properties
- Set the value of the field "Copy to Output Directory" to "Copy if newer"
The steps regarding the web driver ensure that the exe required to open up a browser is always copied to the /bin/Debug folder, from where it will in turn be used to call on the browser executables and open up the browser.
We can use the project we already set up above to test this. Add a new class and set it to have the following code. Note the additional OpenQA using declarations, aside from the NUnit.Framework that we've used before:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace TestAutomation
{
[TestFixture]
public class Driver
{
IWebDriver driver;
[SetUp]
public void Setup()
{
// Create a new instance of the Firefox driver
driver = new InternetExplorerDriver();
}
[TearDown]
public void Teardown()
{
driver.Quit();
}
[Test]
public void GoogleSearch()
{
//Navigate to the site
// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name( "q" ));
// Enter something to search for
query.SendKeys( "Selenium" );
// Now submit the form
query.Submit();
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 5 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until((d) => { return d.Title.StartsWith( "selenium" ); });
//Check that the Title is what we are expecting
Assert.AreEqual( "selenium - Google Search" , driver.Title);
}
}
}
|
Run the tests again. The test should open Internet Explorer and then open up Google. The result in the Test Explorer would look like this, just like our NUnit test:
Conclusion
With the capability to tightly integrate NUnit and Selenium in Visual Studio 2012 solutions and projects, Microsoft redeems itself by rectifying the ghosts of MSTest: it brings unit testing much closer to coding, it finally allows test-first/test driven development, and it opens up the capability for third-party unit testing frameworks to run in Visual Studio as first class citizens. This allows other open source testing frameworks like xUnit.net, QUnit/Jasmine, and MbUnit to run seamlessly with Visual Studio 2012.
References
I'd like to point out Anoop Shetty's blog post on Selenium integration from which I took the steps and code for the Selenium test used in this post. While his post was applicable to Visual Studio 2010 the steps were practically the same for Visual Studio 2012.