playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question]: 'suggestedFilename' is not working and throwing error as: TypeError: Cannot read properties of undefined (reading 'suggestedFilename')

Open AkkyLohana90 opened this issue 3 years ago • 3 comments
trafficstars

Getting the error as: TypeError: Cannot read properties of undefined (reading 'suggestedFilename'), looks like suggestedFilename is not working for my code(JavaScript) and also the file is getting download with alpha-numeric name. Code: **const fileNamePath = download.suggestedFilename() await download.saveAs(fileNamePath); ** Please suggest how to resolve this.

AkkyLohana90 avatar Aug 09 '22 12:08 AkkyLohana90

download in your case is undefined. Could you share a full reproducible with us?

See here for how to deal with downloads: https://playwright.dev/docs/downloads

mxschmitt avatar Aug 09 '22 13:08 mxschmitt

I think I use in the same way: Please find below details: Code: async downloadFile() { //Download File const [download] = await Promise.all([ this.page.waitForTimeout(20000), this.page.waitForEvent("download"), ]) const fileNamePath = download.suggestedFilename() // de-structuring await download.saveAs(fileNamePath); }

AkkyLohana90 avatar Aug 09 '22 14:08 AkkyLohana90

It looks like there's a few errors in your snippet.

  1. Order matters when unpacking arrays:

    const [a] = await Promise.all([Promise.resolve(1), Promise.resolve(2)]);
    

    a === 1. So in your Promise.all you'll want page.waitForEvent("download") as the first item in your array. Your download variable that you are calling suggestFilename is not actual a download event because the order is incorrect.

    If I wanted the second item, you can do const [_, b] = …

  2. Event Trigger: When using waitForEvent you'll want to start waiting for it before you take an action that will trigger the event (like a click). Also, page.waitForTimeout(20000) won't help here since it doesn't trigger a download event.

Here's a working example from the docs:

const [ download ] = await Promise.all([
  // Start waiting for the download
  page.waitForEvent('download'),
  // Perform the action that initiates download
  page.locator('button#delayed-download').click(),
]);
// Wait for the download process to complete
console.log(await download.path());
// Save downloaded file somewhere
await download.saveAs('/path/to/save/download/at.txt');

rwoll avatar Aug 09 '22 19:08 rwoll

Thanks rwoll, Updated the code as per the above suggestions & it is working now. Closing the Question.

AkkyLohana90 avatar Aug 10 '22 06:08 AkkyLohana90