量角器&黄瓜。 this.visit不是函数

时间:2015-09-17 14:41:17

标签: protractor cucumberjs

我正在尝试使用量角器和黄瓜来为我们的一些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'
  }
};

homepage.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

homepageSteps.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));
        }
      });
};

1 个答案:

答案 0 :(得分: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;
};

您还需要安装zombie软件包:

npm install zombie --save-dev