Protractor - Change Browser Capabilities at RunTime

依然范特西╮ 提交于 2019-12-07 08:39:14

问题


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.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/34823248/protractor-change-browser-capabilities-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!