Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upload files with required inputs in native automation #8092

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 27 additions & 2 deletions src/client/automation/playback/upload.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { doUpload } from '../deps/hammerhead';
import {
doUpload, eventSandbox, nativeMethods,
Bayheck marked this conversation as resolved.
Show resolved Hide resolved
} from '../deps/hammerhead';
import { arrayUtils } from '../deps/testcafe-core';


const REQUIRED_ATTR_NAME = 'required';
const FORM_SUBMIT_EVENT_NAME = 'submit';

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_ATTR_NAME);

if (isRequired)
nativeMethods.removeAttribute.call(this.element, REQUIRED_ATTR_NAME);
Aleksey28 marked this conversation as resolved.
Show resolved Hide resolved

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

if (this.element.form) {
eventSandbox.listeners.initElementListening(this.element.form, [FORM_SUBMIT_EVENT_NAME]);
eventSandbox.listeners.addInternalEventAfterListener(this.element.form, [FORM_SUBMIT_EVENT_NAME], 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="fileRequired" name="fileRequired" required />
<input type="submit" id="submitRequired" />
</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('#fileRequired', '../test-data/file1.txt')
.click('#submitRequired');

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