From 43ff9e5e2a05b4e51d04133122d763ef4ed3f2d1 Mon Sep 17 00:00:00 2001 From: Hank Duan Date: Fri, 18 Apr 2014 17:30:02 -0700 Subject: [PATCH] fix(jasminewd): allow asynchronous callbacks for jasmine tests Closes #728, Closes #704 --- jasminewd/index.js | 21 +++++++++++++++++---- jasminewd/spec/adapterSpec.js | 27 +++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/jasminewd/index.js b/jasminewd/index.js index 7e0b69088..0d2465077 100644 --- a/jasminewd/index.js +++ b/jasminewd/index.js @@ -80,12 +80,25 @@ function wrapInControlFlow(globalFn, fnName) { } desc_ += ')'; + // deferred object for signaling completion of asychronous function within globalFn + var asyncFnDone = webdriver.promise.defer(); + + if (fn.length == 0) { + // function with globalFn not asychronous + asyncFnDone.fulfill(); + } else if (fn.length > 1) { + throw Error('Invalid # arguments (' + fn.length + ') within function "' + fnName +'"'); + } + flow.execute(function() { - fn.call(jasmine.getEnv().currentSpec, function() { - throw new Error('Do not use a done callback with WebDriverJS tests. ' + - 'The tests are patched to be asynchronous and will terminate when ' + - 'the webdriver control flow is empty.'); + fn.call(jasmine.getEnv().currentSpec, function(userError) { + if (userError) { + asyncFnDone.reject(new Error(userError)); + } else { + asyncFnDone.fulfill(); + } }); + return asyncFnDone.promise; }, desc_).then(seal(done), function(e) { e.stack = e.stack + '==== async task ====\n' + driverError.stack; done(e); diff --git a/jasminewd/spec/adapterSpec.js b/jasminewd/spec/adapterSpec.js index 0dc9b8e67..6c6cc1423 100644 --- a/jasminewd/spec/adapterSpec.js +++ b/jasminewd/spec/adapterSpec.js @@ -186,11 +186,34 @@ describe('webdriverJS Jasmine adapter', function() { // expect(fakeDriver.getValueB()).toEqual('b'); // }, 300); - // it('should error with a warning if done callback is used', function(done) { - // done(); + // it('should pass errors from done callback', function(done) { + // done('an error'); // }); it('should pass after the timed out tests', function() { expect(true).toEqual(true); }); + + describe('should work for both synchronous and asynchronous tests', function() { + var x; + + beforeEach(function() { + x = 0; + }); + + afterEach(function() { + expect(x).toBe(1); + }); + + it('should execute a synchronous test', function() { + x = 1; + }); + + it('should execute an asynchronous test', function(done) { + setTimeout(function(){ + x = 1; + done(); + }, 500); + }); + }); });