Protractor Cucumber BDD 测试在执行前显示通过

Protractor Cucumber BDD Tests Show Pass before Execution

我有一个使用 Protractor 和 Cucumber 的示例 BDD 测试。执行代码时,控制台会立即显示结果已通过,然后代码才真正开始执行。

我希望执行状态显示与实际执行同步。(例如控制台显示 - 'Given I launch the protractor demo page' 并且下面的代码被执行,然后控制台显示下一步等等)我知道它与异步编码和回调有关,但无法找出确切的问题。

特征文件:

Feature: Test
Scenario:  Test Scenario
    Given I launch the protractor demo page
    When I enter two in the first field
    And I enter three in the second field
    And I click Go button
    Then Result should be displayed as Five

步骤文件:

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

    module.exports = function () {


        this.Given(/^I launch the protractor demo page$/, function (callback) {
            browser.driver.manage().window().maximize();
            browser.get('http://juliemr.github.io/protractor-demo/');

            browser.getTitle().then(function(text){
               console.log('title is - ' + text);
                expect(text).to.equal('Super Calculator');
            });
         callback();
        });

        this.When(/^I enter two in the first field$/, function (callback) {
            element(by.model('first')).sendKeys('2');
            callback();
        });

        this.When(/^I enter three in the second field$/, function (callback) {
            element(by.model('second')).sendKeys('3');
            callback();
        });

        this.When(/^I click Go button$/, function (callback) {
            element(by.id('gobutton')).click();
            callback();
        });

        this.Then(/^Result should be displayed as Five$/, function (callback) {
             element(by.repeater('result in memory')).all(by.tagName('td')).get(2).getText().then(function(text){
            expect(text).to.equal('5');
            });
            callback();
        });

    };

You need to either return a promise or use the done callback in your step definitions. Otherwise cucumber doesn't know when your asynchronous actions are complete.

我也有同样的问题,以上是protractor-cucumbergithub论坛一位核心成员的回复。

当我使用 .then 函数对结果执行一些操作时,我更喜欢 return promises,当我不使用时使用 .done 回调函数,而且你不需要callbacks 现在 CucumberJS 支持承诺。所以你的步骤文件应该看起来像 -

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

module.exports = function () {


    this.Given(/^I launch the protractor demo page$/, function () {
        browser.driver.manage().window().maximize();
        browser.get('http://juliemr.github.io/protractor-demo/');

      return browser.getTitle().then(function(text){
           console.log('title is - ' + text);
            expect(text).to.equal('Super Calculator');
        });
    });

    this.When(/^I enter two in the first field$/, function () {
       return element(by.model('first')).sendKeys('2'); 
    });

    this.When(/^I enter three in the second field$/, function () {
       return element(by.model('second')).sendKeys('3'); // you can use return also
    });

    this.When(/^I click Go button$/, function () {
        return element(by.id('gobutton')).click();
    });

    this.Then(/^Result should be displayed as Five$/, function () {
        return element(by.repeater('result in memory')).all(by.tagName('td')).get(2).getText().then(function(text){
        expect(text).to.equal('5');
        });

    });

};

我建议您阅读有关 Promises http://www.html5rocks.com/en/tutorials/es6/promises/ 的内容,因为它需要一些理解 behave.They 有时会很棘手,我花了一段时间才明白我的想法有很多东西要学:)