我想将XML文件从服务器保存到本地,以便以后检查。因为TestCafe不允许开箱即用的文件下载,所以我做了一些研究,发现:
import fs from 'fs';
const downloadLocation = './downloads/saved.xml'; //downloadlocation on macOS
const fileDLUrlBase = 'https://example.com/downloads/xml/mytest'; //dynamic generated xml
fixture('download test fixture');
test('download test', async t => {
await t.navigateTo(fileDLUrlBase);
await t.wait(30000);
// Wait 30 seconds
await t.expect(fs.fileExistsSync(downloadLocation));
});
我已经在堆栈上阅读了很多评论和文章,但是我真的很困惑。 ALL,实际上是ALL解决方案,标记为s的解决方案,在这里不起作用。
作为示例:Testcafe example to assert file download
我克隆了这个灯具,但是TestCafe将崩溃。但是这个问题被标记为已解决。在我眼里,没有关于下载文件的解决方案在起作用,这使我感到困惑。
有人可以帮我吗?
我检查了一个简单的场景,它可以正常工作,没有任何错误。
server.js
var express = require('express');
var fs = require('fs');
var app = express();
app.get('/', function (req, res) {
res.send('<html>\n' +
'<head>\n' +
'</head>\n' +
'<body>\n' +
'<a href="/download/">Download file</a>\n' +
'</body>\n' +
'</html>');
});
app.get('/download/', (req, res) => {
var files = fs.createReadStream("text-document.txt");
res.writeHead(200, {'Content-disposition': 'attachment; filename=text-document.txt'});
files.pipe(res)
})
app.listen(3000, function () {
console.log('http://localhost:3000/');
});
test.js
import { Selector } from 'testcafe';
import fs from 'fs';
fixture `fixture`;
test
.page('http://localhost:3000/')
('download', async t => {
await t.click(Selector('body > a'));
await t.wait(1000);
await t.expect(fs.existsSync('path-to-file\\text-document.txt')).ok();
});
命令:
testcafe chrome test.js
结果:
fixture
√ download
1 passed (2s)
您能否阐明您的系统环境详细信息?如果您可以提供自己的简单项目(如上述项目),那就太好了。这将帮助我们重现该问题。