All steps shown as pass before protractor cucumber

2019-06-11 14:17发布

While executing my scripts, Immediately all steps shows as pass in console after that my actual scripts getting executed. Even after returning promise in each step.

Feature File:

 Feature: Running Cucumber with Protractor

 Scenario: To verify the Search result
   Given I am on home page
    When I enter search value
    Then I verify the search page

Step Definition:

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

 module.exports = function() {
  this.Given(/^I am on home page$/, function () {
    browser.get(browser.baseUrl);
    return browser.driver.getTitle().then(function(pageTitle) {
    expect(pageTitle).equal('Online Shopping Site for Mobiles, Fashion, Books, Electronics, Home Appliances and More');
});
 });

 this.When(/^I enter search value$/, function () {
   return element(by.name('q')).sendKeys('iPhone 4s');
 });

 this.Then(/^I verify the search page$/, function () {
   browser.sleep(1000);
   return expect(element(by.className('KG9X1FUs7BSJ3tl0huXbH')).isPresent()).to.eventually.equal(true);    
   });
 }

And the Output shows as: enter image description here

1条回答
干净又极端
2楼-- · 2019-06-11 14:47

There are marked as green because you don't resolve the promise.You've choosen in your setup not to return a callback but to return a promise. This means that each last line of code should return a promise.

If you transform your code into promises you will get this

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

module.exports = function() {
  this.Given(/^I am on home page$/, function() {
    browser.get(browser.baseUrl);
    return expect(browser.driver.getTitle())
      .to.eventually.equal('Online Shopping Site for Mobiles, Fashion, Books, Electronics, Home Appliances and More');
  });

  this.When(/^I enter search value$/, function() {
    return element(by.name('q')).sendKeys('iPhone 4s');
  });

  this.Then(/^I verify the search page$/, function() {
    browser.sleep(1000);
    return expect(element(by.className('KG9X1FUs7BSJ3tl0huXbH')).isPresent()).to.eventually.equal(true);
  });
}

查看更多
登录 后发表回答