问题描述
我正在使用TestCafe和Node.JS编写自动测试脚本.测试之一是下载文件并验证下载是否完成.我不想写硬编码
I'm writing an automated test script using TestCafe and Node.JS.One of the tests is to download a file and validate that the download is complete.I don't want to write a hard coded
await t.wait(4000);
因为我的测试是基于数据驱动的框架,所以我可以使用很多不同的文件和文件类型来提供很多数据.因此,文件大小可能会有所不同,从几千字节到几千兆字节.
because since my test is based on a data driven framework I can feed it lots of data with lots of different files and file types. So the file size could vary by extreme amounts from a few Kilobytes to Gigabytes.
如此写作
await t.wait(4000);
可以在一个测试用例上工作,但几乎肯定会在其他测试用例上失败.
might work for one test case but will almost certainly fail for other test cases.
我正在使用依赖项"downloads-folder"来发现本地系统上的downloads文件夹的路径.从那里,我寻找期望存在的特定文件.它适用于快速下载的小型文本文件和小型zip文件.但是,一旦我尝试下载包含大量数据的非常大的zip文件,它就会失败
I'm using dependency 'downloads-folder' to discover the path to the downloads folder on the local system. From there I look for the specific file that I expect to be there. It works for small text files and small zip files that download fast. But as soon as I try to download a really large zip file with lots of data it fails
if (fs.existsSync(zipFileNameAndPath)) {
await t.expect(true).eql(true); // Report it as a success.
} else {
console.log('file does NOT exist: ' + zipFileNameAndPath);
await t.expect(false).eql(true); // Report it as a failure!
}
所以我的问题是:
有没有办法做类似的事情
Is there a way to do something like
if (fs.existsSync(zipFileNameAndPath){[ timeout: 50000000]}) {...} else {...}
结果超时将作为动态超时工作,等待查看何时找到文件,如果在超时到期之前找到文件,则它将返回true,只有在超时时间过去之后才返回false ?
Where the resulting timeout would work as a dynamic timeout, waiting to see when the file is found, and if it is found before the timeout expired then it would return true, and only after the timeout period has passed would it return false?
我正在寻找一个同步的答案,该答案对Lodash或JQuery之类的东西没有任何依赖性,并且可以在ES6中正常工作.
I'm looking for a Synchronous answer that doesn't have any dependencies on things like Lodash or JQuery and works fine in ES6.
推荐答案
我写了一个函数:
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;
}
}
};
然后该函数的调用者如下:
Then the caller of that function looks like:
await Common.CheckFileExistsWithTimeDelay(t, 500000, zipFileNameAndPath);
当然,您可以注释掉console.log(...),我只是将其留在了那里,因此我可以在等待调试时观看它.
Of course you could comment out the console.log(...), I just left it in there so I could watch it as it waited for debugging purposes.
当我运行它时,它在下载59mb的zip文件之前等待了7996微秒,然后才中断并成功地继续执行脚本.
When I ran it, it waited for 7996 microseconds while downloading a 59mb zip file before breaking out and continuing script execution successfully.
很容易被打破;或返回;取决于您是要保留其功能还是直接将其与脚本代码内联使用.
Could just as easily be Break; or Return; depending if you wanted to keep it as a function or use it directly in-line with your script code.
这篇关于如何使用超时做fs.existsSync(path)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!