Consuming Selenium GRID

Please read the pre-requisites for this section before moving further. However if you already are aware of how a GRID is set up and works, feel free to skip the introduction.

Now let’s see how we can leverage a selenium grid. We should think of Selenium GRID as a black box at this time, where if we request a certain browser, then GRID should serve our request by executing automation script on that browser. Of course assuming that there is a node that has the browser we requested

Please see the video at the bottom of this page that explains all of this

saucelabs gem

saucelabs is a gem that I wrote to specify the browser, platform and other config options that we specify in the client calling API (remote webdriver api). Complete documentation has been provided too.

https://github.com/machzqcq/saucelabs

Install

Add the line gem ‘saucelabs’ to Gemfile in our Hybrid/page-object framework code base. Please refer to the entire section where we explained what this code base does. In short we implemented a shopping cart workflow in this project. We will use the same code base.

If you did not have the project already, feel free to check out the code base from my github repository

https://github.com/machzqcq/page-object_framework

env.rb

The below line has been added to env.rb file to be able to use saucelabs gem

hooks.rb

hooks.rb is where we have the logic that forks into local or remote (aka. GRID) execution.

We are defining an environment variable WHERE , that decides if we wish to execute locally or on Selenium GRID. By default it is local execution. Since in our case we want to execute it on a GRID, we have the logic that creates @browser instance and connects to the GRID

Here is where the suacelabs module helps us immensely. There are lot of options we can use with saucelabs gem and we will use one option. Others are self explanatory when you go to the HomePage or documentation for the gem.

Execute on Chrome39 and Windows

Since in our previous post we fired up a GRID that has a node with Chrome 39 browser on a Windows platform, let’s re-direct our automation script to that node for execution.

So how do we specify that we need a chrome 39 browser on windows platform in our code?

Extremely simple with saucelabs gem. Simply assign BROWSER=”chrome39|windows” and of course WHERE=remote. Apart from this we also have to specify the grid url so that the script knows to communicate with the Selenium GRID

consuming_grid

That is it ! The execution starts and you can see that the GRID is delegating the execution to the node that has chrome 39. You can also see the video at the bottom on how it all works.

Execute on any version of browser

Assuming that you have a GRID that has the version of the browser that you need, let’s see the steps required to execute our script on any arbitrary browser

  1. First launch the GRID url to check if the browser is being served by the GRID (http://localhost:4444/grid/console)
  2. If the browser is present, then continue
  3. While executing your project pass WHERE=remote and BROWSER=”bv|pv” and rest of the command is the same

where

  • bv = {browser, version} Ex: chrome39,firefox31,ie8…..
  • pv = {platform, version} Ex:xp,windows,windows81,osx109…..

All possible values of browser, versions, platform and version are listed on the documentation for saucelabs gem. Scroll to the bottom of the page.

Summary

In this section we have seen how to consume a Selenium GRID. Obviously there are learning curves that we need to understand before starting to use Selenium GRID. First, we need to understand how a selenium grid works, then we need to fire up the grid with all browser and platform combinations and then eventually we can consume the browsers by specifying what we want in our client automation code using saucelabs gem.

finally

You might have wondered why the name “saucelabs”. Well, because it was first written to connect to Sauce Labs cloud and then later we added functionality to connect to any Selenium GRID. So if you are using Sauce Labs infrastructure, then you can use saucelabs gem very easily and you can switch between local GRID and saucelabs anytime just with a line of code change. Isn’t that awesome ?

 

Cheers!