【问题标题】:How to configure Protractor to use Cucumber如何配置量角器以使用 Cucumber
【发布时间】:2014-05-21 14:09:30
【问题描述】:

截至0.20.1 Cucumber 现在在 Protractor 中得到完全支持,但我正在努力寻找有关如何正确配置它的任何文档。知道如何设置 world.js 吗?

我在https://github.com/whyvez/angular-cucumber-example/blob/master/features/support/world.coffee 找到了这个示例,但我不确定您是否仍需要指定所有需要的模块和配置,因为量角器配置文件 (referenceConf.js) 已经包含所有这些信息。

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

【问题讨论】:

    标签: protractor cucumberjs


    【解决方案1】:

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

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

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

    world.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

    希望这会有所帮助。

    【讨论】:

    • Pedro,我已经使用您的示例来进行测试,但是当我使用时,它会失败。在这种情况下,我应该使用哪个库来期望?
    • 很难在没有看到错误的情况下判断,但我猜你正在尝试使用 Jasmine 全局函数的“expect”。但是鉴于您在这里使用 Cucumber,您没有该功能。我喜欢使用 Chai.js。这是一个非常好的断言库。你只需要 var chai = require('chai');变种期望 = chai.expect;然后在您的步骤中使用它。如果你使用 Promise,你可能需要使用“chai-as-promised”。考虑到语法与基于 Jasmine 的 expect 函数不同。
    • 感谢佩德罗,我摆脱了使用期望和使用 if 条件来满足条件然后回调()。例如if (data && data.indexOf("World") > 0) callback(); else callback.fail(new Error("预计在带有 searchTerm 的页面" + searchTerm));
    • 大家好!我一直在尝试运行测试,但我一直得到这个:```启动器]错误:TypeError:未定义不是/usr/local/lib/node_modules/protractor/lib/frameworks/cucumber.js的函数: 132:36 在 Function.promise (/usr/local/lib/node_modules/protractor/node_modules/q/q.js:650:9) ```
    • 对于我之前的评论,我正在尝试使用量角器 path/to/config.js 运行
    【解决方案2】:

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

    这似乎只需要最少的配置,而且所需的内容都有明确的记录。

    【讨论】:

      【解决方案3】:

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

        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 错误问题)。

      【讨论】:

        【解决方案4】:

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

        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"
          }
        };
        

        更多信息在这里:Protractor Frameworks

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-01
          • 2015-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-10
          相关资源
          最近更新 更多