Playwright: [Question] download file deleted while copying

Created on 10 Jun 2020  路  9Comments  路  Source: microsoft/playwright

Downloaded files are getting deleted after test execution. How to solve this?

And also how to download file with actual file name?

v1.3

Most helpful comment

The problem with the copy is that you don't await for it. So you start copying and while you do it, Playwright cleans up the context and deletes all the files. I'll add a convenience download.saveAs(path) method to help with the overall issue, but for now, all you need is to await your copy.

All 9 comments

Downloaded files are getting deleted after test execution. How to solve this?

Either copy the file from the download.path() location or use readableStream() to handle storing of the file yourself.

And also how to download file with actual file name?

You can use the suggestedFilename() function to get the original file name from the server.

   const [download] = await Promise.all([
                page.waitForEvent('download'), // wait for download to start
                page.click(down)
            ]);
            const path = await download.path();
            console.log(path);
            const fs = require('fs');
            // destination will be created or overwritten by default.
            fs.copyFile(path, './', (err) => {
                if (err) throw err;
                console.log('File was copied to destination');
            });

I have tried already @mxschmitt. I am getting error that downloaded file does not exist.

image

I am using download.path(); to get the file location

Did you set the acceptDownloas property to true?
https://playwright.dev/#version=v1.1.0&path=docs%2Fapi.md&q=browsernewcontextoptions--options-acceptdownloads

Yes, I am able to see that file is downloading

await browser.newContext({ acceptDownloads: true });

Hi Team,

I have also problem with copy:

d:\dasProjekt\node-js\diverses\slack.js:31
      if (err) throw err;
               ^

[Error: EPERM: operation not permitted, copyfile 'D:\Users\apwol\AppData\Local\Temp\playwright_downloads-tqMuwy\c06bb50a-4329-4b64-b076-128d2ca0cb8b' -> 'd:\dasProjekt\node-js'] {
  errno: -4048,
  code: 'EPERM',
  syscall: 'copyfile',
  path: 'D:\\Users\\apwol\\AppData\\Local\\Temp\\playwright_downloads-tqMuwy\\c06bb50a-4329-4b64-b076-128d2ca0cb8b',
  dest: 'd:\\dasProjekt\\node-js'
}

This is my code:

const { chromium } = require("playwright");
const fs = require("fs");

(async () => {
  const browser = await chromium.launch({ headless: false });
  const crContext = await browser.newContext({ acceptDownloads: true });
  const page = await crContext.newPage();
  await page.goto(
    "https://file-examples.com/index.php/sample-documents-download/sample-xls-download/"
  );

  const handles = await page.$$("#table-files .file-link > a.btn");
  const downloads = [];

  page.on("download", async (download) => {
    downloads.push(
      download.path()
      //download.suggestedFilename()
      //download.createReadStream()
    );
  });

  for (const handle of handles) await handle.click();

  const downloadpaths = await Promise.all(downloads);

  console.log(downloadpaths.length);

  for (num in downloadpaths) {
    fs.copyFile(downloadpaths[num], "./", (err) => {
      if (err) throw err;
      console.log("File was copied to destination");
    });
  }

  await page.close();
  await browser.close();
})();

how to do copy and rename in suggestedFilename?

I have downloads array.

[
  "D:\\Users\\apwol\\AppData\\Local\\Temp\\playwright_downloads-WufqI2\\ed1032a2-35d6-43ee-b584-c28bc2e74f63",
  "file_example_XLS_10.xls",
  "D:\\Users\\apwol\\AppData\\Local\\Temp\\playwright_downloads-WufqI2\\e0b52221-3a99-4417-9101-76203dd3a4b7",
  "file_example_XLS_100.xls",
];

how to put download.path(), download.suggestedFilename(), download.createReadStream() into object and then in Array. It is more easy to loop array

Like this:

[
  {
    path:
      "D:\\Users\\apwol\\AppData\\Local\\Temp\\playwright_downloads-WufqI2\\ed1032a2-35d6-43ee-b584-c28bc2e74f63",
    name: "file_example_XLS_10.xls",
  },
  {
    path:
      "D:\\Users\\apwol\\AppData\\Local\\Temp\\playwright_downloads-WufqI2\\e0b52221-3a99-4417-9101-76203dd3a4b7",
    name: "file_example_XLS_100.xls",
  },
];

tnx a lot

The problem with the copy is that you don't await for it. So you start copying and while you do it, Playwright cleans up the context and deletes all the files. I'll add a convenience download.saveAs(path) method to help with the overall issue, but for now, all you need is to await your copy.

@Bharath-Kumar-S : your snippet can be updated with:

await new Promise(f => fs.copyFile(path, './', f));

@osmenia : your snippet uses async event handler, but Node does not await for async event handlers, so it should do the same as the first snippet. It is mentioned in the docs, but it tricky, so I'll add a saveAs method right away.

EPERM is permission related error? Like Something is locking or browser is downloading the file?

@pavelfeldman If you haven't already started working on this, I'd be happy to add a download.saveAs(path) implementation and tests this week. Please let me know either way. Thanks!


EDIT: I had some free time and apparently really enjoy creating tests, so I put together #2872.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

juliomatcom picture juliomatcom  路  3Comments

TrySound picture TrySound  路  4Comments

etnbrd picture etnbrd  路  4Comments

shirshak55 picture shirshak55  路  3Comments

saltyshiomix picture saltyshiomix  路  3Comments