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); + }); + }); });