Sunday, October 6

How to invoke/run different type of selenium web driver browser using remote webdriver in C# automation scripts


Remote web driver feature in selenium is very advantages and helps to achieve distributed testing.  Distributed testing comprises testing different browsers on different machine and simultaneous run of tests  on number of machines. As a basic lets try to understand how we can we invoke a test case in different browser that is located on different machine using remote webdriver. Declaration for remote webdriver is as below for c# code
IWebDriver driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.InternetExplorer());



The first parameter Uri is the remote address on which selenium is running. In the above example it is running on machine with IP 127.0.0.1(i am running it locally) on port 4444. The second parameter is the capabilities of the browser you going to use. To have successful execution it requires to run selenium on the remote machine and that instance selenium should know where the browser driver are located. This can achieved by using parameter –DWebdriver.ie.driver and assigning it to that of IE driver . Similarly for one can use –DWebdriver.chrome.driver for Chrome driver.  Below are the steps and command to be executed to achieve this

1. Open a new command prompt window
2. Navigate to the directory where you have selenium standalone server jar is downloaded .(if you dont have one already you can download from link
3. Then run the following command in the command window ( the path value shown in blue has to replaced with the path of the drivers where you stored them)

C:\Data\seleniumCsharp\Drivers>java -jar selenium-server-standalone-2.35.0.jar -Dwebdriver.chrome.driver=C:\Data\seleniumCsharp\Drivers\chromedriver_win32_2.3\c
hromedriver
-Dwebdriver.ie.driver=C:\Data\seleniumCsharp\Drivers\IEDriverServer.
exe

This should get the selenium server up and running.One can fire requests from different machine using IP address of machine thats get listed when run ipconfig in command window, alternatively you can fire request from same machine using IP 127.0.0.1. .In this case although selenium standalone server is running on same machine, from your scripts if you access it via IP address 127.0.0.1, it mimics the same process as if you were accessing a standalone server on different machine. Once you started the server, you can check if it is running by opening this url http://127.0.0.1:4444/wd/hub (or from different machine using http://<machineIP>/wd/hub) and it should not throw any error.

So you have so far have setup selenium which can recieve commands through IP 127.0.0.1 and port 4444. Next lets create a class library project in Visual studio express. Say you name it as LanuchRemoteBrowser.  The below code shows the declaration of both types of browser and how to invoke it with desired capabilities.

using System;
//Step ausing OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using OpenQA.Selenium.Remote;

namespace LanuchRemoteBrowser
{
    [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 RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.InternetExplorer());
            //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 RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),DesiredCapabilities.Chrome());
            //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();
            }
         }
    }

Before compiling this code, you need to reference the appropriate dlls from selenium and nunit. For traditional method of doing this, you can refer my previous blog - How to setup C#,nUnit and selenium client drivers on VSExpress for Automated tests. Alternatively there in another method of adding packages or references to you project using NuGet. Installation of NuGet is straight forward and can installed from Tools > Extentions and Updates. On Extension and Updates window, click on Online –> visual studio gallery and then in search box enter Nuget. Then from list, select NuGet Package manager and install it. 

image

Now from Tools –> library package manager, click on Package Manager console.

image

From package manager power shell windows run following commands to add the dependencies/packages using NuGet

    1. Install-Package Selenium.WebDriver
2. Install-Package Selenium.Support
3. Install-Package Selenium.nUnit

Once you run the above commands you would be having the output in powershell window as shown below

image

Next compile the above code  by clicking Build > Build solution and open the dll created in debug directory after compilation via nUnit to run the tests.

No comments:

Post a Comment

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

Related Posts Plugin for WordPress, Blogger...