Explaining how the real world works!     RSS Feed The Automated Tester on Twitter The Automated Tester on LinkedIn The AutomatedTester on github

Selenium Two Tutorial using C#/NUnit and InternetExplorerDriver

Sat 09 Jan 2010

With the hard work for Selenium 2 going on I thought that I would start creating new tutorials to get people used to the idea of using Selenium 2 and have a few examples as well. This tutorial will tell you how to set up your first test using the Selenium-WebDriver code. NOTE: This tutorial requires that you have some programming knowledge and will be able to read the code below.

You will need to download NUnit and at least Visual Studio Express c# edition to complete this tutorial. They are free to download and use. You will also need to download the .NET Bindings from Google Code

This tutorial will not be using the Remote Driver and it will be using the InternetExplorerDriver as this is the only complete browser at the moment that doesn't need to be built from the Repository.

  1. In Visual Studio lets create a new project. You will have to create a new class library by going New> New Solution> Class Library and call it Selenium.Two.Example
  2. Add a reference to the NUnit Framework by right-clicking on the solution and clicking add reference. Click on the browse tab and navigate to <%nunithome%>\bin and select nunit.framework.dll . This allows you now to create your first NUnit test.
  3. Add a reference to the WebDriver.Common.dll and WebDriver.IE.dll as well as they are needed to driver the browser.
  4. We now need to get main part of Selenium-WebDriver added to the project. Do this by right-clicking on the Solution doing Add > Existing Item and select InternetExplorerDriver.dll that correspondes with the system architecture that you will be using. E.g. If you have a 64-bit version of Windows use the x64 version of InternetExplorerDriver.dll, if you have 32-bit version of Windows use the Win32 version.
    You should now see the DLL in the Solution Explorer like the image below.
    InternetExplorerDriver.dll in the Solution
  5. We now need to make sure that this DLL is placed in the correct place when building the solution. Right-click on InternetExplorerDriver.dll and click properties. Change the "Copy to Output Directory" to "Copy if newer" so that it looks like below. This will make sure that the DLL, which is not a COM or .NET DLL, is placed with the rest of the built solution.
    InternetExplorerDriver.dll Properties
  6. We are now ready to have our write our first test!Copy the code below into a class and then compile it.
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
     
    using NUnit.Framework;
     
    namespace Selenium.Two.DotNetExample
    {
        [TestFixture]
        public class Test_Google
        {
    	    IWebDriver driver;
    	     
    	    [SetUp]
    	    public void Setup()
    	    {
    		    driver = new InternetExplorerDriver();
    	    }
    	    
    	    [TearDown]
    	    public void Teardown()
    	    {
    		    driver.Quit();
    	    }
        		
            [Test]
            public void TestSearchGoogleForTheAutomatedTester()
    	    {
    	        //Navigate to the site
                driver.Navigate().GoToUrl("http://www.google.co.uk");
                //Find the Element and create an object so we can use it
                IWebElement queryBox = driver.FindElement(By.Name("q"));
                //Work with the Element that's on the page
                queryBox.SendKeys("The Automated Tester");
    			queryBox.SendKeys(Keys.ArrowDown);
                queryBox.Submit();
                //Check that the Title is what we are expecting
                Assert.True(driver.Title.IndexOf("The Automated Tester") > -1);
            }
        }
    }
            
  7. Now you have a test that launch a browser and do a search on Google for you. Load the DLL created when you build the Solution into NUnit and click run to see the test run.

Now you have created your first test that uses WebDriver.

    Area: tutorials

blog comments powered by Disqus