我想从服务器保存一个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)
能否请您澄清一下您的系统环境详细信息?如果您能像上面那样提供您自己的简单项目,那就太好了。这将帮助我们重现问题。
关于testing - 使用夹具下载 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413354/