我有一个样本BDD测试使用量角器与黄瓜。在执行代码时,控制台立即显示传递的结果,而代码实际上只在该结果之后才开始执行。
我希望执行状态显示与实际执行同步。(例如控制台显示-“如果启动量角器演示页”,下面的代码被执行,然后控制台显示下一步等等),我知道它与异步编码和回调有关,但无法解决确切的问题。
特征文件:
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();
});
};发布于 2016-07-16 08:47:10
您需要在步骤定义中使用
return承诺或使用done回调。否则,黄瓜不知道异步操作何时完成。
我也有同样的问题,上面的发言是protractor-cucumber github论坛的核心成员之一的回应。
当我使用return函数对结果执行某些操作时,我更喜欢使用.then承诺,而在不使用.done回调函数时,更喜欢使用.done回调函数,而且您现在也不需要callbacks现在就支持承诺。所以你的step文件应该是-
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有时是很棘手的,我花了一段时间才得到一个想法,但我还有很多需要学习的地方:)
https://stackoverflow.com/questions/38405184
复制相似问题