Welcome to the concluding piece of our comprehensive Selenium tutorial series. This guide will familiarize you with Selenium Grid, a distributed testing environment that enables quicker test execution. Further, we will uncover the process of conducting cross-browser tests using Selenium Grid.
Understanding Selenium Grid and Cross-Browser Testing
As we draw closer to concluding our detailed Selenium tutorial series, we are thrilled to present this final guide on Selenium Grid. We will look into how Selenium Grid can expedite your test case execution by spreading them across various machines, as well as discuss executing cross-browser tests using Selenium Grid.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Guide Contents
Content Index:
The Need for Selenium Grid?
Perusing our Selenium WebDriver tutorials, you might have noticed that WebDriver runs test cases on a singular machine. Nonetheless, several issues crop up with this setup:
- Test cases executing on diverse operating systems
- Test cases running on different versions of the same browser
- Test cases executing on multiple browsers
- Scenarios awaiting the execution of other test cases, regardless of their interdependence
Selenium Grid addresses all these challenges. We will delve into how Selenium Grid resolves these issues in the following sections. Built on a master-slave architecture, Selenium Grid’s design allows the master machine to distribute test cases to different slave machines.
Selenium Grid comes in two versions: Selenium Grid 1.0 and Selenium Grid 2.0. The more recent edition, Selenium Grid 2.0, supports both Selenium RC and Selenium WebDriver scripts, making it the preferred choice for most experts due to its extra features.
Merits of Selenium Grid
Selenium Grid provides several advantages for test automation:
- Ability to distribute test cases for execution
- Reduced batch processing time
- Multi-browser testing support
- Multi-OS testing support
In the subsequent segments, we will delve deep into the setup and configuration of Selenium Grid.
Installing Selenium GRID
Here’s how to install Selenium Grid:
- Download the Selenium Server jar file from the Selenium HQ website and save it to a local disk. You can locate the download link at http://www.seleniumhq.org/download/.
- Open command prompt and navigate to the folder with the server file. Start the server by running the following command:
java -jar selenium-server-standalone-2.41.0.jar -role hub
- To verify the hub’s status, open a web browser and navigate to http://localhost:4444/grid/console.
- For node setup, go to the machines where you intend to install the nodes. Open the command prompt and execute this command:
java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://localhost:4444/grid/register -port 5556
- Start the selenium server on every machine to initiate the nodes.
Setting up Browsers and Nodes
After setting up the hub and nodes across all machines, you need to configure the browsers for testing. Chrome, Firefox, and Internet Explorer are among the well-liked browsers that Selenium Grid supports.
To employ a particular browser, launch the node with the corresponding command:
For Internet Explorer:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
For Mozilla Firefox:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox
For Google Chrome:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=chrome
Using the maxInstances
parameter, you can also define the maximum number of browser instances. To operate two instances each of Firefox and Internet Explorer, employ this command:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox,maxInstance=2 -browser browserName=iexplore,maxInstance=2
In a similar fashion, you can set up other browsers and their instances based on your needs.
maxSession
The maxSession
parameter is employed to set the maximum number of browsers that can concurrently operate on a remote system. To configure a maximum of three sessions for Chrome and Firefox, use the following command:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,maxInstance=3 -browser browserName=firefox,maxInstance=3 –maxSession 3
This configuration allows for the simultaneous execution of up to three browser sessions.
Sample Grid Code
To execute your test cases in parallel on different browsers, you can use this example code using TestNG:
public class GridExample { @Test public void mailTest() throws MalformedURLException { DesiredCapabilities capabilities; if (browserType.equals("firefox")) { capabilities = DesiredCapabilities.firefox(); capabilities.setBrowserName("firefox"); capabilities.setPlatform(Platform.WINDOWS); } else { capabilities = DesiredCapabilities.internetExplorer(); capabilities.setBrowserName("iexplore"); capabilities.setPlatform(Platform.WINDOWS); } RemoteWebDriver driver = new RemoteWebDriver(new URL("https://localhost:4444/wd/hub"), capabilities); driver.navigate().to("http://gmail.com"); driver.findElement(By.xpath("//input[@id='Email']")).sendKeys("username"); driver.findElement(By.xpath("//input[@id='Passwd']")).sendKeys("password"); driver.close(); } }
As seen in the example above, TestNG is utilized to run a sample test case on Selenium Grid. The test case logs into Gmail and inputs the username and password. The RemoteWebDriver class is used specifically for Selenium Grid. The browser and platform are set as per the desired capabilities.
To adjust your tests for parallel execution on different browsers, the testng.xml file needs to be updated. Here’s an example of a testng.xml suite used to run the aforementioned test case serially on varied browsers:
<suite name="GRID SAMPLE TEST" thread-count="2"> <test name="GRID TEST WITH SERIAL EXECUTION WITH BROWSER IE"> <parameter name="browserType" value="IE"/> <classes> <class name="GridExample"/> </classes> </test> <test name="GRID TEST WITH SERIAL EXECUTION WITH BROWSER FF"> <parameter name="browserType" value="firefox"/> <classes> <class name="GridExample"/> </classes> </test> </suite>
To execute tests in parallel, modify your testng.xml file as follows:
<suite name="GRID SAMPLE TEST" parallel="tests" thread-count="3"> <test name="GRID TEST WITH PARALLEL EXECUTION WITH BROWSER FF"> <parameter name="browserType" value="firefox"/> <classes> <class name="GridExample"/> </classes> </test> <test name="GRID TEST WITH PARALLEL EXECUTION WITH BROWSER IE"> <parameter name="browserType" value="IE"/> <classes> <class name="GridExample"/> </classes> </test> </suite>
In the parallel execution configuration, the parallel
attribute is set as “tests” and the thread-count
attribute establishes the maximum number of concurrent threads to be executed.
Configuration with a JSON File
You can also boot up Selenium Grid with its configuration using a JSON setup file. To utilize this functionality, adhere to these steps:
Start by creating a JSON file with the desired configuration. For instance:
{ "host": null, "port": 4444, "newSessionWaitTimeout": -1, "servlets": [], "prioritizer": null, "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher", "throwOnCapabilityNotPresent": true, "nodePolling": 5000, "cleanUpCycle": 5000, "timeout": 300000, "maxSession": 5 }
To initiate the hub using the JSON configuration file, run this command:
java -jar selenium-server-standalone-2.41.0.jar -role hub –hubConfig grid_hub.json
Likewise, create individual JSON configuration files for different nodes, based on your desired configuration. As an example:
{ "capabilities": [ { "browserName": "chrome", "maxInstances": 2 }, { "browserName": "firefox", "maxInstances": 2 }, { "browserName": "internet explorer", "maxInstances": 1 } ], "configuration": { "nodeTimeout": 120, "port": 5555, "hubPort": 4444, "hubHost": "localhost", "nodePolling": 2000, "registerCycle": 10000, "register": true, "cleanUpCycle": 2000, "timeout": 30000, "maxSession": 5 } }
To initiate a node using a JSON configuration file, use this command:
java -jar selenium-server-standalone-2.41.0.jar -role rc –nodeConfig grid_node.json
You can customize various configuration parameters in the JSON configuration file, like browser, platform, version, maximum instances, etc.
Summing Up
It’s crucial to employ Selenium Grid when carrying out multi-browser testing with a surplus of test cases. In this guide, we’ve run through the setup and configuration of Selenium Grid, including how to set up browsers and nodes, set maximum instances and sessions, and provided an example code for running tests parallely. We also discussed how to use JSON configuration files to boot up Selenium Grid with predetermined settings.
In our next guide, we will go over automation testing with Selenium and Cucumber, a prevalent BDD testing tool and framework. We will explain how to marry Selenium WebDriver with Cucumber to boost test automation efficiently.
We welcome any questions related to Selenium Grid in the comments section!