Sunday, May 14, 2017

Tutorial : Setting up Selenium GRID in LAN

Trying to learn and set up selenium GRID in local network? Here is the simple tutorial to start with.

Selenium GRID - components

  1. Selenium GRID Hub
  2. Selenium GRID Node

Step 1: Start Selenium GRID HUB Server 

java  -jar selenium-server-standalone-3.3.1.jar -role hub

After running this command you will a message as  below

16:55:49.766 INFO - Selenium Grid hub is up and running
17:16:34.636 INFO - Nodes should register to http://192.168.0.128:4444/grid/register/

Step 2: Start Selenium GRID NODE

2.1 Go to node machine & node configuration file with below JSON

FileName: nodeConfiguration.json


   "capabilities":[ 
      { 
         "browserName":"chrome",
         "maxInstances":10,
         "platform":"WIN8",
         "version":"58",
         "seleniumProtocol":"WebDriver"
      },
      {
         "browserName":"firefox",
         "maxInstances":3,
         "platform":"WIN8",
         "version":"53",
         "seleniumProtocol":"WebDriver"
      }
   ],
   "debug":false,
   "proxy":"org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
   "register":true,
   "registerCycle":5000,
   "maxSession":6
}

 2.2 Execute below command to start node and connect to hub

java -Dwebdriver.chrome.driver=C:\Users\vikas\selenium_drivers\chromedriver.exe -Dwebdriver.gecko.driver=c:\geckodriver.exe -jar selenium-server-standalone-3.3.1.jar -role node -nodeConfig nodeConfigFile.json -hub http://192.168.0.128:4444/grid/register

Step 3: Confirm Node is connected and hub is running by opening below url in browser

http://192.168.0.128:4444/grid/console

Step 4: Run Test case from eclipse. below is the sample code.

package com.vikas.samples;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.tools.ant.taskdefs.XSLTProcess.TraceConfiguration;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.vikas.framework.core.Browser;

public class GridExecutionDemo {

    @Test(dataProvider = "multipleTests")
    public void testOnRemmoteChromeBrowser(int testNumber, String browserName) throws MalformedURLException {
      
        // WebDriver driver = new ChromeDriver();
        System.out.println("Starting test :" + testNumber);
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setBrowserName(browserName);
        caps.setPlatform(Platform.WIN8);
        // OR caps.setCapability("browserName", "chrome");
        URL hubUrl = new URL("http://192.168.0.128:4444/wd/hub");

        WebDriver driver = new RemoteWebDriver(hubUrl, caps);
        driver.get("http://google.com");
        driver.findElement(By.name("q")).sendKeys("Vikas Thange Selenium");

        driver.quit();
    }

    @DataProvider(parallel = true)
    public Object[][] multipleTests() {
        return new Object[][] { { 1, "firefox"}, { 2, "chrome" } };
    }
}

No comments:

Post a Comment