Setting up Selenium Grid with Cucumber

This is more of an advanced topic I think. I did a lot of research to come up with the solution I use here at TollFreeForwarding.com

I have tests that would take hours to run sequentially.  To bring this back to normalcy, I use Selenium Grid to farm the jobs to multiple VM’s simultaneously.  This way the total time for all tests to complete is the longest test I have (5min.)

To get to this point, the Cucumber tests have to be modular.  They have to be broken up. You can’t have one giant cucumber test.  Selenium Grid can’t take one giant test and farm out each Scenario.  Instead you have to have multiple features.

For example:
If you  have a UI you automate and you have coverage for areas like –

  • Account Creation
  • Profile CRUD actions
  • Forum Post CRUD actions
  • Calendar Schedules
  • Call Customer Support (WebPhone)
  • Admin: Create Menus for Customers
  • Admin: Make new announcements for Customers
  • Admin: CRUD actions on gallery uploads/edits/deletes

Then I’d make each of these it’s own feature. 

Imagine each of the eight features above took 5 min to complete.  That’s a total of 40min if they are run sequentially.  Meaning you’d start the tests and come back in 40min.  What if you could get that down to 5 min?

You can!

Run them all at the same time, across multiple VMs.  That way they all run in parallel and finish in 5min.

This is where Selenium Grid comes in.

Install the Grid

On the Main VM that will run this job, you will install the Selenium Grid Hub.  This is rather simple to do.  You basically have to have java installed, and you run a command like:
[path to your selenium server standalone jar]/selenium-server-standalone-2.31.0.jar -role hub

I found our VM’s might sometimes restart (IT restarts and what not) so I added a batch file on this Windows VM and use the Windows Scheduler to assign a new task of “on restart run this batch file” the batch file has this content:
@echo off
“C:\Program Files (x86)\Java\jdk1.7.0_17\bin\java.exe” -jar “C:\Selenium-grid\selenium-server-standalone-2.31.0.jar” -role hub

Which tells the server “run Java to execute the Selenium server stand alone with the parameter role of “hub.”

Node VM’s must be configured

On each VM that will be used to get jobs from the Grid hub (referred to as ‘nodes’), you will need to set them up.  These boxes/VM’s again need to have JAVA installed and need to have the same selenium-server-standalone jar.  But it will be run with the role ‘node.’

Here’s an example:
java -jar selenium-server-standalone-2.31.0.jar” -role node -hub http://qa1.ifn.com:4444/grid/register -browser browserName=chrome,maxInstances=5

Similar to the Hub, I also created a batch file and used the windows scheduler to make sure that on a restart the batch file executes… it has this code:

C:\Program Files (x86)\Java\jre7\bin\java.exe” -jar “C:\Selenium-grid\selenium-server-standalone-2.31.0.jar” -role node -hub http://mydomain.com:4444/grid/register -browser browserName=chrome,maxInstances=5

Note that http://mydomain.com:4444 is the domain that the hub is running on.  This command is registering the browser Chrome and saying it has 5 max instances (this means it will run up to 5 chrome tests simultaneously on this box.)  you can of course change that.

Setting Up Cucumber

Since I use Jenkins to run all the Cucumber jobs, I want to be able to specify parameters from a command line that will:

  • Run the Cucumber Features
  • Specify the Browser for the test
  • Specify if the grid will be used or not

To do that, I use the env.rb file in Cucumber’s /Features/support  directory.

Here’s an example of what I did to create these parameters to be used from the command line….
First I added some code:

def browser_name
  (ENV[‘BROWSER’] ||= ‘firefox’).downcase.to_sym
end

def environment
  (ENV[‘ENVI’] ||= ‘int’).downcase.to_sym
end

This is setting the parameter “browser” and “envi” (for environment) to be called on the command line.  It is also giving a default value for each.  By default the browser is Firefox and the environment is int (for integration.)

Below that code, I wrote this before statement:
Before do  |scenario|
  p “Starting #{scenario}”
  if environment == :int
    @browser = Watir::Browser.new(:remote, :url=>”http://mydomain.com:4444/wd/hub”, :desired_capabilities=> browser_name)
    @browser.goto “http://integration.env.com:8080”
  elsif environment == :local
    @browser = Watir::Browser.new browser_name
    @browser.goto “http://integration.env.com:8080”
  end
end

This block above says that before we run the Cucumber scenarios, we must define a few things.  First if the environment is passed in as ‘int’ (our default) we will define @browser to be equal to Watir::Browser.new(:remote, :url=>”http://mydomain.com:4444/wd/hub”, :desired_capabilities=>browser_name)

So if environment is int, we’re defining the @browser to be going to selenium grid to send all the traffic.  If a browser is passed in, we’re also sending that along. 

Test this out by sending a command from the project you have like:
cucumber envi=int browser=firefox

You can monitor the jobs get picked up by different vm’s from the Grid console.

Jenkins Configuration

Once you have Selenium Grid set up, go to Jenkins and make a new basic job. 

This job will just do a Windows Shell command.

You’ll want to use the same command line parameter that was working to test your test above. For example, if you have a feature called “Account Creation” you would do something like this:
cucumber features/account_creationl.feature BROWSER=firefox ENVI=int

If you did that for each job, you’d have all your jobs going to the grid.  You could make a parent job that runs all features simultaneously.

To do that you create a project that has only one function, to kick off downstream projects… each feature would be it’s own project/job.  So the parent project would launch downstream jobs of: account creation, profile crud actions, forum crud actions, etc.  So they all run in parallel and are sent to the grid.

Gotcha’s

There’s a gotcha.  IE webdriver doesn’t like to be used remotely.  For this one outlier, I use IE in a serial fashion.

So in my Jenkins I have my jobs on tabs.  The first time is Functional Tests (these run in Firefox), the second tab is Chrome Tests, the third is IE tests.

On the first tab I have the jobs configured to run with FF only.  On the second to run with Chrome only. The third tab runs in IE only AND does NOT SEND the jobs to the grid. 

This way, I get the Firefox functional tests finished in 5min, Chrome finished in 5 min and IE takes the normal time of 40min.  It’s still better then 40min a pop.

Leave a Reply

Your email address will not be published. Required fields are marked *