Man*_*nya 3 javascript bdd cucumber protractor
我有一个使用Protractor with Cucumber的样本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
Run Code Online (Sandbox Code Playgroud)
步骤文件:
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();
});
};
Run Code Online (Sandbox Code Playgroud)
您需要
return承诺或done在步骤定义中使用回调.否则,黄瓜不知道您的异步操作何时完成.
我有同样的问题,上面的声明是protractor-cucumbergithub论坛的核心成员之一的回应.
我宁愿return承诺,当我表演上的结果一些动作.then的功能和用途.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');
});
});
};
Run Code Online (Sandbox Code Playgroud)
我建议你阅读Promises http://www.html5rocks.com/en/tutorials/es6/promises/,因为它需要了解他们的行为.他们有时候会很棘手,我花了一段时间才得到一个想法我有很多东西需要学习:)
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |