Show List
Upload File
Here's a sample TestCafe test script that demonstrates working with an upload file control:
import { Selector } from 'testcafe';
fixture('Upload File Test')
.page('https://www.example.com');
test('Upload File', async t => {
// Selector for the file input element
const fileInput = Selector('input[type="file"]');
// Path to the file to upload
const filePath = '../path/to/your/file.txt'; // Update with the actual file path
// Upload the file
await t
.setFilesToUpload(fileInput, filePath);
// Assert that the file has been uploaded
const uploadedFile = await fileInput.value;
await t.expect(uploadedFile).contains('file.txt'); // Update with the file name
});
In this test:
- We import the
Selector
function from TestCafe to select elements on the web page. - We define a fixture named 'Upload File Test' and set the page to 'https://www.example.com'.
- Inside the test function:
- We define a selector for the file input element (
fileInput
). Replace'input[type="file"]'
with the appropriate selector for your file input element. - We specify the path to the file we want to upload (
filePath
). Update'../path/to/your/file.txt'
with the actual path to the file on your system. - We use TestCafe's
setFilesToUpload
action to upload the file specified byfilePath
to the file input element. - We assert that the file has been uploaded successfully by checking the value of the file input element.
- We define a selector for the file input element (
Make sure to replace 'https://www.example.com'
with the URL of the website you want to test, and adjust the selector accordingly based on the structure of the webpage you are testing. Additionally, update '../path/to/your/file.txt'
with the actual path to the file you want to upload, and update 'file.txt'
with the name of the file.
Leave a Comment