Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanrosello-og/f9700c548a405367d6642ed09d4e7b16 to your computer and use it in GitHub Desktop.
Save ryanrosello-og/f9700c548a405367d6642ed09d4e7b16 to your computer and use it in GitHub Desktop.
Use playwright to download files from remote servers
import { test, expect } from '@playwright/test';
import * as fs from 'fs/promises'
test.describe('use Playwright to easliy download a file from remote server', () => {
test('able to download a remote file to disk', async ({ page }, testInfo) => {
const baseUrl = 'https://www.tesla.com/'
const resource = 'ns_videos/2021-tesla-impact-report.pdf'
const getResp = await page.context().request.get(`${baseUrl}${resource}`)
expect(getResp.ok()).toBe(true)
let responseBuffer = await getResp.body()
const fileName = `${new Date().valueOf()}_downloaded_pdf_from_tesla.pdf`
const downloadPath = testInfo.outputPath(fileName)
await fs.writeFile(downloadPath, responseBuffer, 'binary')
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment