1

I want to save an XML file from the server to local for later check. Because TestCafe does not allow out of the box the download of files I did some research and found:

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));
});

I have read many comments and posts here on stack overflow but I am really confused. ALL, really ALL solution, marked s solution, do not work here.

As sample: Testcafe example to assert file download

I clone this fixture but TestCafe will crash. But this question is marked as solved. In my eyes, NO solution to download a file is working and this confuses me.

Can anybody help me out?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
ingo
  • 776
  • 1
  • 10
  • 25

1 Answers1

0

I checked a simple scenario, and it works without any errors on my side.

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();

});

Command:

testcafe chrome test.js

Result:

 fixture
 √ download


 1 passed (2s)

Could you please clarify your system environment details? It will be great if you can provide your own simple project like the above one. It will help us reproduce the issue.

Vladimir A.
  • 2,202
  • 2
  • 10
  • 28
  • 1
    You can inspect how to test file download in TestCafe using [this](https://github.com/DevExpress/testcafe-examples/tree/master/examples/check-downloaded-file-name-and-content) runnable example. – mlosev Oct 14 '20 at 08:11