#47: Print An Invoice (File Download)
Test File Download and Verify in Selenium WebDriver
Learning Objectives
Design reliable automated tests that are user aware.
Customize the Chrome browser to save downloaded files into a specified folder
Verify a File Exists
Awareness of execution side effects: the file already exists
Review:
"#{variable}/..."
Test Case #47
Test design
Login (business):
physio@biz.com
(password:test01
)Create an invoice (See Exercise #46)
Click the “Print” link, wait up to 5 seconds, and then check the PDF file is generated.
Obviously, the focus of this exercise is to check the downloaded file. It appears very simple during manual testing. However, in test automation, we need to consider the following:
How to verify a file exists?
This is easy: use code.To be able to do the above step, two pieces of information are required:
- the file name, in this case,invoice_C000002.pdf
- the path of the Chrome download folder. This will depend on who you logged in and on which platform. For example,/Users/courtney/downloads
is mine (on macOS).The hardcoded folder path can be problematic in a software team where all team members utilize the automated tests. Ideally, we would like the path of the Chrome download folder to be static.
We can set the download folder path when starting a Chrome browser in Selenium scripts.
The file might already exist before the test execution.
After running the test once, the file would be created. So on the second runu, depending on the operating system, the behaviour will be different. For example, on macOS, a suffix(1)
(image below) will be added.
How shall we solve it? By deleting the file in the test script before clicking the ‘Print” button.
Tasks
I suggest doing this exercise step by step. Check the downloaded file first, such as /Users/ME/invoice_C000002.pdf
. If this part is working, add the dynamic part.
Task 1. Verify a file exists
Keep reading with a 7-day free trial
Subscribe to AgileWay’s Test Automation & Continuous Testing Blog to keep reading this post and get 7 days of free access to the full post archives.