Wednesday, May 9, 2012

Using Seleniun WebDriver with Windows and .NET

Wanting to write a C# program to do some automated functional testing in multiple browsers, I downloaded Selenium WebDriver version 2.21.0.

I also downloaded the Selenium Client Drivers for C#. At the time I first downloaded these, the version was 2.16.0.

I proceeded to create a Selenium hello-world program by following the C# example provided here. With all the comments and fluff stripped out, my program looked something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace WebDriver1
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("
http://www.google.com/");
            IWebElement query = driver.FindElement(By.Name("q"));
            query.SendKeys("Cheese");
            query.Submit();
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
            System.Console.WriteLine("Page title is: " + driver.Title);
            driver.Quit();
        }
    }
}

One note right off the bat: The example code at seleniumhq.com doesn't include using OpenQA.Selenium.Support.UI. This is needed, or else WebDriverWait can't be resolved.

When I tried to run this program, it threw an exception: OpenQA.Selenium.WebDriverException : Failed to start up socket within 45000. I found some stuff about this online, but none of it helped. What solved the problem was downloading the latest version, 2.21.0, of the Selenium Client Drivers.

With those two problems solved, the program compiled and ran, launching Firefox, doing a Google search for "cheese," and outputting the page title. Pretty cool!

Next, I wanted to do the same thing using IE.

The first stumbling block was when I added this line to my code: driver = new InternetExplorerDriver();

This class wasn't found. Easily remedied by adding this as well: using OpenQA.Selenium.IE;

My whole program now looked like this, executing a loop twice, once for each browser:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;

namespace WebDriver1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 2; i++)
            {
                string browserName = "";
                IWebDriver driver = null;
                switch (i)
                {
                    case 0:
                        browserName = "FireFox";
                        driver = new FirefoxDriver();
                        break;
                    case 1:
                        browserName = "IE";
                        driver = new InternetExplorerDriver();
                        break;
                }
                driver.Navigate().GoToUrl("
http://www.google.com/");
                IWebElement query = driver.FindElement(By.Name("q"));
                query.SendKeys("Cheese");
                query.Submit();
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
                System.Console.WriteLine(browserName + ": Page title is: " + driver.Title);
                driver.Quit();
            }
        }
    }
}

This worked for Firefox but threw an exception when it opened Internet Explorer.
System.InvalidOperationException: Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones. (NoSuchDriver)

Surprisingly, this meant exactly what it said, and was corrected by opening IE, selecting Tools | Internet Options | Security, clicking on all four security zones (Internet, Local Intranet, Trusted Sites and Restricted), ensuring the Enable Protected Mode checkbox was checked for each zone, and restarting IE. (Unchecking the checkbox would work, too. The point is that the setting for all four zones must be the same.) A huge tip of the hat to Tom Dupont for that.

With IE's Protected Mode settings properly configured, the above program ran successfully, automating both browsers.

1 comment:

  1. PS: You must have Firefox and IE installed to run this program.

    ReplyDelete