Websites might expose files for users to download and then access from their local machine. Common cases are downloading tickets, receipts and itineraries.
This example runs against our test webshop and proceeds to download a receipt for a previous purchase. It includes the following steps:
Logging in to the website
Navigating to the account page
Downloading a linked file
We will check that the downloaded file is as expected by comparing it to a fixture file in our final assertion.
We can approach this scenario in different ways. One possibility is to perform the first two steps, then extract
the href value and use it to retrieve the file with a GET request (performed with axios, for example).
We could also click the link directly and wait for the download event, then proceed with the comparison.
Note that in this case, we need to enable downloads in the browser context before proceeding.
Both examples can be run as follows:
USER_EMAIL=user@email.com USER_PASSWORD=supersecure1 npx playwright test file-download.spec.ts
SET USER_EMAIL=user@email.com
SET USER_PASSWORD=supersecure1
npx playwright test file-download.spec.ts