断言文件下载的 Testcafe 示例

GM.*_*GM. 3 javascript testing automated-tests e2e-testing testcafe

我想编写一个夹具来模拟导出文件并确保从浏览器操作中下载文件。任何例子?

不适用

Dav*_*son 6

没有一种奇特的方法来检查下载是否完成,TestCafe 在控制浏览器中的下载能力方面有些限制。

import fs from 'fs';

const fileName = 'junk.txt';
const downloadLocation = 'C:\\Wherever\\Downloads\\';
const fileDLUrlBase = 'https://example.com/downloads/';
fixture('download test fixture');
test('download test', async t => {
  await t.navigateTo(fileDLUrlBase + fileName);
  await t.wait(30000);
  // Wait 30 seconds
  await t.expect(fs.fileExistsSync(downloadLocation + fileName));
});
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以将其转换为一个循环,例如每 5 秒检查一次,持续 60 秒。