Monday, July 23, 2018

Setup Selenium Grid

With Selenium Grid you can create a simple infrastructure of various browsers on different operating systems to not only distribute test load but also give you a diversity of browsers to work with.

 1) Download Selenium Standalone Server
     You man download Selenium standalone server jar file from here. And put the jar file in your disk folder.

2) Start Hub Machine
     Open command line window and navigate to the folder of jar file and run the command below.
     java -jar selenium-server-standalone-3.13.0.jar -role hub
     The hub will be running on default port 4444, you may access the link http://localhost:4444 to check the hub status.

3) Start Node Machine
     Navigate to the folder of jar file on the node machine in command line window, and run the command below.
     java -jar selenium-server-standalone-3.3.1.jar -role node -hub   
            http://<hub>:4444/grid/register -port 5555
     It will start a node on port 5555 and register on the hub. Then you may find it on the hub machine status page.

4) Connect to Remote Driver
     DesiredCapabilities cap = DesiredCapabilities.firefox();
     WebDriver driver = new RemoteWebDriver(new URL("http://<hub>:4444/wd/hub"), cap);

Here is an example test case.

package me.simplejavautomation;

import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;

import me.simplejavautomation.pages.PercentageCalculatorPage;

public class GridTest {

    private WebDriver driver;

    @Before
    public void setUp() throws Exception {
        String hub = "http://192.168.1.61:4444/wd/hub";
        DesiredCapabilities cap = DesiredCapabilities.firefox();
        driver = new RemoteWebDriver(new URL(hub), cap);
    }

    @After
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testPercentageCalculation() {
        // open http://www.percentagecalculator.co/ web site
        driver.get("http://www.percentagecalculator.co/");

        PercentageCalculatorPage pageObject = PageFactory.initElements(driver,
             PercentageCalculatorPage.class);
        pageObject.getA().sendKeys("10");
        pageObject.getB().sendKeys("100");

        assertEquals("10", pageObject.getC().getAttribute("value"));
    }

}

No comments:

Post a Comment