How to shard test files in protractor?

How to shard test files in protractor?

What is Sharding?

Sharding of test suites means to run the test suites in parellel. Below is the representation of running the test suites in parellel as well as in series.

Running tests in parellel

8.JPG

Running tests in series

9.JPG

Why is Sharding needed?

As sharding helps the test suites to run in parellel, it would decrease execution time and increase the tests efficiency.

Step to shard test suites in protractor

  • Declare two capabilites in the capabilities block of the conf.js file.
  • shardTestFiles - allows different specs to run in parallel. If this is set to be true specs will be sharded by file (i.e. all files to be run by this set of capabilities will run in parallel).Default is false..
  • maxInstances - Maximum number of browser instances that can run in parallel for this set of capabilities. This is only needed if shardTestFiles is true. Default is 1.

Code Ilustration

conf.js

// An example configuration file to illustrate sharding.
exports.config = {
  directConnect: true,

  // Capabilities to be passed to the webdriver instance for sharding.
  capabilities: {
    'browserName': 'chrome',
    // Sharding
    'shardTestFiles': true,
    'maxInstances': 2, 
  },

  // Framework to use. Jasmine is recommended.
  framework: 'jasmine',

  // Spec patterns are relative to the current working directory when
  // protractor is called.
  specs: ['spec1.js','spec2.js'],
  SELENIUM_PROMISE_MANAGER: false,
  // Options to be passed to Jasmine.
  jasmineNodeOpts: {
    defaultTimeoutInterval: 30000
  }
};

I will also soon be publishing it on GeeksforGeeks