Selenium Lesson 16 –How to run test using Selenium Grid 2
When there are lot of automated test cases. automation team start facing problem with how to run them efficiently and get the result in lesser time. to resolve such issue, Selenium provides Selenium Grid. Selenium Grid allows you run your automation scripts or tests on different machines with different browsers in parallel.
Grid allows you to :
- Scale test execution by distributing automated tests on several machines.
- Manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
- Minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.
Two Major Advantages are:
- Run tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
- Reduce the time for Test Execution
Let’s take an example, i have 400 automated test cases. It takes 40mins to execute them on one machine with one browser. now i want to execute the same test cases for 4 browsers ( IE 10, Firefox 22, Chrome and Safari). if i run them on one machine then it will take 160mins. i also have QA Lab which has serveral machines and i can use them. then using selenium Grid i can execute the test cases on 4 different machine that reduce my test execution time from 160mins to 40mins where i saved my 75% of time.
Selenium Grid 2:
This example will show you how to start the Selenium 2 Hub, and register both a WebDriver node and a Selenium 1 RC legacy node. We’ll also show you how to call the grid from Java. The hub and nodes are shown here running on the same machine, but of course you can copy the selenium-server-standalone to multiple machines.
Step 1: Start the Selenium hub:
Hub is nothing but the Central place which receive all automated test cases which needs to execute and hub distribute the same on correct nodes.
Nodes are nothing but the machines on which your test execution will be done. it could be machines, phones, etc.
Open a command prompt and navigate to the directory where you copied the selenium-server-standalone file.
Type the following command:
java -jar selenium-server-standalone-2.14.0.jar -role hub
This command will start the hub. by default hub gets started on port 4444. if you wish to change then you can change it by providing additional option -port. You can view the status of the hub by opening a browser window and navigating to: http://localhost:4444/grid/console
Step 2: Start the nodes:
Regardless on whether you want to run a grid with new WebDriver functionality, or a grid with Selenium 1 RC functionality, or both at the same time, you use the same selenium-server-standalone jar file to start the nodes.
java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://localhost:4444/grid/register
Note: The port defaults to 5555 if not specified whenever the “-role” option is provided and is not hub.
once you start the hub and nodes now you need to run the test cases on it. lets see how to run test cases on node.
Test Execution on Nodes:
For Selenium RC Users:
You can continue to use the DefaultSelenium object and pass in the hub information:
Selenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://www.google.com”);
For Selenium Webdriver Users:
for webdriver users, you need to use RemoteWebDriver and the DesiredCapabilities object to define which browser, version and platform you wish to use. Create the target browser capabilities you want to run the tests against:
DesiredCapabilities capability = DesiredCapabilities.firefox();
Pass that into the RemoteWebDriver object:
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
The hub will then assign the test to a matching node.
A node matches if all the requested capabilities are met. To request specific capabilities on the grid, specify them before passing it into the WebDriver object.
capability.setBrowserName(); capability.setPlatform(); capability.setVersion() capability.setCapability(,);
Example: A node registered with the setting:
will be a match for:
capability.setBrowserName(“firefox” ); capability.setPlatform(“LINUX”); capability.setVersion(“22”);
and would also be a match for
capability.setBrowserName(“firefox” ); capability.setVersion(“22”);
The capabilities that are not specified will be ignored. If you specify capabilities that do not exist on your grid (for example, your test specifies Firefox version 22, but have no Firefox 22 instance) then there will be no match and the test will fail to run.