Welcome to the final installment of our extensive Selenium tutorial series. In this tutorial, we will introduce you to Selenium Grid, a distributed test execution environment that allows for faster test execution. We will also learn how to perform cross browser testing using Selenium Grid.
Introduction to Selenium Grid and Cross Browser Testing
As we near the end of our comprehensive Selenium tutorial series, we are excited to bring you one last tutorial on Selenium Grid. In this tutorial, we will explore how Selenium Grid can speed up the execution of your test cases by distributing them across multiple machines. Additionally, we will cover the process of performing cross browser testing 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 )
Tutorial Contents
Table of Contents:
What is the Need of Selenium Grid?
When going through our Selenium WebDriver tutorials, you may have noticed that WebDriver executes test cases on a single machine. However, there are several challenges with this setup:
- Running test cases on different operating systems
- Running test cases on different versions of the same browser
- Running test cases on multiple browsers
- Having scenarios wait for the execution of other test cases, even if they don’t depend on each other
All these challenges are addressed by Selenium Grid. In the upcoming sections, we will explore how Selenium Grid can overcome these challenges. Selenium Grid is built on a master-slave architecture, where the master machine distributes test cases to different slave machines.
There are two versions of Selenium Grid available: Selenium Grid 1.0 and Selenium Grid 2.0. Selenium Grid 2.0 is the latest version and supports both Selenium RC and Selenium WebDriver scripts. Most experts prefer Selenium Grid 2.0 due to its additional features.
Benefits of Selenium Grid
Selenium Grid offers several benefits for test automation:
- Flexibility to distribute test cases for execution
- Reduced batch processing time
- Support for multi-browser testing
- Support for multi-OS testing
In the next sections, we will dive into the setup and configuration of Selenium Grid.
Installing Selenium GRID
Here are the steps to install Selenium Grid:
- Download the Selenium Server jar file from the Selenium HQ website and save it on your local disk. You can find the download link at http://www.seleniumhq.org/download/.
- Open the command prompt and navigate to the folder where you saved the server file. Run the server using the following command:
java -jar selenium-server-standalone-2.41.0.jar -role hub
- To check the status of the hub, open a web browser and go to http://localhost:4444/grid/console.
- To set up nodes, go to the machines where you want to install the nodes. Open the command prompt and run the following command:
java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://localhost:4444/grid/register -port 5556
- Run the selenium server on each machine to start the nodes.
Browser and Nodes Setup
After setting up the hub and nodes on each machine, you can configure the browsers for testing. Selenium Grid supports popular browsers such as Chrome, Firefox, and Internet Explorer.
To use a specific browser, start the node with the respective 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 Firefox:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox
For Chrome:
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=chrome
You can also configure the maximum number of instances for each browser using the maxInstances
parameter. For example, to work with 2 instances of Firefox and 2 instances of Internet Explorer, 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=firefox,maxInstance=2 -browser browserName=iexplore,maxInstance=2
Similarly, you can configure other browsers and their instances according to your requirements.
maxSession
The maxSession
parameter is used to define the maximum number of browsers that can be used concurrently on a remote system. For example, to configure a maximum of 3 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 parallel execution of up to 3 browser sessions.
Sample Grid Code
If you want to run your test cases in parallel on different browsers, you can use the following sample 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(); } }
In this example, we use TestNG to run a sample test case on the Selenium Grid. The test case logs in to Gmail and enters the username and password. The code uses the RemoteWebDriver class, which is specifically designed for use with Selenium Grid. The code also sets the browser and platform according to the desired capabilities.
To configure your tests for parallel execution on different browsers, you need to update your testng.xml file. Here’s an example testng.xml suite for running the above test case serially on different 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 run tests in parallel, update your testng.xml file like this:
<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 to “tests” and the thread-count
attribute determines the maximum number of threads to be executed concurrently.
Configuration Using JSON File
You can also launch Selenium Grid along with its configuration by using a JSON configuration file. To use this feature, follow these steps:
Create a JSON file with the desired configuration. For example:
{ "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 start the hub using the JSON configuration file, use the following command:
java -jar selenium-server-standalone-2.41.0.jar -role hub –hubConfig grid_hub.json
Similarly, create separate JSON configuration files for different nodes, based on your desired configuration. For 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 start a node using a JSON configuration file:
java -jar selenium-server-standalone-2.41.0.jar -role rc –nodeConfig grid_node.json
You can customize various configuration parameters such as browser, platform, version, maximum instances, and more in the JSON configuration file.
Conclusion
It is highly recommended to use Selenium Grid when performing multi-browser testing with a large number of test cases. In this tutorial, we covered the setup and configuration of Selenium Grid, including browser and node setup, maximum instances and sessions, and sample code for running tests in parallel. We also explored the option of using JSON configuration files to launch Selenium Grid with predefined settings.
In the next tutorial, we will explore automation testing with Selenium and Cucumber, a popular BDD testing tool and framework. We will learn how to integrate Selenium WebDriver with Cucumber for efficient test automation.
Please feel free to post your questions about Selenium Grid in the comments below.