Working with Selenium Grid


Working with Selenium Grid

Hi All, this is another of my lockdown blog posts where I look at how we can use the selenium grid to run multiple tests in parallel. Selenium Grid is another member of the selenium suit that helps to execute your test on multiple nodes on multiple browser types.

To start our scenario we will first download the selenium-server-standalone jar. This jar is needed for us to start both the hub and the nodes. The test is run on the hub but the actual execution happens on the node. However, the same server can have the hub and the node. For this discussion, I will use the same machine for starting both the hub and the node for ease of discussion. One point we need to remember is that the selenium-server-standalone has to be kept on the same folder structure on both the hub and the node.

The first step we need to do is to start the hub on our selenium grid setup. This will start a port for which the nodes can register to. To start the selenium grid hub invoke the command as below. Once the below command is issued the hub should generate the below-shown logs to indicate that the hub is started and is ready for nodes to join.

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

19:45:42.607 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
19:45:42.674 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 4444
2020-04-12 19:45:43.026:INFO::main: Logging initialized @637ms to org.seleniumhq.jetty9.util.log.StdErrLog
19:45:43.151 INFO [Hub.start] - Selenium Grid hub is up and running
19:45:43.151 INFO [Hub.start] - Nodes should register to http://192.168.43.107:4444/grid/register/
19:45:43.151 INFO [Hub.start] - Clients should connect to http://192.168.43.107:4444/wd/hub


Next, we need to start the nodes and the command is similar to how we started the hub. In my example since I have both the hub and the node on my laptop, the host is set as a localhost. The node server should generate the logs as below.

java -Dwebdriver.gecko.driver=/training/training/grid/geckodriver -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register -hubHost localhost -host localhost

19:49:33.978 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
19:49:34.067 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Launching a Selenium Grid node on port 32873
2020-04-12 19:49:34.133:INFO::main: Logging initialized @348ms to org.seleniumhq.jetty9.util.log.StdErrLog
19:49:34.307 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
19:49:34.373 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 32873
19:49:34.373 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Selenium Grid node is up and ready to register to the hub
19:49:34.402 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
19:49:34.665 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://localhost:4444/grid/register
19:49:34.695 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use



Once the node is started and registered, if it's successful you should be able to see a log as shown below.

19:49:34.694 INFO [DefaultGridRegistry.add] - Registered a node http://localhost:32873 


Now we are ready to write our selenium web driver code. As shown below, we create a static variable that will hold the URL to which the clients should connect. Next, we create an instance of the remote web driver and pass the variable that points to the node server URL.

package seleniumgrid;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.*;


public class LoginStep {

    private WebDriver driver;
    private static String nodeURL,baseURL;


    @Given("I am in the login page of the application")
    public void i_am_in_the_login_page_of_the_application() throws java.net.MalformedURLException
    {

        baseURL="http://demo.guru99.com/test/newtours/index.php";
        nodeURL="http://localhost:4444/wd/hub";

        DesiredCapabilities capabilities =DesiredCapabilities.firefox();
        capabilities.setBrowserName("firefox");
        capabilities.setPlatform(Platform.MAC);

        driver = new RemoteWebDriver(new URL(nodeURL),capabilities);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(baseURL);
    }

    @When("I enter valid {string} and {string}")
    public void i_enter_valid_and(String username, String password) {
        driver.findElement(By.name("userName")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.xpath("/html/body/div[2]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/" +
                "table/tbody/tr[2]/td[3]/form/table/tbody/tr[4]/td/table/tbody/tr[4]/td[2]/div/input")).click();
    }

    @Then("I should be taken to the overview page")
    public void i_should_be_taken_to_the_overview_page() {
        String actualTitle = driver.getTitle();
        String expectedTitle = "Login: Mercury Tours";
        Assert.assertEquals(expectedTitle,actualTitle);
        driver.close();
    }
}
 

Comments