playwright
playwright copied to clipboard
[Question]: 'suggestedFilename' is not working and throwing error as: TypeError: Cannot read properties of undefined (reading 'suggestedFilename')
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.
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
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); }
It looks like there's a few errors in your snippet.
-
Order matters when unpacking arrays:
const [a] = await Promise.all([Promise.resolve(1), Promise.resolve(2)]);a === 1. So in yourPromise.allyou'll wantpage.waitForEvent("download")as the first item in your array. Yourdownloadvariable that you are callingsuggestFilenameis not actual a download event because the order is incorrect.If I wanted the second item, you can do
const [_, b] = … -
Event Trigger: When using
waitForEventyou'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');
Thanks rwoll, Updated the code as per the above suggestions & it is working now. Closing the Question.