Why Parallel Testing?

When designing automation test suites, the ability to run tests in parallel is a key feature because of the following benefits:

  • Execution time – Increasing tests efficiency and, finally, faster releases
  • Device compatibility – There are many devices with various versions and specifications that need to be tested in a given test period
  • Cross-browser testing – Run different tests on the same or different browser combinations
  • Better coverage – Testing your application through as many platform-device-browser combinations as possible so that no bug is missed
  • Cost-Efficiency – Hardware architecture that allows for parallel programming is more cost-effective than systems that only allow for serial processing. This means they produce more results in less time than serial programs and hold more financial value over time.

How to run Protractor tests in parallel?

Protractor is an end-to-end test framework developed for Angular and AngularJS applications. Still, Protractor can also be used for non-Angular applications by turning off Angular wait with the command browser.waitForAngularEnabled(false).

It runs tests against the application interacting with it as a real user would, running in a real browser. With Protractor, we can also use a headless mode.

Protractor tests can be run on one or more browsers or devices. So with Protractor, there is a possibility to run tests in parallel, and it is achieved by using the Capabilities block or the multiCapabilities block in the test config.js file.

There are two main types of parallel execution we’d like to do:

  • Running multiple specs simultaneously on a certain browser/deviceRunning multiple specs simultaneously on a certain browser/device

For example, if we have spec_1.js and spec_2.js files, and we want each of them to run separately on a certain browser/device in parallel, as illustrated in the image above.

  • Running the same spec against different browsers or devices in parallelRunning the same spec against different browsers or devices in parallel

For example, if we have a spec_1.js file and we want it to run on two different browsers, as illustrated in the image above.

Let’s see a simple example for each objective.

Example:

  • We are going to create two simple test scripts, spec_1.js, and spec_2.js, that will serve us to demonstrate parallelization in Protractor
  • All the Protractor automation frameworks will have a file that contains the configuration, and this will be the initial file that initiates the test (conf.js or often named as protractor.conf.js file)

spec_1.js

describe('Test 1', () => {

  beforeAll(async() => {
    //set browser position and resolution
    browser.manage().window().setPosition(0, 0);
    browser.manage().window().setSize(500, 600); 
    
    await browser.get('https://www.atlantbh.com/');
  });

  it('Check Home page title', async() => {

    var title =  await browser.getTitle();
    expect(title).toEqual('Atlantbh Sarajevo - We Make Things Look Easy');

  });
});

spec_2.js

describe('Test 2', () => {

  beforeAll(async() => {
    //set browser position and resolution
    browser.manage().window().setPosition(700, 0);
    browser.manage().window().setSize(400, 600);
  
    await browser.get('https://www.atlantbh.com/');
  });

  it('Should navigate to sidebar on small resolution', async() => {

    await element(by.className('close-wrap loaded')).click();
    await expect(element(by.linkText('Home')).getText()).toEqual('Home');
 
  });
});


Run multiple specs simultaneously

This objective can be achieved by setting two capabilities 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 shared by file (i.e., all files to be run by this set of capabilities will run in parallel). By default, the shardTestFiles value is set to 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. By default, the maxInstances value is set to 1.

Step 1: First, create a conf.js file consisting of the configuration to be used for this objective

conf.js

// An example configuration file illustrate parallel execution 
exports.config = {
    directConnect: true,
    
    // Capabilities to be passed to the webdriver instance for sharding
    capabilities: {
        'browserName': 'chrome',

        //Allows different specs to run in parallel setting Maximum     number of browser instances
        '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: ['spec_1.js','spec_2.js'],
    SELENIUM_PROMISE_MANAGER: false,

    // Options to be passed to Jasmine.
    jasmineNodeOpts: {
        defaultTimeoutInterval: 30000
    }
};

Step 2: Now run the scripts using the protractor conf.js command. The results are shown below:

Ran two Chrome browser instances: 

running two Chrome browser instances


Output:


Run the same spec against different browsers/devices in parallel

When we want to run more than one instance of WebDriver on the same tests, we use multiCapabilities, which take an array of capabilities. The protractor will run tests in parallel against each set of capabilities. Note that if multiCapabilities is defined, the runner will ignore the capabilities configuration.

So, this objective can be achieved by using a multiCapabilities block in the conf.js file.

Step 1: First, change the conf.js file consisting of the configuration to be used for this objective.

conf.js

// An example configuration file illustrate parallel execution 
exports.config = {
    directConnect: true,
    
    // Use multiCapabilities which takes an array of capabilities
    multiCapabilities: [
        {
            'browserName': 'firefox'
        },
        {
            'browserName': 'chrome'
        }
    ],
    
    // Framework to use. Jasmine is recommended
    framework: 'jasmine',
    
    // Spec patterns are relative to the current working directory when protractor is called.
    specs: ['spec_1.js'],
    SELENIUM_PROMISE_MANAGER: false,

    // Options to be passed to Jasmine.
    jasmineNodeOpts: {
        defaultTimeoutInterval: 30000
    }
};


Step 2
: Now run the spec_1.js script using the protractor conf.js command. The results are shown below:

Ran one Chrome and one Firefox browser instance: 

Ran one Chrome and one Firefox browser instance
Output:

Note that by default, when running multiple browser sessions, the results are not logged until the test run is complete. If verboseMultiSessions is set to true, when running multiple sessions in parallel, the results will be logged when each test is completed.


“Protractor parallel execution” Tech Bite was brought to you by Dženeta Ahmić, Junior Quality Assurance Engineer at Atlantbh.

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

selenium
QA/Test AutomationTech Bites
December 22, 2023

Selenium Grid 4 with Docker

Introduction When talking about automation testing, one of the first things that comes to mind is Selenium. Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of…

Want to discuss this in relation to your project? Get in touch:

Leave a Reply