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. Waiting for the download event is currently supported by Playwright, but not by Puppeteer.
Note that in this case, we need to enable downloads in the browser context before proceeding.
Both examples can be run as follows:
- Use environment variables to inject secrets.
- Compare the expected file with the newly downloaded one.
- There is more than one way to download a file within our script.
# Further reading
- Playwright's documentation on downloading files.