Writing Automation Tool for Web Based System Testing (using Selenium, C#.Net, Internet Explorer)

This tutorial will help to start writing Automation Tool for Testing Web Based System in Internet Explorer.  If you are planning to write a tool to do periodic testing automatically, this tutorial might be helpful.

Step I : Download Selenium Internet Explorer driver from Selenium WebSite

  1. Open the Selenium WebSite
  2. Download ‘The Internet Explorer Driver Server’ .
    Download the matching bits  i.e. 32-bit driver if your system is 32 bit or 64-bit driver if   your sytem is 64-bit driver.
  3. Download ‘Selenium Client & Web Driver Binding’ (Browser:  Internet Explorer and Language: C# ).

    The latest driver at this time of writing can be found at http://selenium.googlecode.com/files/selenium-dotnet-2.39.0.zip . If it is not latest, download the latest version.
  4. After the zip files are downloaded, extract them to local folders.

Step II: Setting Internet Explorer Options

In order to make the tool run smoothly with IE browser, we need to enable the Protected Mode for IE. Follow the steps below to set IE –

  1. Open IE -> Tools -> Internet Options -> Go to Security Tab
  2. Select Internet in the Select Zone pane
  3. Check Enable Protected Mode
  4. Repeat steps ii and iii for Local Intranet, Trusted Sites, Restricted Sites
  5. Click Apply

Step III: Coding

 Now, time to play some code. Excited ? Follow these steps –

  1. Create a Console Based Application in Visual Studio
  2. Add References ( Right Click the Project -> Add Reference -> Browse to Selenium DLL folder in your local computer -> select right version ( e.g. net40 if your .net version is latest -> Select all of the DLLs
    Selenium.WebDriverBackedSelenium.dll
    ThoughtWorks.Selenium.Core.dll
    WebDriver.dll
    WebDriver.Support.dll )
  3. Add Selenium namespaces

    Add following lines at the top of Program.CS

    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Support.UI;

  4. Create WebDriver instance and navigate to Site to be tested

    Add following lines in the Main method

    // path of folder containing webDriver
    IWebDriver driver = new InternetExplorerDriver(@"D:\ananta-tutorial\IEDriverServer_x64_2.37.0");
            driver.Navigate().GoToUrl("http://anantasharma.com");

  5. In the website ( http://anantasharma.com ) , find ‘Software Testing’ link and click it.

    Add following lines at the Main method. WebDriver API provides functions to find the element by ID.

    // find 'Software Testing' link in the website http://anantasharma.com
          IWebElement softwareTestingLi = driver.FindElement(By.Id("menu-item-11"));
    IWebElement softwareTestingLink = softwareTestingLi.FindElement(By.TagName("a"));
      // click on the software Testing link
    softwareTestingLink.Click();

  6. Verify the navigated link

    Add following lines in the Main Method.

      //verify the current link
               //get current URL
                string currentLink = driver.Url;
               if (currentLink.Equals("https://anantasharma.com/category/software-testing/"))
               {    
             Console.Write("Navigated to the expected URL");      
        }
               else
      {
                   Console.WriteLine("Could not navigate to the expected URL");    
         }


  7. Now whole code looks something similar

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Support.UI;

    namespace TestFramework
    {   
    class Program
    {
      static void Main(string[] args)      
      {
         // create driver instance 
            // provide path of folder containing webDriver   
             IWebDriver driver = new InternetExplorerDriver(@"D:\ananta-tutorial\IEDriverServer_x64_2.37.0");     
            driver.Navigate().GoToUrl("http://anantasharma.com");       
         // find 'Software Testing' link in the website http://anantasharma.com  
            IWebElement softwareTestingLi = driver.FindElement(By.Id("menu-item-11"));
             IWebElement softwareTestingLink = softwareTestingLi.FindElement(By.TagName("a"));  
            // click on the software Testing link          
      softwareTestingLink.Click();       
    //verify the current link           
    //get current URL          
    string currentLink = driver.Url;
             if (currentLink.Equals("https://anantasharma.com/category/software-testing/")) 
            {
                 Console.Write("Navigated to the expected URL");   
           }     
           else
             { 
                Console.WriteLine("Could not navigate to the expected URL");
             }
            }   
    }
    }

  8. Run the Program.

    This tool should open the provided URL i.e. http://anantasharma.com and click to ‘Software Testing’ link. If the navigated URL is the expected one, it writes expected message on the console.

Happy Automation using Selenium !!

3 thoughts on “Writing Automation Tool for Web Based System Testing (using Selenium, C#.Net, Internet Explorer)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s