2

我正在尝试使用量角器和黄瓜来为我们的一些 webapps 添加一些功能性 BDD 测试。拼凑与此过程相关的在线信息碎片,我设法拼凑了一个非常基本的测试,但是当我运行测试时protractor conf.js出现以下错误

this.visit 不是函数

我确定这是我做的根本错误的事情,但是有人可以告诉我我的方式的错误吗?

此测试的完整控制台内容如下:

Using the selenium server at http://192.168.12.100:4724/wd/hub
[launcher] Running 1 instances of WebDriver

Feature: Example feature
  As a user of cucumber.js
  I want to have documentation on cucumber
  So that I can concentrate on building awesome applications


  Scenario: Reading documentation                   # features/homepage.feature:6
    Given I am on the Cucumber.js GitHub repository # features/homepage.feature:7
      TypeError: this.visit is not a function
        at World.<anonymous> (/Users/fraserh/Documents/WorkingDir/protractor/features/homepageSteps.js:14:11)
        at doNTCallback0 (node.js:407:9)
        at process._tickCallback (node.js:336:13)

    When I go to the README file                    # features/homepage.feature:8
    Then I should see "Usage" as the page title     # features/homepage.feature:9


(::) failed steps (::)

TypeError: this.visit is not a function
  at World.<anonymous> (/Users/fraserh/Documents/WorkingDir/protractor/features/homepageSteps.js:14:11)
  at doNTCallback0 (node.js:407:9)
  at process._tickCallback (node.js:336:13)


Failing scenarios:
features/homepage.feature:6 # Scenario: Reading documentation

1 scenario (1 failed)
3 steps (1 failed, 2 skipped)
[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

我有以下结构:

conf.js
features/homepage.feature
features/homepageSteps.js

conf.js

exports.config = {
  framework: 'cucumber',
  seleniumAddress: 'http://192.168.12.100:4724/wd/hub', //this is a working selenium instance
  capabilities: {
    'browserName': 'chrome'
  },
  specs: ['features/homepage.feature'],
  cucumberOpts: {
    require: 'features/homepageSteps.js',
    format: 'pretty'
  }
};

主页.feature

Feature: Example feature
  As a user of cucumber.js
  I want to have documentation on cucumber
  So that I can concentrate on building awesome applications

  Scenario: Reading documentation
    Given I am on the Cucumber.js GitHub repository
    When I go to the README file
    Then I should see "Usage" as the page title

主页Steps.js

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

var expect = chai.expect;

module.exports = function() {
    var that = this;
    this.Given(/^I am on the Cucumber.js GitHub repository$/, function (callback) {
        // Express the regexp above with the code you wish you had.
        // `this` is set to a new this.World instance.
        // i.e. you may use this.browser to execute the step:

        this.visit('https://github.com/cucumber/cucumber-js', callback);

        // The callback is passed to visit() so that when the job's finished, the next step can
        // be executed by Cucumber.
      });

      this.When(/^I go to the README file$/, function (callback) {
        // Express the regexp above with the code you wish you had. Call callback() at the end
        // of the step, or callback.pending() if the step is not yet implemented:

        callback.pending();
      });

      this.Then(/^I should see "(.*)" as the page title$/, function (title, callback) {
        // matching groups are passed as parameters to the step definition

        var pageTitle = this.browser.text('title');
        if (title === pageTitle) {
          callback();
        } else {
          callback.fail(new Error("Expected to be on page with title " + title));
        }
      });
};
4

1 回答 1

3

看起来您从这里获取了代码示例:https ://github.com/cucumber/cucumber-js

你错过了创建 this.visit 函数的下一段代码:

// features/support/world.js
var zombie = require('zombie');
function World() {
  this.browser = new zombie(); // this.browser will be available in step definitions

  this.visit = function (url, callback) {
    this.browser.visit(url, callback);
  };
}

module.exports = function() {
  this.World = World;
};

您还需要安装僵尸包:

npm install zombie --save-dev
于 2015-11-20T14:48:19.870 回答