Mayank
Mayank

Reputation: 11

How to run it blocks in parallel in protractor?

My spec file covers 50 different test cases for a specific test scenario. Each test case is covered in individual it block. The spec file takes 2 hrs to execute. I want to categorize it blocks in a way so that they run in parallel in multiple browser instances. For e.g. it should run like (5 it blocks * 10 browser instances) in parallel. I don't want to divide the spec file in different parts. Is there any alternate solution?

Upvotes: 0

Views: 2236

Answers (2)

jithinkmatthew
jithinkmatthew

Reputation: 930

When you decide to execute your test cases, use any configuration listed below. Please refer a typical conf.js file and choose any of the configuration in conf.js file.

exports.config = {

  // The address of a running selenium server.
  seleniumAddress: 'http://localhost:4444/wd/hub',

  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },

  specs: ['spec1.js','spec2.js']

};

Type-1: Single browser instance : start one browser instance at a time.

example configuration in the conf.js file.

capabilities: {
     browserName: 'chrome',
 },
specs: ['spec1.js','spec2.js']

The above configuration will launch chrome browser instance and executes all specs one by one

sequestial --> [chrome] --> spec1.js --> spec2.js

Type-2: Different browser instance will launch in parallel and all the specs which is mensioned in the spec:[] tag will execute in each browser's instance separately.

example configuration in the conf.js file.

multiCapabilities: [
     { browserName: 'chrome', },
     { browserName: 'firefox', },
 ],
specs: ['spec1.js','spec2.js','spec3.js']

The above configuration will launch chrome, firefox browsers and executes all specs in each browser instance parallel.

         --> [chrome]  ---|--> spec1.js --> spec2.js --> spec3.js
         |                
       parallel           
         |
         --> [firefox] ---|--> spec1.js --> spec2.js --> spec3.js

Type - 3: The below configuration will help you to execute different set of specs in different browser instances.

example configuration in the conf.js file.

multiCapabilities: [
    { 
        browserName: 'chrome', 
        specs:['spec1.js']
    },
    { 
        browserName: 'firefox', 
        specs:['spec2.js'] 
    },
],

The above configuration will launch chrome, firefox browsers and executes spec1.js in chrome and spec2.js in firefox instances

         --> [chrome]  ---|--> spec1.js
         |                
       parallel           
         |
         --> [firefox] ---|--> spec2.js

Type - 4: Execute different spec files in parallel with same browser.

Consider you have 5 spec files in your test suit and you want to execute everything in parallel. The below configuration will help you to do that.

example configuration in the conf.js file.

capabilities: {
    browserName: 'chrome',
    specs: [
        'spec1.js', 'spec2.js', 'spec3.js' , 'spec4.js' , 'spec5.js'
    ],
    maxInstances : 5,
    shardTestFiles: true
}

The above configuration will launch 5 chrome instances and each spec will execute in those 5 chrome instances in parallel.

                             |-- [instance 1] --|--> spec1.js
                             |-- [instance 2] --|--> spec2.js
             --> [chrome] ---|-- [instance 3] --|--> spec3.js
                             |-- [instance 4] --|--> spec4.js
                             |-- [instance 5] --|--> spec5.js

The maxInstances number represents the maximum amount of browser windows that Protractor is allowed to create.

For example, with maxInstances set to 10, a test suite of 200 tests would result in Protractor creating 10 Chrome instances with each instance running 20 tests, whereas without shardTestFiles this would be one Chrome instance running all 200 tests.

Upvotes: 4

Ben Mohorc
Ben Mohorc

Reputation: 694

You are unable to run "it" blocks parallel from within the same spec file. The alternate solution would be to divide up your spec file such as spec1.js, spec2.js, andspec3.js

You would then add this to your conf.js file

specs:['filePath/spec1.js','filePath/spec2.js','filePath/spec3.js'],
capabilities: {
    shardTestFiles: true,
    maxInstances: 5 //set this to amount you want running at once
}

The specs param will accept arrays, or you can use wildcards such as filePath/*.js which would run all .js files within that folder (Make sure they are are protractor test cases)

maxInstances will be set to the amount running at once. I find it easier to use headless mode when running multiple instances at once, for chrome the option looks like:

chromeOptions: {
        args: ["--headless", "--disable-gpu", "--window-size=1920,1080"]
}

Upvotes: 2

Related Questions