Protractor Cucumber BDD Tests Show Pass before Execution

后端 未结 1 1922
时光说笑
时光说笑 2020-12-17 05:57

I have a sample BDD test using Protractor with Cucumber. On executing the code, the console immediately shows the result as passed and the code actually begins executing onl

相关标签:
1条回答
  • 2020-12-17 06:38

    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.

    I had the same question and above statement was the response from one of the core members of the protractor-cucumber github forum.

    I prefer to return promises when I am performing some actions on the results with .then function and use .done callback function when I am not, Also you don't need callbacks now CucumberJS supports promises. So your step file should look like -

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

    I would recommend you to read about Promises http://www.html5rocks.com/en/tutorials/es6/promises/ as it requires some understanding how they behave.They can be sometimes tricky, it took me a while to get an idea still I have lot to learn :)

    0 讨论(0)
提交回复
热议问题