Luigi Canoro
Luigi Canoro

Reputation: 31

Downloading PDF file on my test for further upload

In my test, I need to click on a "Download Button" which triggers an HTTP request that automatically downloads the PDF file to my Download directory, and later on, I need to upload the same PDF file somewhere else in my test.

So here is the problem, since I don't know the download directory of the computer which will actually run the test I need to control the path to which the download saves the file, how can I do that with testcafe?

Here are some attempts to use HTTP Request Hook and later on save the file, but when trying to open the PDF file the data is corrupted

 .click(await new Selector('[class="fi font-download_alt "]'))
    const requests = logger.requests.length;

    const buf = logger.requests[requests-1].response.body;


   console.log(decoder.write(cent));
    console.log(buf.toJSON());
   console.log(buf.toString()); 

   let writer = fs.createWriteStream('test_copy.txt', {
    flags: 'w'
});
      buf.pipe(writer);

      var util = require('util');

      fs.writeFile('myFile.pdf', util.inspect(logger.requests[requests-1]), (err) => {
        if(!err) console.log('Data written');
      }); 

Actual result: Downloading to download directory or Managing to save the file to my desired directory but the file gets corrupted using FS or FileStream

Desired result: Downloading the file to my desired directory and the PDF doesn't get corrupted

Upvotes: 3

Views: 770

Answers (1)

Dmitry Ostashev
Dmitry Ostashev

Reputation: 2348

Take a look at the downloads-folder package, it helps to detect the local Downloads folder.

Upvotes: 3

Related Questions