Karma code example using Selenium Grid

This Karma code example will get you a quick start using a selenium grid to run multiple tests in parallel. The example uses the karma-webdriver-launcher plugin and a Gridlastic Connect tunnel to enable encrypted connectivity between your local running Karma server and your Gridlastic selenium grid nodes.

KARMA INSTALLATION
npm install karma
npm install karma-webdriver-launcher
npm install -g karma-cli    (Windows)
npm install --save selenium-webdriver@2.53.1

EXAMPLES
Multiple Karma tests in parallel

Multiple Karma tests in parallel

Here we use the Karma plugin karma-webdriver-launcher to run parallel tests using a Gridlastic selenium grid. Tests are retried once if failed and a Gridlastic Connect tunnel provides the grid nodes access to your local Karma server. See below how to setup the tunnel.

karma.conf.js


module.exports = function (config) {
// Here we are using the Gridlastic Connect tunnel client installed on the local/calling machine
// and can therefore access the selenium grid hub via an encrypted endpoint (localhost).
// Note: starting from selenium version 3.9.1 you must also include "platformName": "windows" in the request when testing with firefox and IE.
  var webdriverConfig = {
    hostname: 'localhost',
    port: 3000,
  }
  var gridlasticUser = USERNAME
  var gridlasticKey = ACCESS_KEY
  
  config.set({
// Karma server on local machine  
	hostname: 'localhost',
	port: '9876',
// Gridlastic Connect tunnel endpoint mapped to the local Karma server
	upstreamProxy: {
	'hostname': 'karma.YOUR_SUBDOMAIN.gridlastic.com',
	'port': '8080',
	'protocol': 'http',
},
    basePath: '',
    frameworks: ["jasmine"],
    reporters: ['progress'],
	logLevel: config.LOG_INFO,
	// To allow time (captureTimeout) for grid nodes to be launched if not present.
	captureTimeout: 600000,
	retryLimit: 1,
	browserDisconnectTimeout: 30000,
	browserDisconnectTolerance: 1,
	browserNoActivityTimeout: 30000,
	//parallel tests
	concurrency: 3,
    customLaunchers: {
	'firefox': {
        base: 'WebDriver',
        config: webdriverConfig,
        browserName: 'firefox',
        name: 'ff',
		platform: 'WIN8_1',
		platformName: 'windows',
		version: '54',
		gridlasticUser: gridlasticUser,
		gridlasticKey: gridlasticKey
      },
	'chrome': {
        base: 'WebDriver',
        config: webdriverConfig,
        browserName: 'chrome',
        name: 'Chrome',
		platform: 'VISTA',
		platformName: 'windows',
		version: 'latest',
		gridlasticUser: gridlasticUser,
		gridlasticKey: gridlasticKey
      },
	'internet explorer': {
        base: 'WebDriver',
        config: webdriverConfig,
        browserName: 'internet explorer',
        name: 'ie',
		platform: 'WIN10',
		platformName: 'windows',
		version: '11',
		gridlasticUser: gridlasticUser,
		gridlasticKey: gridlasticKey
      }
    },
    browsers: ['firefox','chrome','internet explorer'],
    files: [
      "karma_example.spec.js"
    ],
    singleRun: true
  });
}

karma_example.spec.js


describe('Selenium Grid rocks test', function() {
  var seleniumGridRocks = null;
  beforeEach(function() {
    seleniumGridRocks = true;
  });
  it('should rock your socks off', function() {
    expect(seleniumGridRocks).toBeTruthy();
  })
});
RUN
karma start karma.conf.js
NOTE: To run this example as is, get a free Gridlastic account and replace USERNAME, ACCESS_KEY and YOUR_SUBDOMAIN parameters with your own credentials found in your Gridlastic dashboard after launching your selenium grid.

To use Karma with selenium, install the latest selenium client version equivalent to your selected selenium grid version. Example: If you selected to use selenium version 2.53.1 for your selenium grid, install the latest 2.53.x for Javascript (Node).


Setup the Gridlastic Connect tunnel

Follow the Gridlastic Connect tunnel setup documentation and use this config file mapping to give your selenium grid nodes access to your local Karma server:
server_addr: YOUR_SUBDOMAIN.gridlastic.com:443
tunnels:
  tunnel1:
    subdomain: "karma"
    proto:
      http: 9876
Install the tunnel client on the same local machine that is executing your tests (where the Karma server lives).

Start the tunnel client with hub access enabled like:
./gridc -username=HUB_USERNAME --password=HUB_PASSWORD -config=config.cfg -hub 3000 start tunnel1
Note that Gridlastic Connect is disabled by default and you need to update your selenium grid configuration (open port 443) to use encrypted tunnels.

NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.