Thursday, January 14, 2016

How to Implement Selenium Grid

       •Execute multiple tests in parallel.      
 •Faster verification process.
 •The selenium-server-standalone package includes the Hub, WebDriver, and legacy RC needed to run the grid.



                  Setup

Step 1: Start the hub.

Step 2: Start the nodes.
Step 3: Execute tests.

Step 1: Start the hub


HUB:The Hub is the central point that will receive all the test request and distribute them the right nodes.

Open a command prompt and navigate to the directory where you copied the selenium-server-standalone file. Type the following command:
java -jar selenium-server-standalone-2.14.0.jar -role hub

The hub will automatically start-up using port 4444 by default. To change the default port, you can add the optional parameter –port

When you run the command. You can view the status of the hub by opening a browser window and navigating to: http://localhost:4444/grid/console

Step 2: Start the Node


Regardless on whether you want to run a grid with new WebDriver functionality, or a grid with Selenium 1 RC functionality, or both at the same time, you use the same selenium-server-standalone jar file to start the nodes.


java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://localhost:4444/grid/register


Note: The port defaults to 5555 if not specified whenever the "-role" option is provided and is not hub.


java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://localhost:4444/grid/register -port 5556

Creating Batch Files:



 Grid Setup :
start LaunchHub.bat
ping -n 10 localhost
start IENode.bat
ping -n 10 localhost
start LaunchChromeNode.bat
ping -n 10 localhost
start LaunchFireFoxNode.bat
ping -n 10 localhost



launch Hub :

cd C:\
cd javaserver
set HUB_URL=http://172.16.148.29:4444/grid/register
set SELENIUM_VERSION=2.42.2
set browsertimeouttime=800000
Set newsessiontimeout=10000

start java -jar selenium-server-standalone-%SELENIUM_VERSION%.jar -role hub

For Chrome: 

cd C:\
cd JavaServer
set HUB_URL=http://172.16.148.29:4444/grid/register
set SELENIUM_VERSION=2.42.2
set portNumber=5567
set CHROME_DRIVER_LOC="C:\Drivers\chromedriver.exe"
java -jar selenium-server-standalone-%SELENIUM_VERSION%.jar -role webdriver -hub http://172.16.148.29:4444/grid/register -port 6257 -browser browserName=chrome,platform=WINDOWS,maxInstances=5  -Dwebdriver.chrome.driver=%CHROME_DRIVER_LOC%

IE Node:

cd C:\
cd JavaServer
set HUB_URL=http://172.16.148.29:4444/grid/register
set SELENIUM_VERSION=2.42.2
set portNumber=5565
set IE_DRIVER_LOC="C:\Drivers\IEDriverServer.exe"
java -jar selenium-server-standalone-%SELENIUM_VERSION%.jar -role webdriver -hub http://172.16.148.29:4444/grid/register -port 6256 -browser browserName=iexplorer,platform=WINDOWS,maxInstances=5 -Dwebdriver.ie.driver=%IE_DRIVER_LOC%

C# Code to intialize the Remote web Driver 


            switch ("Chrome")
            {

                case "firefox":

                    capabilities = DesiredCapabilities.Firefox();
                    webDriver = new RemoteWebDriver(new Uri(" http://localhost:4444/wd/hub"), capabilities);

                    break;

                case "iexplore":
                    capabilities = DesiredCapabilities.InternetExplorer();
                    capabilities.SetCapability("platform", "WINDOWS");
                    capabilities.SetCapability("browserName", "iexplorer");
                    //  capabilities.(
                    webDriver = new RemoteWebDriver(new Uri(" http://localhost:4444/wd/hub"), capabilities);
                    webDriver.Navigate().GoToUrl("www.google.com");
                    break;

                case "Chrome":
                     capabilities = DesiredCapabilities.Chrome();
                   capabilities.SetCapability("platform", "WINDOWS");
                    capabilities.SetCapability("browserName", "chrome");
                    capabilities.SetCapability("Dwebdriver.chrome.driver", @"C:\Drivers\chromedriver.exe");
                    webDriver = new RemoteWebDriver(new Uri(" http://localhost:4444/wd/hub"), capabilities);
                    webDriver.Navigate().GoToUrl("http://www.seeques.com/25202262/chrome-on-remote-webdriver-via-grid-failed-to-start.html");

                    webDriver.Navigate().GoToUrl("www.google.com");
                    break;
            }

            return webDriver;
Note:
1.        Driver version should be based on your OS bit(32 or 64 bit machine)
2.       Node configuration and C# code configuration should match.( Otherwise  new session exception will come)

No comments:

Post a Comment