Upload & Download
Download a file and verify it on disk, then upload a file and verify the path the application received.
Download
The file is sample-download.txt. In your test, set the browser download folder and then check the file exists on disk.
Upload
Only .txt, .csv, .json, .png and .jpg files are allowed
Practice tasks
- Download the file to a folder you control and assert the file exists with the right name.
- Upload a
.txtfile withsendKeys(path)/setInputFiles()and assert the file name is shown. - Upload an unsupported type such as
.exeand assert the error message appears. - Assert that the output shows the file size for a valid upload.
Sample automation code
// set the download folder before creating the driver
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "/tmp/downloads");
options.setExperimentalOption("prefs", prefs);
driver.findElement(By.id("download-button")).click();
Thread.sleep(1500);
Assert.assertTrue(new File("/tmp/downloads/sample-download.txt").exists());
driver.findElement(By.id("upload-file")).sendKeys("/tmp/upload-me.txt");
Assert.assertTrue(driver.findElement(By.id("uploaded-file-path")).getText().contains("upload-me.txt"));
const downloadPromise = page.waitForEvent('download');
await page.getByTestId('download').click();
const download = await downloadPromise;
expect(download.suggestedFilename()).toBe('sample-download.txt');
await page.getByTestId('upload').setInputFiles('/tmp/upload-me.txt');
await expect(page.getByTestId('uploaded-file-path')).toContainText('upload-me.txt');