t.setFilesToUpload Method
Sets files to upload. Emulates a user’s selection in the browser’s ‘Choose File’ dialog. Target element must be an <input>
with the type="file"
attribute.
t.setFilesToUpload(selector, filePath) → this | Promise<any>
Parameter | Type | Description |
---|---|---|
selector |
Function | String | Selector | Snapshot | Promise | The target <input> element (see Select Target Elements). |
filePath |
String | Array | The path(s) to the uploaded file. Relative paths are resolved against the folder with the test file. |
The following example illustrates how to use the t.setFilesToUpload
action:
fixture`TestController.setFilesToUpload`
.page`./index.html`;
test('Uploading', async t => {
await t
.setFilesToUpload('#upload-input', [
'./uploads/1.svg',
'./uploads/2.svg',
'./uploads/3.svg',
])
.click('#upload-button');
});
The t.setFilesToUpload
action removes all file paths from the input before populating it with new ones.
Note
If an error occurs while files are being uploaded, the test will fail.
The t.clearUpload method allows you to remove files added with t.setFilesToUpload
.
Select Target Elements
Use the selector
parameter to identify the target of a DOM event.
You can pass any of the following objects as a selector
:
A CSS selector string.
test('Click submit', async t => { // Click will be performed on the first element // that matches the CSS selector. await t.click('#submit-button'); });
A selector.
import { Selector } from 'testcafe'; fixture`Parameter as Selector` .page`https://devexpress.github.io/testcafe/example/`; const lastCheckbox = Selector('fieldset p:last-child [type="checkbox"]'); test('Click last checkbox', async t => { // Click will be performed on the element selected by // the 'getLastItem' selector. await t.click(lastCheckbox); });
A client-side function that returns a DOM element.
test('Click first child of the body', async t => { // Click will be performed on the element returned by the function, // which is the third child of the document's body. await t.click(() => document.body.children[0]); });
-
import { Selector } from 'testcafe'; fixture`Parameter as DOM node snapshot` .page`https://devexpress.github.io/testcafe/example/`; test('Click preferred interface', async t => { const preferredInterface = await Selector('#preferred-interface'); // Click will be performed on the element whose snapshot // is specified. This is an element with the '#preferred-interface' ID. await t.click(preferredInterface); });
A Promise returned by a selector.
import { Selector } from 'testcafe'; const submitButton = Selector('#submit-button'); fixture`Parameter as Promise by Selector` .page`https://devexpress.github.io/testcafe/example/`; test('Click submit', async t => { // Click will be performed on the element specified by the selector // as soon as the promise is resolved. await t.click(submitButton()); });
TestCafe waits for the target element to become visible before it executes an action. If this does not happen within the selector timeout, the test fails.
TestCafe cannot interact with background elements. If a different element overlaps the action target, TestCafe waits for this element to disappear. If this does not happen within the selector timeout, TestCafe performs the action with the element on top of the original target. Learn more about stacking and z-index on MDN.
Note
The upload action is an exception to this rule — it does not check the visibility of the target input
.