Run multiple protractor specs in parallel

When you want to run protractor scripts by opening a browser instance for each test, you should add two capabilities shardTestFiles and maxInstances in the Capabilities block of the conf.js file. This will help to execute your scripts on same browser with multiple instances. If you want to execute on multiple browser, we have to use multipleCapabilities which we discussed in detail.

shardTestFiles - To enable sharing of tests at the spec level, we must configure shardTestFiles flag as true within the capabilities.

maxInstances - The maxInstances number represents the maximum number of browser windows that Protractor should create in parallel. For example, If we set maxInstances to 2, a test suite of 20 tests would result in Protractor creating 2 Chrome instances with each instance running 10 tests.

NOTE : - This is only needed if shardTestFiles is set to true. And by default maxInstances value is set to 1.

Let us see a simple example with two specs :-

First create spec and name it as mat_paginator_spec.js

describe('angular-material paginator component page', () => {
  const EC = protractor.ExpectedConditions;

  beforeAll(async() => {
    await browser.get('https://material.angular.io/components/paginator/examples');
	
    await browser.wait(EC.elementToBeClickable($('.mat-paginator-navigation-next .mat-paginator-icon')), 3000);
  });

  it('Should navigate to next page', async() => {
    await $('button[aria-label=\'Next page\']').click();
    
    await expect($('.mat-paginator-range-label').getAttribute('innerText')).toEqual('11 - 20 of 100');
  });

  it('Should navigate to previous page', async() => {
    await $('button[aria-label=\'Previous page\']').click();
    
    await expect($('.mat-paginator-range-label').getAttribute('innerText')).toEqual('1 - 10 of 100');
  });

  it('Should change list length to 5 items per page', async() => {
    await $('mat-select>div').click();
    
    const fiveItemsOption = $$('mat-option>.mat-option-text').first();

    await fiveItemsOption.click();
    
    await expect($('.mat-paginator-range-label').getAttribute('innerText')).toEqual('1 - 5 of 100');
  });
});

Now create another spec and name it as input_spec.js

describe('angular-material input component page', function() {
  const EC = protractor.ExpectedConditions;

  it('Should change input component value', async() => {
    await browser.get('https://material.angular.io/components/input/examples');
      
    await browser.wait(EC.visibilityOf($('.docs-example-viewer-wrapper')), 3000);
      
    const emailInputField = $$('.mat-form-field-infix>input').get(1);
      
    await emailInputField.sendKeys('invalid');
      
    expect($('mat-error').isPresent()).toBe(true);
  });
});

Let us now create Protractor’s conf.js file to define a maximum number of browser instances that Protractor can instantiate to run tests as shown below :-

exports.config = {
    directConnect: true,
  
    // Capabilities to be passed to the webdriver instance.
    capabilities: {
      'browserName': 'chrome',
	  'shardTestFiles': true,
      'maxInstances': 2
    },
  
    framework: 'jasmine',
    specs: [
        'input_spec.js',
        'mat_paginator_spec.js'
    ],

    jasmineNodeOpts: {
      defaultTimeoutInterval: 30000
    }
};

Now run the scripts using protractor conf.js. Below is the screenshot of the output :-

Protractor tests in parallel

Executing tests in parallel will definitely help to drastically reduce the overall test execution time and speed up the continuous delivery process.

I hope this article has helped you to run your protractor specs in parallel. Please feel free to post your questions in the comments section below.

Protractor Tutorials: 

Add new comment