0

I'm trying to wait for a promise resolution using Cucumber, Chai, and Protractor. Is there a way using Chai to wait for something (like a pageload) to occur before sending the callback?

I want something like:

browser.get(url).then(callback)

which I thought would be in Chai:

browser.get(url).should.be.fulfilled.and.notify(callback);

although when I do that, I'm just getting a timeout, but I see the page has loaded. I already have it setup with:

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

I don't want to check for something, I just want to make sure the page loads. From what I've seen most people just do:

browser.get(url);
callback();

and only use an assert or expect in a Gherkins Then clause, but I want to wait for the page to load in a Given or When.

user3554664
  • 349
  • 2
  • 16

1 Answers1

1

What I understand with your question is you want the asynchronous Given, When, Then's in CucumberJS to behave as synchronous execution, So that once your step with browser.get(url) is completed then the next step definition is executed. If that is what your question is then Yes we can do that-

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 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 () {
   return browser.get('http://juliemr.github.io/protractor-demo/');
    });
});

this.When(/^I check the title of the page$/, function () {
   return browser.getTitle().then(function(text){
       console.log('title is - ' + text);
        expect(text).to.equal('Super Calculator');
    });

});
Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • When I try to use the return function like `return browser.get('http://google.com');` it still fails with a timeout error. I've also tried it with a `.then` afterwards with a `callback` which times out as well. – user3554664 Aug 17 '16 at 16:26
  • ok i think your page takes more than the default time of 5000 ms of the steps that is why you are getting timeout error.did you try increasing the default time? – Ram Pasala Aug 17 '16 at 16:40
  • I've tried up to a minute. I see the page load, although nothing happens. – user3554664 Aug 17 '16 at 16:41
  • are you using angular or non-angular app? – Ram Pasala Aug 17 '16 at 16:46
  • angular app, but I've tested it also just on google.com – user3554664 Aug 17 '16 at 16:47
  • It is strange but nevertheless can you tell me which protractor , angular versions are you using? you would have to use `browser.ignoreSynchronization` with google.com – Ram Pasala Aug 17 '16 at 16:53
  • setting `browser.ignoreSynchronization = true;` seemed to fix the problem – user3554664 Aug 17 '16 at 17:24
  • glad it worked! you have to always use that setting for non-angular apps – Ram Pasala Aug 17 '16 at 17:29
  • for angular apps, how do you specify the wait time for the angular synchronization? – user3554664 Aug 17 '16 at 17:31
  • you don't have to wait actually, see that is what protractor does internally when it deals with angular apps. If you still explicitly want to wait then you could use ` browser.waitForAngular()` – Ram Pasala Aug 17 '16 at 17:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121197/discussion-between-user3554664-and-igniteram1). – user3554664 Aug 17 '16 at 17:48