Is there a way to change the browser capabilities within beforeEach of the protractor suite. I need to set the Capabilities.name attribute before each spec execution.
-
1Why do you need to change capability names on the fly? Please elaborate, thanks. – alecxe Jan 16 '16 at 03:36
-
I am using SauceLabs to execute protractor tests. Each test is defined within jasmine suite and I should initiate new browser instance before each spec execution. I am planning to set the current spec name as capabilities.name so that sauce report can show the spec name – user4971738 Jan 16 '16 at 03:40
3 Answers
To create separate instances of the desired capabilities, such as capabilities.name, you will want to try the multiCapabilities option available via Protractor. An example would look similar to what is below and reside in the conf.js file. This allows you to submit a unique name for each test session.
onPrepare: function(){
var caps = browser.getCapabilities()
},
multiCapabilities: [{
browserName: 'firefox',
version: '32',
platform: 'OS X 10.10',
name: "firefox-tests",
shardTestFiles: true,
maxInstances: 25
}, {
browserName: 'chrome',
version: '41',
platform: 'Windows 7',
name: "chrome-tests",
shardTestFiles: true,
maxInstances: 25
}],
A complete example of this can be seen here:
https://github.com/saucelabs-sample-test-frameworks/JS-CucumberJS-Protractor3.0/blob/master/conf.js

- 186
- 6
-
Does this still work in Protractor Version 5.4.0? I got TypeError: caps.setCapability is not a function – Jeremy Kahan May 31 '20 at 23:20
Here are the sauceLabs capabilities: https://wiki.saucelabs.com/display/DOCS/Test+Configuration+Options
When you don't specify a Capabilities.name
it looks like sauceLabs reports each test formatted as browserName:specFilename
by default.

- 1,271
- 11
- 15
-
Actually, it doesn't add the browserName by default, but Protractor will append the spec file name to whatever you set as Capabilities.name. The Sauce Labs sample framework https://github.com/saucelabs-sample-test-frameworks/JS-Protractor-Selenium sets the name to match the browserName in capabilites.name in conf.js and Protractor appends the spec filename by default. – fijiaaron Nov 17 '17 at 23:09
You can't change the capabilities in beforeEach()
(Jasmine hook) or onPrepare()
(Protractor conf.js) because the browser instance has already been created and webdriver session has been started with the capabilities already sent to the Selenium server.
Desired capabilities are set in the conf.js under Capabilities
or Multicapabilities
. You could set them at runtime by getting a variable before exporting in conf.js.
One common way to do this is to set the capability using an environment variable, for example:
Capabilities: {
browserName: process.env.SELENIUM_BROWSER
}
You can set variables to be used in capabilities in beforeLaunch()
but this executes one time only, before any specs are read.
There is an excellent summary of the Protractor / Jasmine hooks here:
http://timothymartin.azurewebsites.net/protractor-before-and-afters/
I have not yet identified a way (without modifying Protractor source) to change capabilities dynamically on a per-spec or per-suite basis.

- 5,015
- 3
- 35
- 28