1. Selenium Grid Architecture
  2. How to write tests for Selenium Grid?

Selenium Grid is a tool, that allows to runs multiple tests across different operating systems, browsers in parallel at the same time. Grid architecture contains Hub that only runs tests on a single machine, execution, however, these tests are performed on different machines called nodes. This approach obviously has its advantages. Speeds execution of a test, otherwise the application under tests is tested simultaneously on several environments, giving additional feedback.

Selenium Grid Architecture:

An architecture of grid is very simple. First, we should have a hub, only one hub in a grid. This will be our starting point and to a hub, we will load tests into. From there, tests are loaded into nodes.

Simple node architecture
Simple node architecture

Nodes are instances that will execute tests that were loaded from a hub. There can be one or more nodes, this depending on our configuration and of course needs.

To install Selenium Grid on a local machine we should make two things: first, install java JDK, and second download Selenium Server JAR file from SeleniumHq webpage.

After download, all needed files lets start with hub configuration. To start selenium grid hub on a local machine, open console and go to the directory where is located selenium server JAR file.

Selenium java -jar selenium-server-standalone-3.3.1.jar -role hub
14:52:44.850 INFO - Selenium build info: version: '3.3.1', revision: '5234b32'
14:52:44.851 INFO - Launching Selenium Grid hub
2017-04-07 14:52:45.442:INFO::main: Logging initialized @1003ms to org.seleniumhq.jetty9.util.log.StdErrLog
14:52:45.458 INFO - Will listen on 4444
2017-04-07 14:52:45.521:INFO:osjs.Server:main: jetty-9.2.20.v20161216
2017-04-07 14:52:45.562:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0
2017-04-07 14:52:45.562:INFO:osjs.session:main: No SessionScavenger set, using defaults
2017-04-07 14:52:45.566:INFO:osjs.session:main: Scavenging every 600000ms
2017-04-07 14:52:45.572:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@319b92f3{/,null,AVAILABLE}
2017-04-07 14:52:45.608:INFO:osjs.AbstractConnector:main: Started ServerConnector@7dc222ae{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2017-04-07 14:52:45.608:INFO:osjs.Server:main: Started @1169ms
14:52:45.609 INFO - Nodes should register to http://192.168.0.2:4444/grid/register/
14:52:45.609 INFO - Selenium Grid hub is up and running

After that, we can open a browser and go to http://192.168.0.2:4444/grid/console and for now, we should get an empty console, as bellow:

Console
Console

and config:

Console
Console

For now, we don’t have only a lot in this configuration. Next step is to configure nodes. To configure node on localhost: java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://192.168.0.2:4444/grid/register/

Selenium java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://192.168.0.2:4444/grid/register/
15:16:33.384 INFO - Selenium build info: version: '3.3.1', revision: '5234b32'
15:16:33.385 INFO - Launching a Selenium Grid node
2017-04-07 15:16:33.663:INFO::main: Logging initialized @508ms to org.seleniumhq.jetty9.util.log.StdErrLog
15:16:33.691 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
 registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC
15:16:33.691 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
 registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC
15:16:33.692 INFO - Driver class not found: com.opera.core.systems.OperaDriver
15:16:33.692 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
15:16:33.692 INFO - Driver class not found: com.opera.core.systems.OperaDriver
15:16:33.692 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
2017-04-07 15:16:33.722:INFO:osjs.Server:main: jetty-9.2.20.v20161216
2017-04-07 15:16:33.746:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@5be6e01c{/,null,AVAILABLE}
2017-04-07 15:16:33.772:INFO:osjs.AbstractConnector:main: Started ServerConnector@4b0b0854{HTTP/1.1,[http/1.1]}{0.0.0.0:5555}
2017-04-07 15:16:33.772:INFO:osjs.Server:main: Started @617ms
15:16:33.772 INFO - Selenium Grid node is up and ready to register to the hub
15:16:33.783 INFO - Starting auto registration thread. Will try to register every 5000 ms.
15:16:33.783 INFO - Registering the node to the hub: http://192.168.0.2:4444/grid/register
15:16:33.793 INFO - The node is registered to the hub and ready to use
15:16:38.911 INFO - SessionCleaner initialized with insideBrowserTimeout 0 and clientGoneTimeout 1800000 polling every 180000

And now we can see our first node:

Node
Node

To override default configurations node, we can register the second node as fallows:

java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://192.168.0.2:4444/grid/register/ -browser browserName=firefox,version=52.0.2,maxInstances=5,platform=LINUX -port 6666

Listing below:

Selenium java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://192.168.0.2:4444/grid/register/ -browser browserName=firefox,version=52.0.2,maxInstances=5,platform=LINUX -port 6666
15:37:27.136 INFO - Selenium build info: version: '3.3.1', revision: '5234b32'
15:37:27.137 INFO - Launching a Selenium Grid node
2017-04-07 15:37:27.433:INFO::main: Logging initialized @555ms to org.seleniumhq.jetty9.util.log.StdErrLog
15:37:27.469 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
 registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC
15:37:27.469 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
 registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC
15:37:27.469 INFO - Driver class not found: com.opera.core.systems.OperaDriver
15:37:27.469 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
15:37:27.469 INFO - Driver class not found: com.opera.core.systems.OperaDriver
15:37:27.470 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
2017-04-07 15:37:27.505:INFO:osjs.Server:main: jetty-9.2.20.v20161216
2017-04-07 15:37:27.530:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@10e92f8f{/,null,AVAILABLE}
2017-04-07 15:37:27.554:INFO:osjs.AbstractConnector:main: Started ServerConnector@353d0772{HTTP/1.1,[http/1.1]}{0.0.0.0:6666}
2017-04-07 15:37:27.554:INFO:osjs.Server:main: Started @676ms
15:37:27.554 INFO - Selenium Grid node is up and ready to register to the hub
15:37:27.565 INFO - Starting auto registration thread. Will try to register every 5000 ms.
15:37:27.566 INFO - Registering the node to the hub: http://192.168.0.2:4444/grid/register
15:37:27.574 INFO - The node is registered to the hub and ready to use
15:37:32.715 INFO - SessionCleaner initialized with insideBrowserTimeout 0 and clientGoneTimeout 1800000 polling every 180000

After refreshing console with nodes configuration we should have second with custom configuration:

Node
Node

How to write tests for Selenium Grid?

To design tests for the grid is not that complicated that you thing. But first od course we must create some changes in our code. Wery important is to import DesiredCapabilites package. This allows us to use: DesiredCapabilites object.

Define a browser and initialize a DesiredCapabilites object with firefox method:

DesiredCapabilities capability = DesiredCapabilities.firefox();

Declare requirements for a specific platform and browser version.

DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setPlatform(Platform.MAC);
capability.setVersion(52.0.2);

For this configuration, we will run out tests for OSX with Firefox 52.0.2 browser. Below is configuration for all platforms:

capability.setPlatform(Platform.MAC);   //Set platform to OSX
capability.setPlatform(Platform.LINUX); // Set platform to Linux based systems
capability.setPlatform(Platform.WINDOWS); //Set platform to Windows
capability.setPlatform(Platform.VISTA); //Set platform to VISTA
capability.setPlatform(Platform.XP);   //Will return a node with Windows XP or 2003 Server or Windows 2000 Professional Operating System.
capability.setPlatform(Platform.WIN8); //Set platform to Windows 8
capability.setPlatform(Platform.WIN8_1); //Set platform to Windows 8.1

Reference:

  1. org/seleniumhq/selenium
  2. Selenium GitHub