Protractor 和 cucumber 。 this.visit 不是一个函数

标签 protractor cucumberjs

我正在尝试使用 Protractor 和 Cucumber 向我们的一些 Web 应用程序添加一些功能性 BDD 测试。将与此过程相关的在线信息碎片拼凑在一起,我成功地拼凑出了一个非常基本的测试,但是当我使用 protractor conf.js 运行测试时,我收到以下错误

this.visit is not a function

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

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

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

最佳答案

看起来您从这里获取了代码示例: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

关于 Protractor 和 cucumber 。 this.visit 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633189/

相关文章:

javascript - 识别切换栏元素文本并在 Protractor 中单击它

javascript - 使用 Protractor 和 cucumberjs 选择单选按钮选项时出错

protractor - 当场景失败时停止 cucumberJs - protractor 和 cucumberjs

selenium - protractor/selenium 开始忽略 protractor.conf.js 中的 chrome 选项 (chromeOptions)

javascript - 无法添加新的给定/何时/然后,出现错误 `SyntaxError: Invalid regular expression: missing/`

javascript - 黑猩猩.js 配置

visual-studio-code - 如何在Visual Studio Code(VSCode)中调试Cucumber?

jasmine - Protractor 与 Mocha 和 Chai

javascript - Protractor 测试中的 request-promise + co API 触发器

angularjs - 使用 Protractor 时如何运行单个特定测试用例