protractor icon indicating copy to clipboard operation
protractor copied to clipboard

ControlFlow usage before describe does not work in Jasmine (works in Jasmine2)

Open tullmann opened this issue 11 years ago • 5 comments

If I use an explicit controlFlow().execute to do some one-time setup before my tests, the implicit control flow created by a describe won't "wait" for it.

If I use the "jasmine2" framework, instead of the default "jasmine" framework, the spec behaves as I expect it to.

var describeWaited = false;

// Pre-describe setup.  Simulate by returning a promise that waits half a
// second.
protractor.promise.controlFlow().execute(function() {
  return protractor.promise.delayed(500).then(function() {
    describeWaited = true;
  });
}, 'Doing some setup');

// Actual test.  Implicit Control Flow in describe should "wait" for
// setup.
describe('Actual set of tests', function() {
  it('actual test', function() {
    expect(describeWaited).toBe(true);
  });
});

I'm running protractor from master, on Linux, with Chrome.

tullmann avatar Mar 27 '15 17:03 tullmann

Not sure how to fix yet, but the issue is that in Jasmine 1.3, the task on the webdriver control flow which controls the it block is getting scheduled as a child of the setup one, so it's not waiting for it to complete.

juliemr avatar Mar 30 '15 22:03 juliemr

OK, so this boils down to Jasmine 1.3's old runs/waitsFor behavior for doing asynchronous tests. Essentially, it causes a timeout in the JavaScript event loop, which messes up webdriver's promise queue. Here's a minimal example:

var webdriver = require('selenium-webdriver');

var waited;

webdriver.promise.controlFlow().execute(function() {
  return webdriver.promise.delayed(500).then(function() {
    console.log('waited');
    waited = true;
  });
}, 'Doing some setup');

// This is what Jasmine2 does - it blocks are called immediately, so they schedule right away
webdriver.promise.controlFlow().execute(function() {
  if (waited !== true) {
    console.log('A: FAILURE');
  } else {
    console.log('A: SUCCESS');
  }
});

// This is what Jasmine1.3 does - there are delays due to runs/waitsFor
setTimeout(function() {
  webdriver.promise.controlFlow().execute(function() {
    if (waited !== true) {
      console.log('B: FAILURE');
    } else {
      console.log('B: SUCCESS');
    }
  });
}, 50);

Output:

B: FAILURE
A: SUCCESS

I'm not sure what, if anything, we can do about this in the jasminewd adapter. One of the main advantages of Jasmine2 is that it gets rid of weird hacks like this.

Unless this is significantly blocking anyone, I'm going to close this issue - I think we can better spend time improving Protractor with the new framework than supporting corner cases in old Jasmine. There are many alternatives if an upgrade to Jasmine2 is not feasible (use onPrepare or beforeEach or a prior it block to do the setup).

@tullmann thanks for bringing this up and especially for providing an easy way to reproduce. Please do let me know if this is a higher priority problem that I'm rating it.

juliemr avatar Mar 30 '15 23:03 juliemr

Yeah closing this is fine, I think I've accumulated enough reasons to switch to Jasmine2 now. :)

I'm not sure I entirely follow, but it seems like the behavior in the "Jasmine1.3" case in your example is just a bug in webdriver's controlFlow? I'm not following how running controlFlow functions in a setTimeout handler is allowed to make a mess of the controlFlow. That just seems broken. If its expected behavior, then at least some harder error than making a mess seems worthwhile. Should I try taking this up with webdriverjs?

tullmann avatar Mar 30 '15 23:03 tullmann

The logic of the control flow, as I see it, is 'if a task is scheduled while another task is running, it is a child of the currently running task'. So, with the timeout, the setup gets a chance to become the active task.

It's not super intuitive, but I don't know if the alternative is better.

EDIT: but yes if you have a proposal that would make it cleaner, by all means lets bring it up with WebDriverJS and cc me.

juliemr avatar Mar 30 '15 23:03 juliemr

Ah, thanks, for the explanation! I think see how the delayed execute looks "like a child" of the in-progress one. That's pretty subtle.

Hrm ... I don't see any good way to differentiate this case from any flows created directly by the current one.

tullmann avatar Mar 30 '15 23:03 tullmann