gpt4 book ai didi

javascript - 测试未遵守 "beforeEach"内的超时

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:27 25 4
gpt4 key购买 nike

我知道为了测试 API 端点,应该模拟 xhtml 请求,但现在我想用真正的 API 来测试它。

我想做的事情:

打开一个页面,单击“连接”按钮,然后等待最多 10 秒,以便某个元素的 innertext 更改为“已连接”。

这是我的简单测试代码:

    const assert = require('assert');
const webdriver = require('selenium-webdriver');
const By = webdriver.By;
const until = webdriver.until;
const chrome = require('selenium-webdriver/chrome');
const test = require('selenium-webdriver/testing');
const mochaTimeOut = 25000;


test.beforeEach(function() {
this.timeout(mochaTimeOut);
driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
driver.get('http://127.0.0.1/dist/popup.html');
});

test.afterEach(function() {
driver.quit();
});

test.describe('Connect functionality', function() {
test.it('Try to connect', function(done) {
// this.timeout(22222); // only works if I uncomment this line
driver.findElement(By.id('connect')).click().then(function (el) {
driver.findElement(By.id('state')).then(function (stateEl) {
driver.wait(until.elementTextContains(stateEl, 'Connected'), 10000);
})
}).then(done);
});
});

如果我按原样运行此测试,则会收到此错误:

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

但是如果我取消注释这一行,它就会起作用:

// this.timeout(22222);

我不太喜欢使用超时,但我没有看到其他解决办法。

最佳答案

timeout的调用是分层的。问题是您的 beforeEach 没有子项。您需要全局设置超时,或者您可以在 test.describe 调用中复制它:

test.describe('Connect functionality', function() {
this.timeout(mochaTimeout);

这样它将应用于 test.describe block 内的所有测试。

关于javascript - 测试未遵守 "beforeEach"内的超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41825411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com