在我的 Fixture 中,我有一个将开始使用浏览器下载文件的操作。在服务器直接响应之前,这工作得很好。
.expect(fs.existsSync(downloadsFolder()+'/export.xml')).ok()
但是现在我有一些服务器需要创建的文件,所以会出现等待时间。我尝试扩展功能:
.expect(fs.existsSync(downloadsFolder()+'/export.xml')).ok('War wohl nicht erfolgreich',{ timeout: 300000 })
但结果与第一次试用相同。我做了一些研究,发现:
async function CheckFileExistsWithTimeDelay(t, timeDelay, fileNameAndPath) {
for (var i = 0; i < timeDelay; i++) {
console.log('Waited for a total of ' + i.toString() + ' microseconds');
await t.wait(1);
if (fs.existsSync(fileNameAndPath)) {
// break;
return;
}
}
};
这也行不通。我认为监视文件功能会阻止 Fs,因此浏览器无法下载文件(写入文件)。此外,这不起作用:
async function waitForFile (path) {
for (let i = 0; i < 300; i++) {
if (fs.existsSync(path))
return true;
await t.wait(1000);
}
return fs.existsSync(path);
}
await t.expect(await waitForFile(downloadsFolder()+'/export.xml')).ok("War wohl nicht erfolgreich", { timeout: 300000 });
看起来文件下载会失败,直到 testcafe 等待一段时间。如果直接下载文件,一切都很好。
有什么好的例子可以在不阻塞文件系统的情况下等待下载文件吗?
这是请求,所以我在这里添加它:testcafe 将获取命令下载的行)
await t
.click(Selector('button').withText('Aktionen'))
.click(Selector('a').withText('Xml Exportieren'));
如我所写。立即下载完美。直到下载被延迟,它才会失败。看起来有些 Hook 卡在文件上,因此 chrome 无法下载。
新信息 1 单击下载链接后,服务器将准确响应 38.7 秒。在使用 testcafe 执行此操作后,我将进入浏览器窗口
如果我使用真实案例,则意味着我将在 38 秒后点击物理网站上的链接下载文件。没有错误。
新信息 2 我还尝试将 long .wait(150000) 添加到夹具中,然后检查文件是否存在。浏览器在 .wait(150000) 循环中等待时尝试在后台下载文件。这也是失败的。
所以我认为这证明这是一个 Testcafe 问题而不是节点问题。
最佳答案
这是另一个如何在测试中等待下载文件的例子:
import { Selector } from 'testcafe';
import fs from 'fs';
const waitForFileDownloaded = (filePath, timeDelay) => new Promise(resolve => {
let i = 1;
const intervalId = setInterval(() => {
if (fs.existsSync(filePath)) {
clearInterval(intervalId);
resolve(true);
}
i++;
if (i > timeDelay) {
clearInterval(intervalId);
resolve(false);
}
}, 1000);
});
fixture `Downloading`
.page `https://github.com/DevExpress/testcafe`;
test('Test', async t => {
await t
.click(Selector('a').withText('218 releases'))
.click(Selector('a').withText('Source code'))
.expect(await waitForFileDownloaded('c:\\Users\\username\\Downloads\\testcafe-1.6.0.zip', 60)).ok();
});
另外,您能否说明下载过程是否在 TestCafe 单击链接后开始(例如,仅使用超时时间较长的 await t.wait()
)?
关于testing - 文件下载超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58541282/