如何使用茉莉花和CucumberJS用量角器 [英] How to use Jasmine and CucumberJS with Protractor

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

问题描述

我期待用量角器,CucumberJS,和Jasmine用于测试我的项目。我如何使用这两种茉莉花和CucumberJS用量角器?下面是我创建的项目设置:

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:

/路径/要/ 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补充的吗?这会不会通过量角器配置文件或别的地方在code?

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?

/路径/要/ MyProj文/ 功能/ 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

/路径/要/ MyProj文/ 功能/ stepDefinitions.js

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

在此执行,期望没有定义,presumably因为茉莉花尚未整合,它的期望与它一起全球性的。下面是完整的错误消息:

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

/路径/要/ 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量角器Git仓库,因为最新的版本(2.1.0)拥有的一个bug ,该美元,CucumberJS p $ pvents集成。

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和茉莉花是互斥的;你将无法使用茉莉花的预计,黄瓜步骤。你有什么做的,而不是为单独加载期望的模块。我建议薛宝钗-as-承诺插件。 (柴AS-承诺简化了周围的承诺写入预期的过程。量角器覆盖期望()在茉莉花功能,为你做这一幕后)最有可能你会想这样做,在你的世界因为这是为你的步骤定义访问它的最简单方法。你的世界会是这个样子:

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文档和你的世界重新定义步将范围提供进入世界的所有属性:

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

现在,对于一些无耻的自我推销:如果您使用的量角器与CucumberJS,我会建议在寻找一个模块我帮助建立所谓的 CukeFarm 。它配备preconfigured用几个模块,你会发现有用的,它提供了一些可以在大多数的项目中使用的一般步骤定义的。

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.

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

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