microsoft / microsoft/playwright
[Feature]: Rename file path in download history
- Dominant language
- TypeScript
- Stars
- 96.3k
- Forks
- 6.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 180
Description
### 🚀 Feature Request
Ability to influence which entries are included in the download history upon a download event.
Assuming a `Download` instance is obtained through `page.on('download', download => { ... })` or `page.waitForEvent('download')`, the following methods would be very helpful to impact download & history behavior:
* `download.renameTo(path, isPersistent=true)` in addition to `saveAs`. The difference would be that `renameTo` also updates the download history. If `isPersistent`, it would not mark the file for deletion upon destruction of the context (true for cases where you want the download to be persistent, false where you may just want to add a file extension to allow for direct opening, e.g. `await download.path() + '.xlsx'`)
* `download.saveAs(path, isAddToHistory=false)`: additional parameter to steer history behavior
* `download.removeFromHistory()`: remove the file from download history without removing the file itself
* `download.delete(isRemoveFromHistory)`: additional parameter to steer history behavior (I'm not sure what the current behavior is for deletion)
I'd be happy to create a PR; hints where the relevant code lives are appreciated though ;-)
### Example
```typescript
page.on('download', download => {
// Rename and update history
download.renameTo(path.resolve(downloadLocation, download.suggestedFileName()))
})
```
Alternatively:
```typescript
page.on('download', download => {
// Save and add to history, then remove temporary file and its history entry
await download.saveAs(path.resolve(downloadLocation, download.suggestedFileName()), true)
await download.delete(true)
})
```
### Motivation
When downloading a file, Playwright uses a new UUID as the download file name, which makes a lot of sense to ensure uniqueness. If saving the file under the "true" file name is desired, something like `download.saveAs(path.resolve(downloadLocation, download.suggestedFilename()))` can be used to save a copy of the download file.
This however leaves the UUID file name in the download history, which typically points to a temporary folder and doesn't contain any file extension, so that opening a file from the download history is not possible.
In my scenario, Playwright is used to augment existing web-based applications with automation running locally (a bit like attended robotic process automation) and therefore the browser is partially controlled by Playwright and partially by a human user. Therefore it makes a big difference whether the download history contains a UUID or a clickable link that will actually open correctly.
Contributor guide
Research direction
Start from the Download entry points mentioned in page.on('download', ...) and page.waitForEvent('download'), then trace the existing saveAs, path, and deletion behavior. Compare the requested renameTo, saveAs history flag, removeFromHistory, and delete options, with done defined as consistent file persistence and download-history behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100