Back to Blog
Automating File Upload Tests with Selenium and Cypress
Advertisement
The Automation Imperative
Manual regressions are the enemy of speed. File upload workflows are often excluded from automation suites due to the difficulty of interacting with native OS file pickers. However, modern frameworks have solved this.
Cypress Strategy
Cypress bypasses the browser UI entirely, injecting the file directly into the input event logic.
cy.get('[data-testid="file-input"]').selectFile({
contents: Cypress.Buffer.from('file contents'),
fileName: 'report.pdf',
mimeType: 'application/pdf',
})
Selenium WebDriver Strategy
In Selenium, the key is to avoid clicking the "Browse" button. Instead, send the absolute file path keys directly to the <input type="file"> element.
# Python Selenium Example
element = driver.find_element(By.ID, "upload_input")
element.send_keys("/var/tmp/generated_dummy_file.pdf")
Best Practice
Combine these scripts with a standardized folder of dummy files in your CI environment. This ensures that every build is tested against the same valid and invalid file artifacts.
Advertisement
