Run Test cases by using Selenium Grid

In the previous post, we have seen how to configure hub and node by using Json file. In this post, we will see how to run test cases by using Selenium Grid.

We have already seen how to start the hub and node. We assume that Hub is up and running. Let’s start a node by using below command.

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

Node started in grid

 


Writing program:

You need to use DesiredCapabilities  to set the browser and platform where the test cases will be executed. We have to use RemoteWebDriver to invoke the browser instance. This accepts the url (Selenium Grid web interface) and capabilities as its arguments. 

In one of the class, we are opening Gmail application and another class will open Facebook. Let’s see the code of the classes.

First Class:

package com.testinggrid;

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

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test1 {
	WebDriver driver;
	
	@BeforeTest
	public void init() throws MalformedURLException{
		DesiredCapabilities caps = DesiredCapabilities.firefox();
		caps.setBrowserName("firefox");
		caps.setPlatform(Platform.WIN8_1);
		driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), caps);
	}
	
	@Test
	public void openFacebook(){
		driver.get("http://www.facebook.com");
	}
	
	@AfterTest
	public void closeApplication(){
		driver.quit();
	}
}

Second Class:

package com.testinggrid;

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

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test2 {
	WebDriver driver;
	
	@BeforeTest
	public void init() throws MalformedURLException{
		DesiredCapabilities caps = DesiredCapabilities.firefox();
		caps.setBrowserName("firefox");
		caps.setPlatform(Platform.WIN8_1);
		driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), caps);
	}
	
	@Test
	public void openGmail(){
		driver.get("http://www.gmail.com");
	}
	
	@AfterTest
	public void closeApplication(){
		driver.quit();
	}

}

testng.xml used to run both the classes:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="ExampleTestSuite" parllel="tests" thread-count="2">
<test name="Selenium Grid Example">
    <classes>
      <class name="com.testinggrid.Test1"></class>
      <class name="com.testinggrid.Test2"></class>
    </classes>
  </test>
</suite>

Observe the keyword parllel=”tests” in testng.xml, which is used to run test cases parallel.In our case, it has opened one firefox and one chrome browser instance. It picks up the browser randomly depending on the availability.

Output:

Selenium Grid tests


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...