Friday 19 February 2016

Selenium Grid architecture


Selenium grid allows you to run the same tests on different platforms in parallel. Just imagine that you have a web application that can be accessed from desktop, laptop, iPad, iPhone, Android phone or any other mobile device.

Testing the same application on all these platforms is a challenging task and time consuming as well. But with Selenium Grid you can test your application on various platforms very quickly. The beauty about selenium is that you do not need to write different code for running tests on various configurations. Same selenium code works on all platforms.


Components of  Selenium Grid -
  1. Hub - Selenium Server - Passes on JSON commands to capable nodes. A single HUB controls all nodes. HUB knows what are the capabilities of each node are like OS, browser etc.
  2. Node - Selenium Server driving actual browser. On node actual automation execution happens. Each node registers itself with the HUB.
  3. Test machine - Where your test code is executed.  
Steps in Selenium Grid automation execution
  1. You run your selenium code from editor like IntelliJ IDEA or eclipse
  2. Your code send http commands to HUB including information like what the desired capabilities are. For example - you may ask for remote webdriver that can run tests on Windows OS and chrome browser. 
  3. HUB reads the capabilities information and forwards the commands to the capable node.
  4. Node creates a new session and returns the result to HUB.
  5. HUB returns the result to your code.
  6. All other commands are executed in the same session 

To view architectural diagram, click here.

Starting Hub

You can start the hub using below command.

java -jar server.jar -role hub

You can also use the below command to start the hub with json configuration

java -jar server.jar -role hub -hubConfig myconfig.json





Starting Node

You can start the node using below command.

java -jar server.jar -role node -hub http://localhost:4444/grid/register -port 3333

You can also register the node using json file.

java -jar server.jar -role node -nodeConfig myconfig.json


Here is the sample node configuration json file.

{
"capabilities":
                [
                {
                                "platform": "WINDOWS",
                                "browserName": "chrome",
                                "maxInstances": 1,
                                "seleniumProtocol": "WebDriver"
                },
                {
                                "platform": "WINDOWS",
                                "browserName": "firefox",
                                "maxInstances":1,
                                "seleniumProtocol": "WebDriver"
                },
                {
                                "platform": "WINDOWS",
                                "browserName": "internet explorer",
                                "maxInstances": 1,
                                "seleniumProtocol": "WebDriver"
                }
                ],
  "configuration":
                {
                                "cleanUpCycle": 6000,
                                "registerCycle": 6000,
                                "nodeTimeout": 1000,
                                "timeout": 500000,
                               "nodePolling": 3000,
                               "hub": "http://localhost:4444/grid/register",
                               "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
                               "maxSession": 2,
                               "port": 6666,
                               "host": "nodeName",
                               "register": true,
                               "hubPort": 4444,
                               "hubHost" :"localhost",
                               "role": "node"
                }
}





Once Hub and node is up and running, you can view your grid at below url
http://localhost:4444/grid/console




To register the Appium node you can use below command.

appium --nodeconfig mynodeconfig.json

Here is the sample iPhone node config file.

{
    "capabilities": [
        {
            "browserName": "iPhone-Simulator",
            "version": "7.1",
            "maxInstances": 1,
            "platform": "MAC"
        }
    ],
    "configuration": {
        "cleanUpCycle": 3000,
        "timeout": 400000,
      "browserTimeout": 70000,
        "hub": "http://localhost:4444/grid/register",
        "host": " iPhoneHostName",
        "maxSession": 1,
        "port": 1234,
        "hubPort": 4444,
        "hubHost": " localhost",
        "url": "http:// iPhoneHostName:1234/wd/hub",        
       "register": true,
       "registerCycle": 6000,
        "role": "node"
    }
}

Here is the sample json configuration file for Android

{
"capabilities":
[
{
"deviceName": "sagarcell",
"browserName": "Android",
"version":"4.4.4",
"maxInstances": 1,
"platform":"ANDROID"
}
],
"configuration":
{
"cleanUpCycle":300000,
"timeout":400000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url":"http://appium:4730/wd/hub",
"maxSession": 1,
"port": "4730",
"host": "appium",
"register": true,
"registerCycle": 3000,
"hubPort": "4444",
"hubHost": "127.0.0.1"
}
}

Executing Code

Below example shows how to execute the selenium tests in Grid.

package browsertests;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class TestFirefoxGrid
{
    private WebDriver driver;

    @Test
    public void loadingSinglePageTest() throws Exception{

        driver = new RemoteWebDriver(
                new URL("http://localhost:4444/wd/hub"),
                DesiredCapabilities.firefox());
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.softpost.org");
        driver.quit();
        //quit the browser
    }
}


Below example shows how to execute the selenium tests on Android phone in Grid.

package browsertests;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class LaunchWebsiteOnAndroidPhoneGrid
{
    private WebDriver driver;

    @Test
    public void loadingSinglePageTest() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "ANDROID");
        capabilities.setCapability("platformVersion", "4.4.4");
        capabilities.setCapability("browserName", "MobileBrowserType.BROWSER");
        capabilities.setCapability("app", "chrome");
        WebDriver driver = new RemoteWebDriver(
                new URL("http://localhost:4444/wd/hub"),
                capabilities);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.softpost.org");
        driver.quit();
        //quit the browser
    }
}


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Click not working in Selenium Webdriver

Sometimes click method of selenium does not work. In such situations, you can use below solutions.
  1. Use javascript 
  2. Use Actions class
By using javascript, you can click on any element.

WebElement element = driver.findElement(By.linkText("Log in")); 
jsClick(element);

public static void jsClick(WebElement element){ 
 ((JavascriptExecutor) driver).executeScript
("arguments[0].click();",element);
 }

By using Actions, you can click on any element.

WebElement element = driver.findElement(By.linkText("Log in"));
clickByActions(element);

public static void clickByActions(WebElement element)
{ 
    Actions builder = new Actions(driver); 
    Action click = builder.click(element).build();
    click.perform(); 
}


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

How to scroll to element in Selenium Webdriver

When automating the iOS applications running on iPad or iPhone in Safari, you encounter exceptions like element is not visible. But the same code works fine on other browsers like chrome and firefox.
So what is causing the issue in safari?

To fix this issue, you need to scroll to element and then work on that element.
Below method will accept the element and scroll to it.

public void scrollToElement(WebElement element){
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();",element)
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,-100)")
}
  

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Buy Best Selenium Books

Contributors