You can use the saveAs
method from Playwright's Download
class to rename each downloaded file with a unique name ending in '.csv'. Here's how:
First, start waiting for the download event before clicking the download button.
const downloadPromise = page.waitForEvent('download');
await page.getByText('Download file').click();
Once the download event is triggered, get a reference to the Download
object.
const download = await downloadPromise;
Next, generate a unique name for the downloaded file. Let's assume you have a function generateUniqueName()
for this purpose.
const uniqueName = generateUniqueName() + '.csv';
Then, specify the path where the download should be saved.
const savePath = 'https://ray.run/path/to/save/download/' + uniqueName;
Finally, save the downloaded file with the specified path and name.
await download.saveAs(savePath);
And that's it! You've successfully renamed a downloaded file with a unique name ending in '.csv' using Playwright. Remember to always refer to the official Playwright documentation for more detailed information.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].