March 7, 2017

Selenium Grid

What is Selenium Grid:

Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems

Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.


When to use Selenium Grid:

  1. To test the UI of you page across the browsers - CROSS BROWSER TESTING
  2. If you have a suite of 100 flows devide them logically and start the execution on the differet Nodes at the same time. Make sure No tests are dependent on the tests of the other group - SAVE TIME
  3. When your application has feature which supports mulitple languages, then create the seperate test data for each language and run those tests in parallel

Advantages Of Selenium Grid?

  1. Run your tests against different browsers, operating systems, and machines all at the same time. This will ensure that the application you are testing is fully compatible with a wide range of browser-O.S combinations.
  2. Selenium-Grid is used to speed up the execution of a test pass by using multiple machines to run tests in parallel. For example, if you have a suite of 100 tests, but you set up Selenium-Grid to support 4 different machines to run those tests, your test suite will complete in approx one-fourth the time as it would if you ran your tests sequentially on a single machine.
Selenium Grid uses a hub-node concept. A grid consists of a single hub, and one or more nodes. Both are started using the selenium-server.jar executable.

What is HUB?
The hub receives a test to be executed along with information on which 'browser' and ‘platform’ where the test should be run. It ‘knows’ the configuration of each node that has been ‘registered’ to the hub. Using this information it selects an available node that has the requested browser-platform combination. Once a node has been selected, Selenium commands initiated by the test are sent to the hub, which passes them to the node assigned to that test.

What is a NODE?
The node runs the browser, and executes the Selenium commands within that browser against the application under test.

How to Install Selenium Grid:

Download the Selenium-Server jar file from the SeleniumHq website’s download page.

You’ll need to be sure the java executable is on your execution path so you can run it from the command-line. If it does not run correcly, verify your system’s 'path' variable includes the path to the java.exe.

Hub configuration:
To start a hub with default parameters, run the following command from a command-line shell. This will work on all the supported platforms like Windows, Linux or Mac.

java -jar <selenium-server-standalone-2.xx.x.jar> -role hub

This starts a hub using default parameter values ad the the default port number will be 4444.

You should see the following logging output after running the above command.
<month> <date>, <year> <hh>:<mm>:<ss> AM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a selenium grid server
<year>-<month> <date> <hh>:<mm>:<ss>.082:INFO:osjs.Server:jetty-7.x.y-SNAPSHOT
<year>-<month> <date> <hh>:<mm>:<ss>.151:INFO:osjsh.ContextHandler:started o.s.j.s.ServletContextHandler{/,null}
<year>-<month> <date> <hh>:<mm>:<ss>.185:INFO:osjs.AbstractConnector:Started SocketConnector@0.0.0.0:4444

Specifying the Port
AS we discussed, the default port used by the hub is 4444. If another application on your computer is already using this port, or if, you already have a Selenium-Server started, you’ll see the following message in the log output.

<hh>:<mm>:<ss>.490 WARN - Failed to start: SocketListener0@0.0.0.0:4444
Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.

If this occurs you can either shutdown the other process that is using port 4444, or you can tell Selenium-Grid to use a different port for its hub. Use the -port option for changing the port used by the hub.

java -jar selenium-server-standalone-2.<xx>.<x>.jar -role hub -port 4441

This will work even if another hub is already running on the same machine, that is, as long as they’re both not using port 4441.

Starting Node:
To start a node using default parameters, run the following command from a command-line.

java -jar selenium-server-standalone-2.<xx>.<x>.jar -role node  -hub http://localhost:4444/grid/register

This assumes the hub has been started above using default parameters. The use of ‘localhost’ assumes your node is running on the same machine as your hub and 4444 specifies the port number of your hub. If running the hub and node on separate machines, simply replace ‘localhost’ with the hostname of the remote machine running the hub.

WARNING: Be sure to turn off the firewalls on the machine running your hub and nodes. Otherwise you may get connection errors.

Configure Node:
For running the tests with selenium grid, selenium webdriver tests needs to use the instance of RemoteWebDriver and DesiredCapabilities classes to define which Browser version and platform the tests will be executed on.

Based on the preferences set in the DesiredCapabilities instance, the hub will point the test to a node that matches these preferences.

Configure Node for Firefox:
The Script Level:
DesiredCapabilities ffcap = new DesiredCapabilities();
ffcap.setBrowserName("firefox");
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),ffcap);

Command level while registering the Node:
java -jar selenium-server-standalone-2.<xx>.<x>.jar -role node  -hub http://localhost:4444/grid/register -browser "browserName = firefox" -port 5555

Configure Node for Internet Explorer:
The Script Level:
DesiredCapabilities iecap = new DesiredCapabilities.internetExplorer();
iecap.setCapability(InteretExplorerDriver.INTRODUCE_FLAKIESS_B_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),iecap);

Command level while registering the Node:
java -jar selenium-server-standalone-2.<xx>.<x>.jar -role node  -hub http://localhost:4444/grid/register -maxSession 10 -browser "browserName = interet explorer, platform=WINDOWS" -port 5556


Common Errors

Unable to access jar file selenium-server-standalone-2.<xx>.<x>.jar

This error can occur when starting up either a hub or node. This means Java cannot find the selenium-server jar file. Either run the command from the directory where the selenium-server-standalone-2.<xx>.<x>.jar file is stored, or specify an explicit path to the jar.


Points to be careful when run the tests in parallel:

  1. Only those tests which are independent of each should be run in parallel
  2. When working with browsers, there should not be manual intervention like 'Do you want to set this browser as your default browser' dialog

1 comment:

  1. Well written content about software testing. It is one of the major trends in software testing. Most of the digital assurance services providers will change their process with AI. Waiting for your upcoming articles like this.

    ReplyDelete