Skip to content

Commit 07b9ce4

Browse files
DavertMikclaude
andcommitted
fix: keep existing download files and Puppeteer downloadBehavior
Only remove files the download tracker saved itself instead of emptying output/downloads, so files written by handleDownloads or users and files of other workers are left alone. Respect a downloadBehavior configured in Puppeteer chrome options. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e2b9ab2 commit 07b9ce4

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎lib/helper/Playwright.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ class Playwright extends Helper {
360360
this.downloads.add(name)
361361
this.debugSection('Download', name)
362362
if (!store.outputDir) return
363-
download.saveAs(this.downloads.pathFor(name)).catch(err => this.debug(`Download ${name} was not saved: ${err.message}`))
363+
const file = this.downloads.pathFor(name)
364+
download
365+
.saveAs(file)
366+
.then(() => this.downloads.trackSaved(file))
367+
.catch(err => this.debug(`Download ${name} was not saved: ${err.message}`))
364368
}
365369

366370
// for network stuff
@@ -2099,7 +2103,7 @@ class Playwright extends Helper {
20992103
/**
21002104
* {{> seeFileDownloaded }}
21012105
*
2102-
* Downloaded files are saved into `output/downloads`, which is emptied before each test.
2106+
* Downloaded files are saved into `output/downloads` and removed before the next test.
21032107
*/
21042108
async seeFileDownloaded(fileName, sec = null) {
21052109
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout

‎lib/helper/Puppeteer.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,13 @@ class Puppeteer extends Helper {
382382
}
383383

384384
async _allowDownloads(browserContextId) {
385-
await this._downloadSession.send('Browser.setDownloadBehavior', { behavior: 'allow', downloadPath: this.downloads.dir, eventsEnabled: true, browserContextId })
385+
const params = { behavior: 'allow', downloadPath: this.downloads.dir, eventsEnabled: true, browserContextId }
386+
const userBehavior = this.puppeteerOptions.downloadBehavior
387+
if (userBehavior) {
388+
params.behavior = userBehavior.policy
389+
params.downloadPath = userBehavior.downloadPath
390+
}
391+
await this._downloadSession.send('Browser.setDownloadBehavior', params)
386392
}
387393

388394
async _after() {
@@ -686,6 +692,7 @@ class Puppeteer extends Helper {
686692
this._downloadSession.on('Browser.downloadWillBegin', event => {
687693
this.downloads.add(event.suggestedFilename)
688694
this.debugSection('Download', event.suggestedFilename)
695+
if (!this.puppeteerOptions.downloadBehavior) this.downloads.trackSaved(this.downloads.pathFor(event.suggestedFilename))
689696
})
690697
}
691698

@@ -1370,7 +1377,8 @@ class Puppeteer extends Helper {
13701377
/**
13711378
* {{> seeFileDownloaded }}
13721379
*
1373-
* Downloaded files are saved into `output/downloads`, which is emptied before each test.
1380+
* Downloaded files are saved into `output/downloads` and removed before the next test.
1381+
* A `downloadBehavior` set in `chrome` options is respected.
13741382
*/
13751383
async seeFileDownloaded(fileName, sec = null) {
13761384
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout

‎lib/helper/extras/Download.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import store from '../../store.js'
66
class Downloads {
77
constructor() {
88
this.names = []
9+
this.saved = []
910
}
1011

1112
get dir() {
@@ -18,11 +19,16 @@ class Downloads {
1819

1920
reset() {
2021
this.names = []
22+
for (const file of this.saved) fs.rmSync(file, { force: true })
23+
this.saved = []
2124
if (!store.outputDir) return
22-
fs.rmSync(this.dir, { recursive: true, force: true })
2325
fs.mkdirSync(this.dir, { recursive: true })
2426
}
2527

28+
trackSaved(file) {
29+
this.saved.push(file)
30+
}
31+
2632
add(name) {
2733
this.names.push(name)
2834
}

0 commit comments

Comments
 (0)