Created
July 8, 2022 08:39
-
-
Save ryanrosello-og/f9700c548a405367d6642ed09d4e7b16 to your computer and use it in GitHub Desktop.
Use playwright to download files from remote servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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