Unit Testing and Test Automation in VS2012 Part 2: Integrating Selenium with Visual Studio

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:

  1. 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"
  2. Select "Selenium Webdriver" and click Install to add Selenium references to your project.
  3. Select "Selenium Webdriver Support Classes" and click Install -- these are some additional references necessary to run Selenium tests in your Visual Studio solution.
  4. Download IE Webdriver from the Selenium download page (choose the appropriate 32 or 64 bit version) and unzip.
  5. Righ-click on the project name and click "Add existing item..."
  6. Browse to the folder containing IEDriverServer.exe and choose that file to add to the project
  7. Under the project tree right-click on the file and click Properties
  8. 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:

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
            driver.Navigate().GoToUrl("http://www.google.com");

            // 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.

About Jon Limjap

Jon Limjap has been programming since he was 12 and hasn't stopped yet. He currently codes Java during day, iOS at night, and C# in the weekends.
This entry was posted in Technical Articles and tagged , , , , , . Bookmark the permalink.

4 Responses to Unit Testing and Test Automation in VS2012 Part 2: Integrating Selenium with Visual Studio

  1. Ksou says:

    Can you explain if their where any errors in getting this to run . Visual Studio keeps saying that “In order to debug this project add an exe…”
    But I’ve done that and I can’t figure out why VS isn’t using it

  2. Jon Limjap says:

    Ksou,

    It looks like you’re trying to Run/Debug the application itself, not the tests. Are you sure that you’re really just clickin gon Run -> All Tests?

  3. PhucHoa says:

    Hi,

    I did all 8 steps as your guide, but my project threw error that “IEDriverServer.exe file does not exist in the current directory or in a directory on the PATH environment variable.”

    I checked that in my debug folder, there was IEDriverServer, but it couldn’t be found.

    Can you help me with this bug :(

  4. Jon Limjap says:

    PhucHoa,

    That is strange, I’ve run into this problem several times myself and steps 4-8 usually solved it. This stackoverflow post: http://stackoverflow.com/questions/11010317/iedriverserver-does-not-exist-error-during-running-selenium-test-with-c-sharp recommends setting it to “Copy Always”, you might want to try that as well.

    Oh, another thing: make sure you have the correct version of IEDriverServer.exe running. You might be trying to run a 64-bit exe in a 32-bit environment, or vice-versa (which should work, actually) so that’s one more thing to check.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>