How to Setup Selenium Grid Using WebDriver

Elena Lauren
Aug 24, 2017 · 2 min read

Selenium Grid Using WebDriver

SELENIUM-GRID allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid supports distributed test execution. It allows for running your tests in a distributed test execution environment.
Below are the steps to run your scripts in parallel.

We can run two tests in two different browsers. So this is used for reducing the time. Here Test1 will run in FF and Test2 will run in IE.
We can run the same test in two browsers [same test or different test]. So this is used for cross browser testing. Here test1 will run in IE and FF. Below are the commands to start the server and other ports:

This is to start the HUB
java ?jar selenium?server?standalone?2.24.1.jar ?role hub
Start server on different port(5555):
java ?jar selenium?server?standalone?2.24.1.jar ?role webdriver ?hub
http://localhost:4444/grid/register
Start server on different port(5556):
java ?jar selenium?server?standalone?2.24.1.jar ?role webdriver ?hub
http://localhost:4444/grid/register ?port 5556
To View the console below is the command:
http://localhost:4444/grid/console
Sample Script:
Test1:

package grid;
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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Test1 {
public WebDriver driver;
@Parameters({"browser"})
@BeforeClass
public void setUp(String browser) throws MalformedURLException {
DesiredCapabilities capabilities=null;
if(browser.equalsIgnoreCase("firefox"))
{
capabilities=DesiredCapabilities.firefox();
capabilities.setPlatform(Platform.ANY);
}
if(browser.equalsIgnoreCase("internetExplorer"))
{
capabilities = DesiredCapabilities.internetExplorer();
capabilities.setPlatform(Platform.ANY);
}
System.out.println("Firefox");
driver = new RemoteWebDriver(new
URL("https://localhost:4444/wd/hub"),capabilities);
}
@TEST
public void method1() throws Exception
{
driver.get("http://google.com");
Thread.sleep(15000);
}
@AfterClass
public void tearDown() {
driver.quit();
}
}

Test2:

package grid;
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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Test2 {
public WebDriver driver;
@Parameters({"browser"})
@BeforeClass
public void setUp(String browser) throws MalformedURLException {
DesiredCapabilities capabilities=null;
if(browser.equalsIgnoreCase("firefox"))
{
capabilities=DesiredCapabilities.firefox();
capabilities.setPlatform(Platform.ANY);
}
if(browser.equalsIgnoreCase("internetExplorer"))
{
capabilities = DesiredCapabilities.internetExplorer();
capabilities.setPlatform(Platform.ANY);
}
System.out.println("Internet explorer");
driver = new RemoteWebDriver(new URL("https://localhost:4444/wd/hub"),capabilities);
}
@TEST
public void method2() throws Exception
{
driver.get("http://www.yahoo.com");
Thread.sleep(15000);
}
@AfterClass
public void tearDown() {
driver.quit();
}
}

Below is the Test Suite
Testng1.xml:

<!?? IF you run this test two browsers will open and
two tests will run in two different browsers.
SO this is used for to reduce the time.
here Test1 will run in FF and Test2 will run in IE??>
<!DOCTYPE suite SYSTEM "http://testng.org/testng?1.0.dtd">
<suite name="Suite" parallel="tests" thread?count="10">
<test name="Test" preserve?order="false">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="grid.Test1"/>
</classes>
</test>
<test name="Test1" preserve?order="false">
<parameter name="browser" value="internetExplorer"></parameter>
<classes>
<class name="grid.Test2"/>
</classes>
</test>
</suite>

Testng2.xml

<!?? IF you run this test two browsers will open and
same test will run in two different browsers.
SO this is used for cross browser testing.
here test1 will run in IE and FF??>
<!DOCTYPE suite SYSTEM "http://testng.org/testng?1.0.dtd">
<suite name="Suite" parallel="tests" thread?count="10">
<test name="Test" preserve?order="false">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="grid.Test1"/>
</classes>
</test>
<test name="Test1" preserve?order="false">
<parameter name="browser" value="internetExplorer"></parameter>
<classes>
<class name="grid.Test1"/>
</classes>
</test>
</suite>
)
Elena Lauren

Written by

Passion on Advanced Technologies. Always motive to adapt new innovations and share knowledge through online training.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade