升级到nodejs版本3.0.0的selenium-webdriver后发生promise.defer()错误

时间:2016-11-05 10:41:04

标签: node.js selenium-webdriver

从selenium-webdriver 2.53.2升级到3.0.0后,以下示例单元测试不再按预期工作:

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

并给我这个错误堆栈:

  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)

我目前正在使用webdriver.promise来创建自定义承诺,它似乎不再按预期工作。

1 个答案:

答案 0 :(得分:1)

Deferred返回的webdriver.promise.defer()对象的API已更改。

使用const waitPromise = driver.wait(deferred.promise, TIMEOUT, 'waiting for a promise to succeed')代替const waitPromise = driver.wait(deferred, TIMEOUT, 'waiting for a promise to succeed')可以解决问题。