量角器黄瓜BDD测试在执行前显示通过

时间:2016-07-15 21:20:12

标签: javascript cucumber protractor bdd

我使用Protractor with Cucumber进行了BDD测试。在执行代码时,控制台立即显示传递的结果,并且代码实际上仅在此之后开始执行。

我希望执行状态显示与实际执行同步。(例如控制台显示 - ' 鉴于我启动了量角器演示页'并执行了下面的代码,然后控制台显示下一步等等)我知道它与Async编码和回调有关,但不能确定问题确实存在。

功能文件:

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

    };

1 个答案:

答案 0 :(得分:7)

  

您需要return承诺或在步骤定义中使用done回调。否则黄瓜不知道你的异步时间   行动完成。

我有同样的问题,上面的陈述是protractor-cucumber github论坛的核心成员之一的回复。

当我使用return函数对结果执行某些操作时,我更喜欢.then承诺,当我不使用时,我更喜欢使用.done回调函数,而且您不需要{{ 1}}现在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'); }); }); }; http://www.html5rocks.com/en/tutorials/es6/promises/的内容,因为它需要了解他们的行为。他们有时候会很棘手,我花了一段时间才能得到一个想法我还有很多需要学习的地方:)