【问题标题】:How to use Jasmine and CucumberJS with Protractor如何将 Jasmine 和 CucumberJS 与量角器一起使用
【发布时间】:2015-12-03 00:08:52
【问题描述】:

我希望使用 Protractor、CucumberJS 和 Jasmine 来测试我的项目。如何将 Jasmine 和 CucumberJS 与 Protractor 一起使用?这是我创建的项目设置:

/path/to/myproj/protractor.conf.js

exports.config = {
  seleniumServerJar: 'node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',

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

  baseUrl: 'http://localhost:8080',

  multiCapabilities: [
    {
      'browserName': 'chrome'
    }
  ],

  allScriptsTimeout: 380000,
  getPageTimeout: 20000,

  framework: 'cucumber',

  cucumberOpts: {
    require: 'features/stepDefinitions.js',
    format: 'summary'
  }
};

如您所见,本项目使用“黄瓜”作为框架。如何在 CucumberJS 旁边添加 Jasmine 框架?这是通过量角器配置文件还是代码中的其他地方?

/path/to/myproj/features/demo.feature

Feature: Some terse yet descriptive text of what is desired

  Scenario: Some determinable business situation
    Given some precondition

/path/to/myproj/features/stepDefinitions.js

module.exports = function() {
  this.Given(/^some precondition$/, function (callback) {
    expect(true).toEqual(true);
    callback();
  });
};

执行这个时,没有定义“expect”,大概是因为Jasmine没有被集成,并且它是expect全局的。以下是完整的错误信息:

$ $(npm bin)/protractor protractor.conf.js 
Starting selenium standalone server...
[launcher] Running 1 instances of WebDriver
Selenium standalone server started at http://192.168.1.115:59957/wd/hub
(::) failed steps (::)

ReferenceError: expect is not defined
  at World.<anonymous> (/path/to/myproj/features/stepDefinitions.js:3:5)
  at process._tickCallback (node.js:355:11)


Failing scenarios:
/path/to/myproj/features/demo.feature:3 # Scenario: Some determinable business situation

1 scenario (1 failed)
1 step (1 failed)
Shutting down selenium standalone server.
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1

/path/to/myproj/package.json

{
  "name": "myproj",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "cucumber": "0.4.9",
    "protractor": "git+https://github.com/angular/protractor.git#0262268fa43b9eefac815d986740efa07bb15818"
  }
}

注意:我在 package.json 中使用了对 Protractor Git 存储库的特定提交,因为最新版本 (2.1.0) 有 a bug,这会阻止与 CucumberJS 的集成。

【问题讨论】:

    标签: angularjs testing jasmine cucumber protractor


    【解决方案1】:

    CucumberJS 和 Jasmine 是互斥的;您将无法在 Cucumber 步骤中使用 Jasmine 的期望。你需要做的是加载一个单独的期望模块。我建议 Chai 使用 chai-as-promised 插件。 (chai-as-promised 简化了围绕 Promise 编写期望的过程。Protractor 覆盖 Jasmine 中的 expect() 函数在幕后为您执行此操作)您很可能希望在您的 World 中执行此操作,因为这是最简单的方法在您的步骤定义中提供对它的访问。你的世界应该是这样的:

    var World, chai, chaiAsPromised;
    chai = require('chai');
    chaiAsPromised = require('chai-as-promised');
    
    World = function World(callback) {
      chai.use(chaiAsPromised);
      this.expect = chai.expect;
      callback();
    }
    
    module.exports.World = World;
    

    然后在您的 Step Definitions 文件中,您只需根据 CucumberJS documentation 在 World 中加载,您的 Step Definitions 将被限定为提供对 World 的所有属性的访问:

    module.exports = function() {
    
      this.World = require("path/to/world.js").World;
    
      this.Given(/^some precondition$/, function (callback) {
        this.expect(true).to.equal(true);
        callback();
      });
    };
    

    现在,为了一些无耻的自我推销:如果你正在使用 Protractor 和 CucumberJS,我建议查看我帮助构建的一个名为 CukeFarm 的模块。它预配置了一些您会发现有用的模块,并提供了许多可用于大多数任何项目的通用步骤定义。

    【讨论】:

    • Mocha 是一个测试框架,它允许使用任何期望模块,包括 Chai。这是否是 Mocha 作为单元测试框架受欢迎的原因之一,以便用于编写 Cucumber 测试的相同预期模块也可以用于编写单元测试,从而在测试代码的基础上产生更一致的代码?感谢您的回答和 CukeFarm 的推荐!
    • @AristarkhArtemiy 是的,这是选择 Mocha 的一个很好的理由。在单元测试和 Cucumber 步骤定义中使用相同的模式/语法(例如使用 chai)是一件好事,尤其是当其他人必须跳入代码库时。
    • 能否详细说明 CucumberJS 和 Jasmine 互斥的原因?我正在尝试将 CucumberJs 集成到量角器中以运行 E2E 测试。如果我能做到这一点,那就太好了。我正在使用的堆栈:Angular2、webpack、..
    • @SamVloeberghs 当然,CucumberJS 和 Jasmine 都是测试框架。它们是解决相同问题的不同库。说你想在 CucumberJS 中使用 Jasmine 就像说你想在 Mocha 中使用 Jasmine,或者在 Grunt 中使用 Gulp,或者在 Ember 中使用 Angular。至于使用 Protractor:Protractor 是一个测试运行器,它需要一个测试框架来提供结构。他们似乎更喜欢 Jasmine,但他们支持您想要的任何框架。查看他们的 frameworks.md 文件以获取有关使用 CucumberJS 设置 Protractor 的提示,或者只使用像 CukeFarm 这样为您提供的库。
    • @SamVloeberghs 回复:我之前的评论:frameworks.mdCukeFarm
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多