Using Selenium Grid with Two Nodes-FF & Chrome(HUB)
Steps for Setting Up the Grid
1. Download the Selenium Sever Jar File using the link
2. Pre-requisite for Every Machine
Copy the Selenium Server in every host hub,remote node1,remote node2
3. To check the default port number used by Hub '4444' is reserved or not
Command --> netstat -a -n | findStr 4444
We have to check this command only in hub machine.
4. Starting the Hub. Navigate to the location where the selenium-server jar is downloaded and execute the below command.
java -jar selenium-server-standalone-2.44.0.jar -role hub
5. Starting the Hub using particular port. If the default port '4444' is already in use. Then we have to pass another port number in the command line for the argument -port as mentioned below:
java -jar selenium-server-standalone-2.44.0.jar -role hub -port 4545
6. Verifying the Hub Console
7.Starting Node1: Navigate to the location where selenium server jar is located and execute the below command
java -jar selenium-server-standalone-2.44.0.jar -role webdriver -browser "browserName=firefox,version=35,maxInstances=1,platform=WINDOWS" -hubHost localhost -port 4546
8. Starting Node2: Navigate to the location where selenium server jar is located and execute the below command
java -jar selenium-server-standalone-2.44.0.jar -role webdriver -browser "browserName=chrome,version=40,maxInstances=1,platform=WINDOWS" -hubHost localhost -port 4547 -Dwebdriver.chrome.driver=D:\\Selenium\\chromedriver_win32\\chromedriver.exe
9. Selenium WebDriver Script for FF
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("firefox");
caps.setVersion("35");
caps.setPlatform(Platform.WINDOWS);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
10. Selenium WebDriver Script for CH
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
caps.setVersion("40");
caps.setPlatform(Platform.WINDOWS);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
Selenium WebDriver Program:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class GridTests {
RemoteWebDriver driver;
DesiredCapabilities caps;
@Parameters({"browsertype","url"})
@BeforeTest
public void invokebrowser(String browserType,String url) throws MalformedURLException
{
if(browserType.equals("FF"))
{
caps = DesiredCapabilities.firefox();
caps.setBrowserName("firefox");
caps.setVersion("10");
caps.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
System.out.println("in FF");
//driver =new FirefoxDriver();
}
else if(browserType.equals("IE"))
{
System.out.println("in IE");
File f =new File("IEDriverServer.exe");
System.setProperty("webdriver.ie.driver",f.getAbsolutePath());
driver = new InternetExplorerDriver();
}
else
{
caps = DesiredCapabilities.chrome();
caps.setBrowserName("chrome");
caps.setVersion("40");
caps.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
System.out.println("in CH");
}
driver.get(url);
}
@Test
public void verifyTitle()
{
Assert.assertEquals(driver.getTitle(),"Facebook");
}
}
Selenium WebDriver Program:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class GridTests {
RemoteWebDriver driver;
DesiredCapabilities caps;
@Parameters({"browsertype","url"})
@BeforeTest
public void invokebrowser(String browserType,String url) throws MalformedURLException
{
if(browserType.equals("FF"))
{
caps = DesiredCapabilities.firefox();
caps.setBrowserName("firefox");
caps.setVersion("10");
caps.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
System.out.println("in FF");
//driver =new FirefoxDriver();
}
else if(browserType.equals("IE"))
{
System.out.println("in IE");
File f =new File("IEDriverServer.exe");
System.setProperty("webdriver.ie.driver",f.getAbsolutePath());
driver = new InternetExplorerDriver();
}
else
{
caps = DesiredCapabilities.chrome();
caps.setBrowserName("chrome");
caps.setVersion("40");
caps.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
System.out.println("in CH");
}
driver.get(url);
}
@Test
public void verifyTitle()
{
Assert.assertEquals(driver.getTitle(),"Facebook");
}
}