Skip to content

Conversation

@pascalbaljet
Copy link

@pascalbaljet pascalbaljet commented Jan 11, 2026

This PR adds support for capturing and asserting file downloads.

// Single download
visit('/reports')
    ->expectDownload(fn ($page) => $page->click('a[download]'))
    ->assertFilename('report.pdf')
    ->assertSuccessful();

// Multiple downloads
$downloads = visit('/invoices')
    ->expectDownloads(fn ($page) => $page->click('#download-all'), count: 3);

foreach ($downloads as $download) {
    $download->assertSuccessful();
}

The Download object supports fluent chaining:

visit('/export')
    ->expectDownload(fn ($page) => $page->click('#export'))
    ->assertFilename('data.csv')
    ->assertUrlContains('/exports/')
    ->assertContentContains('id,name,email')
    ->assertSuccessful()
    ->saveAs(storage_path('exports/data.csv'));

You can also access download details directly:

$download->suggestedFilename(); // 'report.pdf'
$download->url();               // 'https://example.com/file.pdf'
$download->contents();          // file contents as string
$download->path();              // temp file path

@nunomaduro
Copy link
Member

Does jest or vitest have an API for this?

@pascalbaljet
Copy link
Author

Does jest or vitest have an API for this?

From what I found, neither Jest or Vitest support this out of the box. With Dusk, I used a custom browser instance that configures the download behavior via Chrome DevTools:

$devTools = new \Facebook\WebDriver\Chrome\ChromeDevToolsDriver($this->driver);

$devTools->execute('Browser.setDownloadBehavior', [
	'behavior' => 'allow',
	'downloadPath' => $path,
]);

I mostly followed Playwright's approach: https://playwright.dev/docs/api/class-download

@nunomaduro
Copy link
Member

can you add docs?

@nunomaduro
Copy link
Member

fix tests also.

@pascalbaljet
Copy link
Author

pascalbaljet commented Jan 12, 2026

I'm not sure why it didn't fail in CI on my fork, but I removed the dependency on the Collection class. Docs PR is ready as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants