File Download
When automating file downloads in web testing, it's essential to handle them consistently across different browsers to ensure reliable test execution.
Here, we provide best practices for file downloads and demonstrate how to configure download directories for Google Chrome, Mozilla Firefox, and Microsoft Edge.
Download Paths
Hardcoding download paths in test scripts can lead to maintenance issues and portability problems. Utilize relative paths for download directories to ensure portability and compatibility across different environments.
// 👎
// Hardcoded download path
const downloadPath = '/path/to/downloads';
// 👍
// Relative download path
const downloadPath = path.join(__dirname, 'downloads');
Wait Strategies
Failing to implement proper wait strategies can lead to race conditions or unreliable tests, especially for download completion. Implement explicit wait strategies to wait for file downloads to complete, ensuring synchronization between test steps.
// 👎
// No explicit wait for download completion
await browser.pause(5000);
// 👍
// Wait for file download completion
await waitUntil(async ()=> await fs.existsSync(downloadPath), 5000);
Configuring Download Directories
To override file download behavior for Google Chrome, Mozilla Firefox, and Microsoft Edge, provide the download directory in the WebDriverIO capabilities:
- Chrome
- Firefox
- Microsoft Edge
loading...
loading...
loading...
For an example implementation, refer to the WebdriverIO Test Download Behavior Recipe.
Configuring Chromium Browser Downloads
To change the download path for Chromium-based browsers (such as Chrome, Edge, Brave, etc.) using WebDriverIOs getPuppeteer
method for accessing Chrome DevTools.
const page = await browser.getPuppeteer();
// Initiate a CDP Session:
const cdpSession = await page.target().createCDPSession();
// Set the Download Path:
await cdpSession.send('Browser.setDownloadBehavior', { behavior: 'allow', downloadPath: downloadPath });