11

As of 0.20.1 Cucumber is now fully supported in Protractor but I'm battling to find any documentation on how to configure it properly. Any idea how you would setup world.js?

I have found this example at https://github.com/whyvez/angular-cucumber-example/blob/master/features/support/world.coffee but I'm not sure if you would still need to specify all the require modules and configuration as the protractor config file (referenceConf.js) would have all this info already.

assert = require 'assert'
path = require 'path'

protractor = require 'protractor'
webdriver = require 'selenium-webdriver'

driver = new webdriver.Builder().
  usingServer('http://localhost:4444/wd/hub').
  withCapabilities(webdriver.Capabilities.chrome()).
  build()

driver.manage().timeouts().setScriptTimeout(100000)

ptor = protractor.wrapDriver driver

class World
  constructor: (callback) ->
    @browser = ptor
    @By = protractor.By
    @assert = assert
    callback()

module.exports.World = World
4

4 回答 4

13

我创建了一个示例项目来展示如何使用 Cucumber 配置 Protractor 并利用 World。

The World 是一个在不同场景之间共享共性的地方,这样您就可以让您的代码井井有条。

实际上,您只需在 /features 下名为 /support 的文件夹中创建您的 world.js 文件。你也会把你的钩子放在那里。您的步骤定义中将提供每个属性或函数。

世界.js:

module.exports = function() {

  this.World = function World(callback) {
    this.prop = "Hello from the World!";

    this.greetings = function(name, callback) {
      console.log("\n----Hello " + name);
      callback();
    };

    callback();
}

然后在您的步骤中:

var sampleSteps = function() {

    this.Given(/^this is the first sample$/, function (callback) {
      console.log("\n----" + this.prop);
      callback();
    });

    this.Given(/^this is the second sample$/, function (callback) {
      this.greetings("everybody", callback);
    });

};

module.exports = sampleSteps;

你的 protractor.js 配置文件看起来像这样:

exports.config = {

  specs: [
    'e2e/features/*.feature'
  ],

  capabilities: {
    'browserName': 'chrome'
  },

  baseUrl: 'http://localhost:8081/',

  framework: 'cucumber',

};

这是 GitHub 存储库。

https://github.com/plopcas/st-protractor-cucumber

希望这可以帮助。

于 2014-09-14T18:17:50.720 回答
1

看看protractor-cucumbe——它带有 selenium-webdriver,支持 Promises,并且有据可查。

它似乎需要最少的配置,并且清楚地记录了所需的内容。

于 2016-02-18T14:30:41.130 回答
0

将其作为框架添加到配置文件中:

exports.config = {
  // set to "custom" instead of cucumber.
  framework: 'custom',

  // path relative to the current config file
  frameworkPath: 'protractor-cucumber-framework'

  // relevant cucumber command line options
  cucumberOpts: {
    format: "summary"
  }
};

更多信息:量角器框架

于 2015-03-31T19:35:00.073 回答
0

我从这个设置中获得了很好的里程

  class ChtWorld
    chai = require('chai');
    chaiAsPromised = require('chai-as-promised');

    constructor:  ->
      @browser = @protractor = require('protractor').getInstance()
      @By = @protractor.By
      chai.use(chaiAsPromised)
      @expect= chai.expect


  module.exports= ->
    this.World= (callback) ->
      w = new ChtWorld()
      callback(w)

由于量角器已经设置好了,只需要对其进行引用就足够了(请注意,要让 Cucumber 正确加载新世界,modules.exports 必须恰到好处)。

作为旁注,它位于 features/support/world.coffee 中,并没有明确添加到“要求”列表中(试图这样做让我陷入 Gherkin Lexing 错误问题)。

于 2014-05-22T07:11:11.077 回答