如何在量角器中使用Jasmine和CucumberJS

问题描述

CucumberJS和Jasmine是互斥的;您将无法在黄瓜步骤中使用Jasmine的期望。您要做的是加载一个单独的期望模块。我建议应许的插件。(chai-as-promise简化了围绕诺言编写期望的过程。Protractor重写了expect()Jasmine 的功能以在后台为您执行此操作)您很可能希望在自己的世界中执行此操作,因为这是提供访问权限的最简单方法在您的步骤定义中。您的世界看起来像这样:

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;

然后在您的“步骤定义”文件中,您只需按照CucumberJS文档加载到“世界”中,您将获得“步骤定义”的范围,以提供对“世界”所有属性的访问:

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的模块。它预配置了一些您会发现有用的模块,并且提供了许多可用于大多数项目的常规步骤定义。

解决方法

我希望使用Protractor,CucumberJS和Jasmine来测试我的项目。如何在量角器中同时使用Jasmine和CucumberJS?这是我创建的项目设置:

/ 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 / 功能/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();
  });
};

执行此操作时,未定义“期望”,可能是因为Jasmine尚未集成,并且期望与它一起全局。这是完整的错误消息:

$ $(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)有一个bug,阻止了与CucumberJS的集成。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...