Selenium-Grid allows you run your tests on remote desktop against different Operating Systems like Windows,Linux,MAC and against different browsers like Opera,Safari,IE,Firefox and Chrome in parallel. Selenium-Grid allows running your tests in a distributed test execution environment.
Grid Architecture:
In Selenium-Grid, the Component Hub acts as a server receives the requests from Selenium WebDriver program and redirects requests to the Nodes which are configured to Hub. Refer to the details in the diagram mentioned below:
Installation:
Ensure the Selenium Standalone Server Jar is downloaded in Hub and Node Systems as well.
Download the Selenium Standalone Server Jar from this link -> http://selenium-release.storage.googleapis.com/index.html
Starting Hub:
The Hub is the central point that will receive all the test request and distribute them the the right nodes.
Open the new command line and execute the below command:
java -jar selenium-server-standalone-xxx.jar -role hub -port 4545
Starting Node 1:
Use the same http://selenium-release.storage.googleapis.com/index.html jar file to start the nodes and connect to the Hub.
Capabilities for Node 1:
- Platform : WINDOWS
- Browser Name: firefox
- Browser Version:123
Open the new command line and execute the below command:
java -Dwebdriver.gecko.driver=”C:\\XXXX\\geckodriver-v0.23.0-win64\\geckodriver.exe”
-jar selenium-server-standalone-3.141.59.jar
-role webdriver
-browser “browserName=firefox,version=123,maxInstances=1,platform=WINDOWS”
-hubHost localhost
-hubPort 4444
-port 4546
Starting Node 2:
Use the same http://selenium-release.storage.googleapis.com/index.html jar file to start the nodes and connect to the Hub.
Capabilities for Node 2:
- Platform : WINDOWS
- Browser Name: chrome
- Browser Version:40
Open the new command line and execute the below command:
java -Dwebdriver.chrome.driver=”C:\\XXXX\\chromedriver_win32\\chromedriver.exe”
-jar selenium-server-standalone-3.141.59.jar
-role webdriver
-browser “browserName=chrome,version=40,maxInstances=1,platform=WINDOWS”
-hubHost localhost
-port 4547
Selenium WebDriver Testng Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package totalqa; 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 GridEx { @Test public void executeTest() throws MalformedURLException { DesiredCapabilities caps = new DesiredCapabilities(); caps.setBrowserName("firefox"); caps.setVersion("123"); caps.setPlatform(Platform.WINDOWS); // Comment below lines of code to execute tests on Chrome // caps.setBrowserName("chrome"); // caps.setVersion("40"); // caps.setPlatform(Platform.WINDOWS); RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps); driver.get("http://www.google.com"); System.out.println(driver.getTitle()); } } |
Results:
To check the ports which are reserved
To verify that the port number 4444 is in use. Execute the below command in the command line:
netstat -a -n | findStr 4444
Conclusion:
Selenium Grid is useful to run test cases remotely with specific capabilities.