Getting Started with Selenium WebDriver and NUnit

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#

Step1

2. Select “Class Library” and enter the name “SeleniumDemo2”

Step2

3. “Set as StartUp Project”

Step3

4. Open “Manage NuGet Packages…”

Step4

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”

Step8

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”

Step12

13. Start Debugging (F5). When the NUnit window opens, go to “File”, “Open Project”

Step13

14. Select “SeleniumDemo2.dll” at “SeleniumDemo\SeleniumDemo2\bin\Debug”

Step1

15. Click on “Run” in NUnit window

Step15

Getting Started with Selenium WebDriver, InternetExplorerDriver and C#

This is a step-by-step guide to creating a simple Selenium WebDriver project. When run this program will complete a Bing search in Internet Explorer for the word Selenium. The same program using WatiN rather than Selenium WebDriver can be found in the post Getting Started with WatiN (Part 1).

Prerequisites: MS Visual Studio 2010, NuGet package manager

1. Create a new Console Application with the name “SeleniumDemo1”. Name the solution “SeleniumDemo”

Step 1

2. Open “Manage NuGet Packages…”

Step 2

3. Search for and install “Selenium Webdriver”

Step 3

4. Download “IEDriverServer_Win32_2.33.0.zip” from https://code.google.com/p/selenium/downloads/list

Step 4

5. Unzip to extract the executable “IEDriverServer.exe”

Step 5

6. Copy the code below into the class “Program.cs”. Change the path in InternetExplorerDriver() so that it points to the executable “IEDriverServer.exe” on your machine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace SeleniumDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            BingSearchForSelenium();
        }

        public static void BingSearchForSelenium()
        {
            using (IWebDriver driver = new InternetExplorerDriver(@"C:\Users\olpower\Documents\Selenium"))
            {                
                driver.Url = "http://www.bing.com";
                driver.FindElement(By.Name("q")).SendKeys("Selenium");
                driver.FindElement(By.Name("go")).Click();
            }
        }
    }
}

7. Start Debugging (F5)

If the path to “IEDriverServer.exe” is omitted, a “DriverServiceNotFoundException” is thrown

DriverServiceNotFoundException

If the zoom level of Internet Explorer is not set to 100%, an “InvalidOperationException” is thrown. Set the zoom level to 100% by opening Internet Explorer and using the shortcut Ctrl+0

InvalidOperationException

Handling Modal and Modeless Dialogs with WatiN

As well as pop-up dialogs, WatiN, the web application automation tool, can handle modal and modeless dialogs. Here’s a step by step WatiN demonstration for a modeless dialog.

1. Create a new Console Application named “WatinDemo4” as in the post Getting Started with WatiN (Part 1) and type the following code into “Program.cs”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;

namespace WatinDemo4
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // create a browser instance
            IE browser = new IE();

            // open the html page saved in the debug folder
            browser.GoTo(AppDomain.CurrentDomain.BaseDirectory+"LaunchPage.html");            

            // wait for page to load
            browser.WaitForComplete();

            // click on button to open the dialog window
            browser.Buttons[0].ClickNoWait();

            // find the modal or modeless dialog window by title
            HtmlDialog dialog = browser.HtmlDialog(Find.ByTitle("Bing"));            

            // search for some text
            string searchText = "Dialog box";
            dialog.TextField(Find.ByName("q")).TypeText(searchText);
            dialog.Button(Find.ByName("go")).Click();

            // a new browser instance with the search results will open
            // attach to this browser
            IE resultsBrowser = IE.AttachTo<IE>(Find.ByTitle(searchText + " - Bing"));

            // close the original browser (note the dialog closes too)
            browser.Close();
            browser = null;

            // narrow the results by language
            resultsBrowser.Link(Find.ByText("Narrow by language")).Click();
            resultsBrowser.Link(Find.ByText("Only English")).WaitUntilExists();
            resultsBrowser.Link(Find.ByText("Only English")).Click();
            System.Threading.Thread.Sleep(2000);

            // tidy up
            resultsBrowser.Close();
            resultsBrowser = null;
        }
    }
}

2. Create a file called “LaunchPage.html” and add the text below. Save “LaunchPage.html” in the Debug folder of the project (“WatiNDemo\WatinDemo4\bin\Debug”).

<html>
<head>
	<title>Launch Page</title>
</head>
<body>
	<input onclick="window.showModelessDialog('http://www.bing.com/')" type="button" value="Launch Dialog"/>
</body>
</html>

If you want to experiment with a modal dialog, change “showModelessDialog” to “showModalDialog”.

3. Start Debugging (F5). The “Launch Page” opens and the “Launch Dialog” button is clicked. A dialog opens which contains the Bing search page. The text “Dialog box” is typed into the search box and the search button is clicked.

Dialog Box

The search results open in a new browser.

Results Browser

The search is then narrowed by language to show English results only.