Selenium-grid - A simple desktop setup on Windows

Using a selenium-grid can have many advantages, especially on bigger projects. One such advantage is that you can run many tests in parallel. Another advantage is that you only need to run your tests to one point (the selenium-grid hub). The hub will sort out if it has a connected node that is matching your request and if so, it will serve your tests to that node.

Selenium grid

Assumptions/preconditions

  • You know how to download and install Java (from java.com)
  • You know how to download the webdrivers (also previous post)
  • You know how to add a PATH to the PATH variable of Windows (see Google)
  • Make sure Java and the webdrivers are in the PATH variable

Start the hub

Download the Selenium Standalone Server and run this in the commandline:
java -jar <filedir\filename>.jar -role hub
Allow the firewall setting if/when this pop's up.

Start the node

java -jar <filedir\filename>.jar -role node
This will just register the node to the hub with some default settings.
Meaning that the node will register to the hub using default settings. If you open the browser and navigate to http://localhost:4444/grid/console you will see that the node registered with 1 IE instance, 5 Firefox instances and 5 Chrome instances.

Detailed node configuration

The above is fine for a simple setup on your localhost, but usually you run the hub and nodes on different machines and also have a pre-defined set of browsers you run per node, so let's make use of a node config file. In the json file we'll put all the details about the hub and some additional node configurations.
Kill the previous node session and now start it like this:
java -jar <filedir\filename>.jar -role node -nodeConfig "<filedir\nodeConfig.json>"
You see that the node registered to the hub (in this case still on my localhost, but this runs just as well fine on any other machine as long as they can talk to another). And the node this time registered with an OS mentioned, with two FF instances running on a specific version, one Edge instance and two Chrome instances.

Connect to the browser on the grid

In IRB (* see known issues), or in your env.rb file, you can now connect to a browser on the selenium-grid like this:
require 'lapis_lazuli'
include LapisLazuli
grid_url = "http://localhost:4444/wd/hub"
browser :chrome, url: grid_url

Capabilities

If you don't care about OS, browser version, or parallel testing, a simple setup is more than enough. You could leave out all these details in the grid node configuration file and in your test suite. But in most cases you need a tiny bit more details, so let's also dive a bit into these capabilities (which is usually the most confusing part of the whole setup if you ask me).

Default Capability Matcher

The default capability matcher of Grid2 looks at these parameters:
  • Platform
  • Browser name
  • version (relates to the browser)
  • “applicationName” (This last one is poorly documented)
This means that by default you can only make a match with the correct node by making use of these capabilities. The last one I never used because for a simple setup the first three should be more than enough. 

One thing I did notice is that the docs are a bit behind in maintanance. For example Platform cannot use `WIN7`(at least the last time I tried), but it can use `WIN8` and `WIN10`.

For the `version` capability you have more freedom and can enter whatever you want as a number. So for the tiny bit more advanced cases you can abuse this field to be sure you're connecting to the correct node without the need to write your own capability matcher. In the "Problems you might encounter" section of my mobile setup guide post, I have a link to the solution/example on StackOverflow.

More details about the default Selenium capabilities:

Custom Capability Matcher

In case the above is not advanced enough for you. You should dive into the world of the custom capability matcher. This is bit to advanced for this beginners guide so I'll skip the explanation. Google for Selenium Custom Capability Matcher to find all the info you need.

Appium

In case you want to test on mobile devices connected to a selenium grid, please read my mobile test automation post and use this appium page for all possible capabilities.

Grid in the cloud

Of course there are also cloud solutions for this, which require no selenium-grid setup on your side. You just need to have an account and know how to connect to this cloud-grid. Even if you're not going to use a local setup, but a cloud solution, it makes sense to play around with the settings a bit on your local machine to understand how this works. Every cloud service has a custom capability matcher so check out their documentation for more options.

SauceLabs

BrowserStack

Known issues

In most cases this just works out of the box. But it could be that you run into these issues.

Issue: 'SystemStackError: stack level too deep'

The problem lies in the lapis_lazuli gem, see git issues tracker.
The solution is to first navigate to the project folder before you start irb

Issue: Node cannot connect to the hub

Solution: add `-host <ip_of_hub>` behind the startup of the node command. When you use the config file, just put it in there like in the sample I showed above.
See original topic: https://stackoverflow.com/questions/42011661/selenium-grid-node-cannot-connect-to-hub

Isssue: Node connects to hub, but Hub failed to connect to Node

On the grid console you most likely see something like this:
Solution: Most likely you forgot to accept the Java firewall message on the node, or you ignored it. Either way, just make sure java is being accepted and you're good to go.

Final words

Hopefully this was helpfull to you. In case you have info to add to this post, or have questions reguarding the setup, feel free to contact me.

Comments

Popular posts from this blog

PowerShell - How to overcome Azure VM's fixed resolution limitation

TA Basics: Website Test Automation on mobile devices via Appium server

How to deal with browser alerts that require a confirmation with Ruby/Cucumber/LapisLazuli