Wednesday, October 2

How to invoke locally different types of browser web driver using selenium and c# for test Automation scripts


This blog is in sequence to How to blogs related Selenium in C#. In previous blog of this series , we had created a basic web driver test . Different browsers have different webdrivers and invoking them is all nearly similar but there are slight changes.Of three main browsers – Firefox, Google Chrome and IE – firefox comes inbuilt. Hence in the test written in previous blog we could straight away start using firefox browser without downloading driver file.
As seen ,the statement below would be all just necessary to create instance of firefox browser.
IWebDriver driver = new FirefoxDriver();

The same statement for IE or Google chrome would not work as the driver files for these drivers are separate files and do not come embedded with selenium jar. For this one needs to download drivers for IE ( 32 bit or 64 bit ) and Google chrome . These are available from selenium at this location



Save them in desktop location something like C:\WebDrivers. After that they can be invoked by passing  IE or Google chrome driver  path as shown below
IWebDriver driver = new InternetExplorerDriver(@"C:\WebDrivers\IEDriverServer.exe");
IWebDriver driver = new ChromeDriver (@"C:\WebDrivers\chromedriver.exe");

Using above syntax for passing paths of drivers during driver creation, we can write two simple c# selenium tests in visual studio express as below

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Step a
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;

namespace NUnitSelenium
{
    [TestFixture]
    public class UnitTest1
    {      

        [SetUp]
        public void SetupTest()
        {
        }
        
        //Test that uses IE Driver
        [Test]
        public void Test_OpeningHomePageUsingIE()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new InternetExplorerDriver(@"C:\WebDrivers\IEDriverServer.exe");

            //Step c : Making driver to navigate
            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
          
            //Step d 
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();
     
            //Step e
            driver.Quit();

            )
         }
         
         // Test that uses Google chrome driver
         [Test]
         public void Test_OpeningHomePageUsingChrome()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new ChromeDriver (@"C:\WebDrivers\chromedriver.exe");;

            //Step c : Making driver to navigate
            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
          
            //Step d 
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();
     
            //Step e
            driver.Quit();

            )
         }
    }
Another way in windows is to append Path variable with path of drivers. On how to change the path, you can refer link –> How to change Path
Once done, you can test this is working by running a new command window. And it command window just type in IEDriverServer.exe. Then you should get the following out put

image
You can test the same for google chrome and it should work. Once this setup is done, then on the computer where this PATH has been set, we need to have the code pointing to location of driver exe files, instead then can just have no arguments . Then the tests would like as below

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Step a
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;

namespace NUnitSelenium
{
    [TestFixture]
    public class UnitTest1
    {      

        [SetUp]
        public void SetupTest()
        {
        }
        
        //Test that uses IE Driver
        [Test]
        public void Test_OpeningHomePageUsingIE()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new InternetExplorerDriver();

            //Step c : Making driver to navigate
            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");       
    
            //Step d 
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();
     
            //Step e
            driver.Quit();

            )
         }
         
         // Test that uses Google chrome driver
         [Test]
         public void Test_OpeningHomePageUsingChrome()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new ChromeDriver ();;

            //Step c : Making driver to navigate
            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
          
            //Step d 
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();
     
            //Step e
            driver.Quit();

            )
         }
    }





Previous Blogs related selenium webdriver with C#
1. How to setup C#,nUnit and selenium client drivers on VSExpress for Automated tests
2. Creating Basic Selenium web driver test case using Nunit and C#

No comments:

Post a Comment

---------------------------------------------

Related Posts Plugin for WordPress, Blogger...