javascript - 为什么我不断收到错误 : Timeout of 2000ms exceeded from Mocha on async?

标签 javascript selenium testing mocha.js

我正尝试在 Selenium 上开始 javascript 测试,但我卡在了开头。

MochaJS 从不等待测试结束,而是在 2 秒后抛出

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/pavel/code/gscc/test/test.js)

我的代码是

const {Builder, By, Key, until} = require('selenium-webdriver');
let assert = require('chai').assert;

describe('Test Suite', function() {
    it('should do', async function(done) {
        try {
        let driver = new Builder().forBrowser('firefox').build();
        await driver.get('http://www.google.com/ncr');
        const title = await driver.getTitle();
        assert.equal(title, 'Google');
        } catch(err) {
            console.log(err);
        } finally {
            await driver.quit();
        }
        done();
    })
})

Mocha 说我的代码中有 Unresolved promise ,但真的有吗?

最佳答案

好的,在调查之后,我将在解决问题时尝试回答我自己的问题。

首先,错误:超时超过 2000 毫秒。 是一个常见问题,只是超时已超出,但测试没有运行(因为它们需要更多时间才能运行)。它记录在 stackoverflow 上 pretty good .

下一个问题是 Mocha 等待 promise 或 done 函数来完成测试。当我写的

it('should do', async function(done) {
    try {
    let driver = new Builder().forBrowser('firefox').build();
    await driver.get('http://www.google.com/ncr');
    const title = await driver.getTitle();
    assert.equal(title, 'Google');
    } catch(err) {
        console.log(err);
    } finally {
        await driver.quit();
    }
    done();
})

它既没有得到 promise 也没有完成,因为它不适用于异步/等待机制,只能用于标准 promise 。

所以我删除了 done 并且完全删除了 try-catch block ,现在它终于可以工作了!

最终代码为

describe('Test Suite', function() {
    this.timeout(0);

    before(async function() {
        this.driver = await new Builder().forBrowser('firefox').build();
    });

    it('should do', async function() {
        await this.driver.get('http://www.google.com/ncr');
        const title = await this.driver.getTitle();
        assert.equal(title, 'Google1121312213');
    })

    after(async function() {
        this.driver.quit();
    })
});

关于javascript - 为什么我不断收到错误 : Timeout of 2000ms exceeded from Mocha on async?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53258510/

相关文章:

python - Selenium 异常: Element is not clickable at point

javascript - 窗口调整大小脚本有意想不到的副作用

javascript - "' null ' is null or not an object"IE 错误

python - Selenium预期条件-选择具有多个属性的元素

selenium - 关于Selenium Webdriver中隐式等待的问题

iphone - xcode 中的自动应用程序测试

没有数据库的 Django 单元测试

python - 在本地测试 Hive + spark python 程序?

javascript - 如何在 Vue.js Express MongoDB 应用程序中传递 props

php - Javascript 将内容写入 PHP var 以在 IF 语句中使用不起作用