My Cucumber step is getting passed even though expect statement is failing. It seems steps is running before expect statement is completed.
Please let me know how can I instruct the cucumber step to fail if expect is getting failed.
[Folder Structure] Following is my Feature file
#features/test.feature
Feature: Running Cucumber with Protractor
As a user of Protractor
I should be able to use Cucumber
In order to run my E2E tests
Scenario: Protractor and Cucumber Test
Given I go to "https://angularjs.org/"
When I add "Be Awesome" in the task field
And I click the add button
Then I should see my new task in the list
Following is Step Definition
var chai = require('chai'),
expect = chai.expect,
chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();
var {defineSupportCode} = require('cucumber');
let scenarioTimeout = 200 * 1000;
defineSupportCode(({setDefaultTimeout}) => {
setDefaultTimeout(scenarioTimeout);
});
defineSupportCode(function({Given, When, Then}) {
Given(/^I go to "([^"]*)"$/, function(site) {
return browser.get(site);
});
When(/^I add "([^"]*)" in the task field$/, function(task) {
return element(by.model('todoList.todoText')).sendKeys(task);
});
When(/^I click the add button$/, function() {
var el = element(by.css('[value="add"]'));
return el.click();
});
Then(/^I should see my new task in the list$/, function() {
var todoList = element.all(by.repeater('todo in todoList.todos'));
return expect(todoList.get(2).getText()).to.eventually.equal('Not Awesome');
});
});
All works well If I write Then step as below :
Solution 1:
element.all(by.repeater('todo in todoList.todos')).then(function(items){
expect(items[2].getText()).to.eventually.equal('Not Awesome').and.notify(callback);
});
Solution2:
return element.all(by.repeater('todo in todoList.todos')).then(function(items){
return expect(items[2].getText()).to.eventually.equal('Not Awesome');
});
I really want to understand why it did not work if I have written Then step in the below way:
Then(/^I should see my new task in the list$/, function() {
var todoList = element.all(by.repeater('todo in todoList.todos'));
return expect(todoList.get(2).getText()).to.eventually.equal('Not Awesome');