Skip to content

Instantly share code, notes, and snippets.

@MarcusFelling
Created September 28, 2023 16:24
Show Gist options
  • Save MarcusFelling/897eb39c90ebe7300e5285125093f9ad to your computer and use it in GitHub Desktop.
Save MarcusFelling/897eb39c90ebe7300e5285125093f9ad to your computer and use it in GitHub Desktop.
// Throw Error: Path is not available when connecting remotely. Use saveAs() to save a local copy.
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('download result', async ({ page }) => {
await page.goto('https://demo.playwright.dev/svgomg');
await page.locator('.menu-item >> text=Demo').click();
const downloadButton = page.locator('a[title=Download]');
await expect(downloadButton).toHaveAttribute('href', /blob/);
const [download] = await Promise.all([
page.waitForEvent('download'),
downloadButton.click(),
]);
expect(download.suggestedFilename()).toBe('car-lite.svg');
const result = fs.readFileSync(await download.path(), 'utf-8');
expect(result).toContain('<svg');
});
// Fix using saveAs()
// Docs: https://playwright.dev/docs/downloads
import { test, expect } from '@playwright/test';
test('download result', async ({ page }) => {
await page.goto('https://demo.playwright.dev/svgomg');
await page.locator('.menu-item >> text=Demo').click();
const waitForDownloadTask = page.waitForEvent('download');
const downloadButton = page.locator('a[title=Download]');
await expect(downloadButton).toHaveAttribute('href', /blob/);
await downloadButton.click();
const download = await waitForDownloadTask;
expect(download.suggestedFilename()).toBe('car-lite.svg');
await download.saveAs("/path/to/save/at/" + download.suggestedFilename());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment