Here is a step-by-step guide to setting up an NUnit test with Selenium WebDriver. In this example, the program opens a browser window, goes to bing.com, searches for the phrase “Selenium” and then tests that the results page contains a link to SeleniumHQ. A similar program using WatiN rather than Selenium WebDriver can be found in the post Getting Started with WatiN (Part 2).
1. Add a new project to the SeleniumDemo solution created in the post Getting Started with Selenium WebDriver, InternetExplorerDriver and C#
2. Select “Class Library” and enter the name “SeleniumDemo2”
3. “Set as StartUp Project”
4. Open “Manage NuGet Packages…”
5. Search for and install “NUnit”
6. Search for and install “NUnit.Runners”
7. Go to “Project”, “Add Reference…”
8. Click on “Browse” and select “WebDriver.dll” at location “SeleniumDemo\packages\Selenium.WebDriver.2.33.0\lib\net40”
9. Copy the code below into the class “Class1.cs”
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.IE; namespace SeleniumDemo2 { [TestFixture] public class Class1 { private IWebDriver driver; [TestFixtureSetUp] public void SetUp() { driver = new InternetExplorerDriver(@"C:\Users\olpower\Documents\Selenium"); driver.Navigate().GoToUrl("http://www.bing.com"); } [TestFixtureTearDown] public void TearDown() { driver.Quit(); driver.Dispose(); } [Test] public void BingSearchForWatiN() { driver.FindElement(By.Name("q")).SendKeys("Selenium"); driver.FindElement(By.Name("go")).Click(); StringAssert.AreEqualIgnoringCase(driver.FindElement(By.PartialLinkText("Selenium - Web Browser Automation")).GetAttribute("href"), "http://seleniumhq.org/"); } } }
10. Go to “Project”, “SeleniumDemo2 Properties…”
11. Go to “Debug” and select “Start external program”
12. Select “nunit.exe” at “SeleniumDemo\packages\NUnit.Runners.2.6.2\tools”
13. Start Debugging (F5). When the NUnit window opens, go to “File”, “Open Project”
14. Select “SeleniumDemo2.dll” at “SeleniumDemo\SeleniumDemo2\bin\Debug”
15. Click on “Run” in NUnit window