materialdreams materialdreams - 29 days ago 8
Node.js Question

promise.defer() error after upgrading to selenium-webdriver for nodejs version 3.0.0

After upgrading from the selenium-webdriver 2.53.2 to 3.0.0 the following example unit test does no longer work as expected:

'use strict';

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

test.describe('Selenium Webdriver NodeJS API (selenium_webdriver-test.js)', function () {
test.it('Wait success using a custom promise', function () {
let finished = false;

function asyncOperation(delay) {
let deferred = webdriver.promise.defer();
setTimeout(function () {
finished = true;
deferred.fulfill(true);
}, delay || 100);

return deferred;
}

let deferred = asyncOperation(100);

let waitPromise = driver.wait(deferred, TIMEOUT, 'waiting for a promise to succeed');

waitPromise.then(function () {
assert.strictEqual(finished, true);
}).catch(function (e) {
throw new Error(e);
});
});
});


and give me this error stack:

1) Selenium Webdriver NodeJS API (selenium_webdriver-test.js) Wait success using a custom promise:
Error: TypeError: fn is not a function
at test\selenium\tests\0-helper\selenium_webdriver-test.js:189:10
at ManagedPromise.invokeCallback_ (node_modules\selenium-webdriver\lib\promise.js:1341:14)
at TaskQueue.execute_ (node_modules\selenium-webdriver\lib\promise.js:2950:14)
at TaskQueue.executeNext_ (node_modules\selenium-webdriver\lib\promise.js:2933:27)
at asyncRun (node_modules\selenium-webdriver\lib\promise.js:2793:27)
at node_modules\selenium-webdriver\lib\promise.js:675:7
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
From: Task: Selenium Webdriver NodeJS API (selenium_webdriver-test.js) Wait success using a custom promise
at Context.ret (node_modules\selenium-webdriver\testing\index.js:185:10)
at node_modules\selenium-webdriver\testing\index.js:104:5
at ManagedPromise.invokeCallback_ (node_modules\selenium-webdriver\lib\promise.js:1341:14)
at TaskQueue.execute_ (node_modules\selenium-webdriver\lib\promise.js:2950:14)
at TaskQueue.executeNext_ (node_modules\selenium-webdriver\lib\promise.js:2933:27)
at asyncRun (node_modules\selenium-webdriver\lib\promise.js:2793:27)


I'm currently using webdriver.promise all over the place to create custom promises and it does no longer seem to work as expected.

Answer

The API of the Deferred object returned by webdriver.promise.defer() has changed.

Using const waitPromise = driver.wait(deferred.promise, TIMEOUT, 'waiting for a promise to succeed') instead of const waitPromise = driver.wait(deferred, TIMEOUT, 'waiting for a promise to succeed') solves the problem.