Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/client/automation/playback/upload.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { doUpload } from '../deps/hammerhead';
import {
doUpload, eventSandbox, nativeMethods,
} from '../deps/hammerhead';
import { arrayUtils } from '../deps/testcafe-core';


export default class UploadAutomation {
constructor (element, paths, createError) {
constructor (element, paths, createError, isNativeAutomation) {
this.element = element;
this.paths = paths;
this.createError = createError;

if (isNativeAutomation)
this._handleRequiredInput();
}

_handleRequiredInput () {
const isRequired = nativeMethods.hasAttribute.call(this.element, 'required');

if (isRequired)
nativeMethods.removeAttribute.call(this.element, 'required');

const ensureUploadInputHasRequiredAttribute = () => {
if (isRequired)
nativeMethods.setAttribute.call(this.element, 'required', 'true');
};

if (this.element.form) {
eventSandbox.listeners.initElementListening(this.element.form, ['submit']);
eventSandbox.listeners.addInternalEventAfterListener(this.element.form, ['submit'], ensureUploadInputHasRequiredAttribute);
}
}

run () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ function ensureFileInput (element: Node[]): never | void {
}

ActionExecutor.ACTIONS_HANDLERS[COMMAND_TYPE.setFilesToUpload] = {
create: (command, elements) => {
create: (command, elements, dispatchNativeAutomationEventFn) => {
return new UploadAutomation(elements[0], command.filePath,
(filePaths: string[], scannedFilePaths: string[]) => new ActionCannotFindFileToUploadError(filePaths, scannedFilePaths),
dispatchNativeAutomationEventFn
);
},

Expand Down
24 changes: 14 additions & 10 deletions test/functional/fixtures/api/es-next/upload/pages/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload</title>
</head>
<body>
<form action="/file-upload" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit" id="submit">
</form>
</body>
<head>
<meta charset="UTF-8" />
<title>Upload</title>
</head>
<body>
<form action="/file-upload" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<input type="submit" id="submit" />
</form>
<form action="/file-upload" method="POST" enctype="multipart/form-data">
<input type="file" id="fileTwo" name="fileTwo" required />
<input type="submit" id="submitTwo" />
</form>
</body>
</html>
4 changes: 4 additions & 0 deletions test/functional/fixtures/api/es-next/upload/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ describe('[API] Upload', function () {
return runTests('./testcafe-fixtures/upload-test.js', 'Upload the file', { only: 'chrome' });
});

it('Should upload the specified file with required input', function () {
return runTests('./testcafe-fixtures/upload-test.js', 'Upload the file with required input', { only: 'chrome' });
});

it('Should validate the selector argument', function () {
return runTests('./testcafe-fixtures/upload-test.js', 'Invalid selector argument (setFilesToUpload)', {
shouldFail: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ test('Invalid selector argument (clearUpload)', async t => {
test('Error on upload non-existing file', async t => {
await t.setFilesToUpload('#file', ['../dummy-file-1.txt', '../dummy-file-2.txt']);
});

test('Upload the file with required input', async t => {
await t
.setFilesToUpload('#fileTwo', '../test-data/file1.txt')
.click('#submitTwo');

expect(await getUploadedText()).equals('File 1 is uploaded!');
});