如何将 Jasmine 和 CucumberJS 与量角器一起使用 [英] How to use Jasmine and CucumberJS with Protractor

查看:17
本文介绍了如何将 Jasmine 和 CucumberJS 与量角器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking to use Protractor, CucumberJS, and Jasmine for testing my project. How do I use both Jasmine and CucumberJS with Protractor? Here's the project setup I've created:

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

/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 框架?这是通过 Protractor 配置文件还是代码中的其他位置?

As you can see, this project uses "cucumber" as the framework. How do I add in the Jasmine framework alongside CucumberJS? Would this be through the Protractor configuration file or someplace else in the code?

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

/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

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

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

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

When this is executed, "expect" is not defined, presumably because Jasmine has not been integrated, and it's expect global along with it. Here's the full error message:

$ $(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

/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) 具有 一个错误,它阻止了与 CucumberJS 的集成.

Note: I'm using a particular commit to the Protractor Git repository in my package.json, because the latest version (2.1.0) has a bug, which prevents integration with CucumberJS.

推荐答案

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

CucumberJS and Jasmine are mutually exclusive; you won't be able to use Jasmine's expects in Cucumber steps. What you have to do instead is load a separate expectation module. I would suggest Chai with the chai-as-promised plugin. (chai-as-promised simplifies the process of writing expectations around promises. Protractor overrides the expect() function in Jasmine to do this for you behind the scenes) Most likely you'll want to do this in your World as that's the easiest way to provide access to it in your Step Definitions. Your World would look something like this:

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 文档将其加载到世界中 并且您的 Step Definitions 将被限定为提供对 World 的所有属性的访问:

Then in your Step Definitions file you just load in the World per the CucumberJS documentation and you're Step Definitions will be scoped to provide access to all properties of the 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.它预配置了一些您会发现有用的模块,并提供了许多可用于大多数项目的通用步骤定义.

Now, for some shameless self-promoting: if you're using Protractor with CucumberJS, I would recommend looking at a module I helped build called CukeFarm. It comes preconfigured with a few modules you'll find useful and it provides a number of general Step Definitions that can be used on most any project.

这篇关于如何将 Jasmine 和 CucumberJS 与量角器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆