我怎样才能将Jasmine与CucumberJS一起使用?

时间:2015-12-02 14:48:23

标签: javascript jasmine protractor bdd cucumberjs

如何在yellowjs中使用jasmine?

我从https://stackoverflow.com/a/30763260/5453732

尝试了这个解决方案

但我总是有这个错误:TypeError:this.expect(...)。toBe不是World的函数。 (/myApp/tests/e2e/steps/main.step.js:33:79)

第39行:

this.expect(element(by.css('[data-el="' + field + '"]')).isPresent()).toBe(true);

应用/模块/用户/测试/ E2E / user.feature

#user.feature

Feature: Login feature
  As a user
  I want authenticate my account

  Scenario: Authentication success
    Given I am on "#/" page
    Given I check if "navbar-menu-user-module" is visible
    Given I wait "3" seconds

/tests/e2e/steps/main.step.js

module.exports = function () {

    this.World = require("../support/world.js").World;

    this.path = '#/';

    this.Given(/^I am on "?([^"]*)"? page$/, function (arg1, callback) {
        browser.get(arg1);
        callback();
    });

    this.Given(/^I wait "?([^"]*)"? seconds$/, function (arg1, callback) {
        browser.sleep(3000);
        callback();
    });

    this.Given(/^I check if "?([^"]*)"? is visible$/, function (field, callback) {
        this.expect(element(by.css('[data-el="' + field + '"]')).isPresent()).toBe(true);
        callback();
    });
};

/tests/e2e/support/world.js

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;

protractor.conf.js

/* protractor.conf.js */
exports.config = {
  directConnect: true,

  seleniumServerJar: 'node_modules/selenium-server/lib/runner/selenium-server-standalone-2.48.2.jar',

  specs: [
      'app/modules/user/tests/e2e/*.feature'
  ],

  getPageTimeout: 30000,

  capabilities: {
    'browserName': 'chrome',
    version: '',
    platform: 'ANY'
  },

  onPrepare: function() {
      var width = 1024, height = 800;

      browser.get('#/');
      browser.driver.manage().window().setSize(width, height);
  },

  framework: 'cucumber',

  cucumberOpts: {
    require: [
        'tests/e2e/steps/main.step.js'
    ],
    format: 'pretty', // or summary
    keepAlive: false
  },

  onCleanUp: function() {}

};

和我的HTML:

<a data-el="navbar-menu-user-module" href="./#/user">User Module</a>

的package.json

{
  "name": "myApp",
  "version": "1.0.0",
  "description": "myApp",
  "dependencies": {
  },
  "devDependencies": {
    "chai": "^3.3.0",
    "chai-as-promised": "^5.1.0",
    "jasmine-core": "~2.3.4",
    ...
    "protractor": "^2.5.1",
    "selenium-server": "^2.48.2",
    "selenium-standalone": "^4.7.0",
    "selenium-webdriver": "^2.48.0",
  }
}

2 个答案:

答案 0 :(得分:2)

要记住的关键是CucumberJS和Jasmine是互斥的。您只能将Jasmine的expect与Jasmine框架结合使用。 toBe()是一个由Jasmine的expect提供的函数,它在您的框架中不存在。这就是您收到所述错误的原因。

由于您使用CucumberJS构建测试,因此需要使用单独的断言库,最常用的是Chai。您需要使用函数provided by Chai作为断言。在您的情况下,您可能希望使用equal()函数。还要记住,Protractor的isPresent()函数会返回一个承诺,因此您需要使用eventually提供的chai-as-promised链。总之,以下断言:

this.expect(element(by.css('[data-el="' + field + '"]')).isPresent()).toBe(true);

应更改为:

this.expect(element(by.css('[data-el="' + field + '"]')).isPresent()).to.eventually.equal(true);

答案 1 :(得分:1)

您可以使用我们的jasmine-expect库。我们从Jasmine中取出了期望库,并将其作为一个单独的模块发布。

https://www.npmjs.com/package/xolvio-jasmine-expect