In this article, we will look in detail at how we can download files in Playwright.

1. Single File Download:

– Here we are using Promise.all to prevent a race condition between clicking and waiting for the download.
– Then we are calling waitForEvent before click to set up waiting.
– Then using page.locator(‘text=evening.png’).click() we are triggering the download.
download.suggestedFilename() returns suggested filename for this download by the browser.
download.saveAs(filePath) copies the download to a user-specified path.
fs.existsSync(filePath) checks if a file exists in the filesystem using Node.js. In case the file is not present in the file path, it will return a false and the assertion will fail. In order to include the fs module in the current file we have to write const fs = require(‘fs’); at the top of the test suite file.

1
2
3
4
5
6
7
8
9
10
11
test('Download a Single file and assert', async ({page}) => {
  await page.goto('https://the-internet.herokuapp.com/download')
  const [download] = await Promise.all([
    page.waitForEvent('download'),
    page.locator('text=evening.png').click(),
  ])
  const suggestedFileName = download.suggestedFilename()
  const filePath = 'download/' + suggestedFileName
  await download.saveAs(filePath)
  expect(fs.existsSync(filePath)).toBeTruthy()
})

 
2. Multiple File Download:

const fileNames = [“evening.png”, “morning.jpg”] creates an array of file names to be downloaded.
– Using for (const fileName of fileNames) we are going through each file name, downloading it and then asserting it.
– With page.locator(`text=${fileName}`).click() we are using template literals to replace the file name for each loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
test('Download Multiple files and assert', async ({page}) => {
  await page.goto('https://the-internet.herokuapp.com/download')
  const fileNames = ['evening.png', 'morning.jpg']
  for (const fileName of fileNames) {
    const [download] = await Promise.all([
      page.waitForEvent('download'),
      page.locator(`text=${fileName}`).click(),
    ])
    const suggestedFileName = download.suggestedFilename()
    const filePath = 'download/' + suggestedFileName
    await download.saveAs(filePath)
    expect(fs.existsSync(filePath)).toBeTruthy()
  }
})

 
Finally, upon executing all tests and we should get a pass.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { test, expect } from '@playwright/test';
const fs = require('fs')

test.describe('Example to demonstrate File Download in Playwright', () => {

    test('Download a Single file and assert', async ({ page }) => {
        await page.goto('https://the-internet.herokuapp.com/download')
        const [download] = await Promise.all([
            page.waitForEvent('download'),
            page.locator('text=evening.png').click()
        ]);
        const suggestedFileName = download.suggestedFilename()
        const filePath = 'download/' + suggestedFileName
        await download.saveAs(filePath)
        expect(fs.existsSync(filePath)).toBeTruthy()
    })

    test('Download Multiple files and assert', async ({ page }) => {
        await page.goto('https://the-internet.herokuapp.com/download')
        const fileNames = ["evening.png", "morning.jpg"]
        for (const fileName of fileNames) {
            const [download] = await Promise.all([
                page.waitForEvent('download'),
                page.locator(`text=${fileName}`).click()
            ]);
            const suggestedFileName = download.suggestedFilename()
            const filePath = 'download/' + suggestedFileName
            await download.saveAs(filePath)
            expect(fs.existsSync(filePath)).toBeTruthy()
        }
    })
})

playwright download file test script

playwright file download html report

Do check out 🙂

Github: https://github.com/alapanme/Playwright-Automation