Selenium Grid is used to distribute scripts on multiple browsers and operating system at same time. So that execution time of our scripts is reduced.
Command to run Hub:
java -jar selenium-server-standalone-3.0.1.jar -role hub
Register a Node:
java -jar selenium-server-standalone-3.0.1.jar -role node -hub http://localhost:4444/grid/register
Changing Default Port:
java -jar selenium-server-standalone-3.0.1.jar -role hub -port 4441
Editing Path Variable:
Edit the existing Path system environment variable
Name: Path
Value: ParentPath\LocationOfDriverApplications
Selenium Code 1:
Selenium Code 2:
testng.xml:
Command to run Hub:
java -jar selenium-server-standalone-3.0.1.jar -role hub
Register a Node:
java -jar selenium-server-standalone-3.0.1.jar -role node -hub http://localhost:4444/grid/register
Changing Default Port:
java -jar selenium-server-standalone-3.0.1.jar -role hub -port 4441
Editing Path Variable:
Edit the existing Path system environment variable
Name: Path
Value: ParentPath\LocationOfDriverApplications
Selenium Code 1:
package com.grid.test; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.Platform; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.Test; public class GridDemo { RemoteWebDriver driver; @Test public void gridDemo() throws MalformedURLException{ DesiredCapabilities cap = DesiredCapabilities.firefox(); cap.setBrowserName("firefox"); cap.setPlatform(Platform.VISTA); URL url = new URL("http://192.168.2.9:5555/wd/hub"); driver = new RemoteWebDriver(url, cap); driver.get("http://google.com"); } }
Selenium Code 2:
package com.grid.test; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class SeleniumGrid { RemoteWebDriver driver; DesiredCapabilities cap; URL url; @Parameters({"url", "browser", "platform"}) @Test public void seleniumGrid(String URL, String browserName, String platform) throws MalformedURLException{ switch (browserName) { case "firefox": cap = DesiredCapabilities.firefox(); cap.setBrowserName(browserName); cap.setCapability("platform", platform); url = new URL(URL); break; case "chrome": cap = DesiredCapabilities.chrome(); cap.setBrowserName(browserName); cap.setCapability("platform", platform); url = new URL(URL); break; case "internet explorer": cap = DesiredCapabilities.internetExplorer(); cap.setBrowserName(browserName); cap.setCapability("platform", platform); url = new URL(URL); break; default: break; } driver = new RemoteWebDriver(url, cap); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("selenium grid"); element.sendKeys(Keys.ENTER); } }
testng.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="My Suite" parallel="tests"> <test name="My First Test"> <parameter name="browser" value="firefox"></parameter> <parameter name="url" value="http://192.168.2.9:5555/wd/hub"></parameter> <parameter name="platform" value="VISTA"></parameter> <classes> <class name="com.grid.test.SeleniumGrid"></class> </classes> </test> <test name="My Second Test"> <parameter name="browser" value="chrome"></parameter> <parameter name="url" value="http://192.168.2.9:6666/wd/hub"></parameter> <parameter name="platform" value="VISTA"></parameter> <classes> <class name="com.grid.test.SeleniumGrid"></class> </classes> </test> <test name="My Third Test"> <parameter name="browser" value="internet explorer"></parameter> <parameter name="url" value="http://192.168.2.9:7777/wd/hub"></parameter> <parameter name="platform" value="VISTA"></parameter> <classes> <class name="com.grid.test.SeleniumGrid"></class> </classes> </test> </suite>
The information which you have provided is very good. It is very useful who is looking for selenium Online Training
ReplyDelete