0
votes

So I am brand new at Javascript, my only language before this was Ruby. I have written API tests with Cucumber and Ruby for years, but now I am trying to figure out UI tests for an angular app using Protractor and Cucumber.js. I have the framework set up and the test steps run are passing, but falsely so.

Here is a snippet of my step definitions, with a few edits to change data in assertions and the string for the assertion is intentionally wrong to trigger a failure. They run and are passing, but only because it ignores the assertion. I don't see it actually doing anything in the browser, but if I put in console.log messages I do see them in the console. However, if I comment out the last callback, then I can see it run in the browser and it actually checks the assertions and fails as it should.

Cucumber doesn't require callbacks, and removing them results in it running in exactly the same way... only I can't comment out a callback of course and watch it run like I mentioned above.

And if I don't put that timeout in the first step, then the whole thing errors out at the first step with "Error: function timed out after 5000 milliseconds"

Why?!? Thanks!!

Protractor 5.3.0 with Cucumber 4.0.0 and protractor-cucumber-framework 4.2.0

Given('I am on the home page', {timeout: 30000}, (callback) => {
    browser.waitForAngularEnabled(false);
    browser.get(browser.params.env.int).then(callback);
});

Then('the log in form is displayed', callback => {
    expect(element(by.id('email')).isPresent()).to.eventually.be.true;
    expect(element(by.id('password')).isPresent()).to.eventually.be.true;
    callback();
});

When('I enter my user name', callback => {
    element(by.name('email')).sendKeys('[email protected]');
    expect(element(by.id('email')).getAttribute('value')).to.eventually.equal('something that does match');
    callback();
});

When('I enter my password', callback => {
    element(by.name('password')).sendKeys('blah');
    callback();
});

When('I click the log in button', callback => {
    element(by.buttonText('Log In')).click();
    callback();
});

Then('I am on the X page', callback => {
    expect(browser.getCurrentUrl()).to.eventually.contains('Y');
    // callback();
});
1

1 Answers

0
votes

1) For issue: "Error: function timed out after 5000 milliseconds

This is due to your step definition function execution time duration exceeds the default timeout: 5 secs.

You can change this time out globally by following my another post, or as you did add timeout only on needed step definition functions individually.

2) For Cucumber callback issue, You can choose to use callback, or choose to return a promise likely in each step definition function. I prefer the latter approach.

var { Given, Then, When } = require("cucumber");

Given(/^open cucumberjs github page$/, ()=> {
    browser.get("https://github.com/cucumber/cucumber-js");
    return expect(browser.getTitle()).to.eventually
          .equal("cucumber/cucumber-js: Cucumber for JavaScript");
}); 

When('I enter my password', ()=> {
    return element(by.name('password')).sendKeys('blah');
});

When('I click the log in button', ()=> {
    return element(by.buttonText('Log In')).click();

});

More code example, you can find from my example project here