不能用量角器进行黄瓜测试

时间:2016-01-29 14:14:09

标签: javascript testing protractor cucumberjs

每当我运行测试时,我都会收到错误: TypeError:e.getContext不是函数

我正在使用https://github.com/cucumber/cucumber-js中的示例以及world.js中的一些更改(使它们修复超时错误)

应用版本:

  • node 4.2.6
  • 黄瓜0.9.4
  • 量角器3.0.0
  • 量角器 - 黄瓜 - 框架0.3.3
  • zombie 4.2.1

我的 world.js

$msg = "some persian words";
$from = "my_email@my_server.com";
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "BCC: $to".PHP_EOL;
$message =  '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Message</title></head>
<body style="margin:0px; font-family:Tahoma, Geneva, sans-serif; font-size:12px;">
'.htmlspecialchars_decode($msg).'
<div style="margin-top:5px">Please do not reply to this email.</div>
</div></body></html>';

我的 sampleSteps.js

// features/support/world.js
var zombie = require('zombie');
zombie.waitDuration = '30s';
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;
  this.setDefaultTimeout(60 * 1000);
};

我的 sample.feature

// features/step_definitions/my_step_definitions.js

module.exports = function () {
  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 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(new Error("Expected to be on page with title " + title));
    }
  });
};

我的量角器-conf.js

# features/my_feature.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

1 个答案:

答案 0 :(得分:1)

我在这个例子中遇到了同样的问题。问题出在github.com页面上。什么问题,我不知道。

所以,我对要访问的页面进行了更改,测试开始运行时没有 TypeError:e.getContext不是函数

我更改了 sampleSteps.js 文件:

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

    this.visit('http://www.google.com/', 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 SEARCH page$/, 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:

    // changed to this one, otherwise next steps also are skipped...
    callback();
  });

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

    this.browser.assert.text('title', title);

    callback();
  });
};

然后对 sample.feature 进行了一些更改:

Feature: Example feature
  As a user of Google.com
  I want to have search with Google
  So that I can find something

  Scenario: Search something
    Given I am on the Google.com
    When I go to the SEARCH page
    Then I should see "Google" as the page title

我希望,这将有助于使用cucumber-js和zombie.js的第一步。

这不是量角器的问题,因为同样的问题是,当我在WebStorm中运行此示例时。