javascript - 如何将 Jasmine 与 CucumberJS 一起使用?

标签 javascript jasmine protractor bdd cucumberjs

如何将 jasmine 与 cucumberjs 一起使用?

我尝试了来自 https://stackoverflow.com/a/30763260/5453732 的解决方案

但我总是遇到这个错误:TypeError: this.expect(...).toBe is not a function at World. (/myApp/tests/e2e/steps/main.step.js:33:79)

第 39 行:

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

app/modules/user/tests/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",
  }
}

最佳答案

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

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

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

应该改为:

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

关于javascript - 如何将 Jasmine 与 CucumberJS 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045664/

相关文章:

automation - cucumberOpts.tags 与 Protractor + CucumberJs + Gulp 的用法

javascript - Charts.js 销毁以前的数据并更新

javascript - 使用 JavaScript 进行屏幕截图

javascript - 为什么这个函数调用会破坏程序并导致 undefined variable ?

javascript - 如何使requirejs与jasmine和blanketjs一起使用以实现代码覆盖率?

node.js - Jenkins 中没有安装 NodeJS

javascript - 页面开始加载时是否可以使用 Ruby 的 selenium-webdriver 执行 script

angular - dialogRef.afterClosed 不是一个函数

javascript - 如何使用 Jasmine 监视值属性(而不是方法)

angularjs - 如何在angularjs中测试真实的$http数据