playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Induce Input File Upload Error

Open phibao37 opened this issue 4 years ago • 3 comments

Context:

  • Playwright Version: v1.24.1
  • Operating System: Windows 10
  • Java version: JDK 8
  • Browser: Google Chrome
  • Extra: N/A

Code Snippet

Help us help you! Put down a short code snippet that illustrates your bug and that we can run and debug locally. For example:


String filePath = Paths.get("myfile.pdf");
page.locator("input#upload").setInputFiles(filePath);
Files.delete(filePath);
page.click("button[type='submit']");
assertThat("#errorMsg").hasText("Cannot read file, the file has been moved, edited, or deleted");

** We have a website under testing that have an upload form with file selection input. It has a special behavior as below: If the user selects a file, then forces move/edit/delete the file before clicking the "Upload" button, the website should show the error "Cannot read file, the file has been moved, edited, or deleted".

If we perform manual test the above function, the website works fine. However, when using Playwright for automation test for above function, the website doesn't display error, the file still able to upload success.

I think that when using Playwright, it using some special method that copy the content of selected file to "somewhere", so even if we move/edit/delete the file, the HTML form still able to upload (it doesn't "link" to the real file like using manual test).

Is my above understanding correct? Is there anyway to test for the above function using PlayWright?

**

Add any other details about the problem here.

phibao37 avatar Aug 08 '22 15:08 phibao37

Is my above understanding correct?

At a high-level, yes. Abstractly, Playwright copies the files and/or has them in memory (at least for small files). Large files follow a different path and a few other implement details come into play.

Is there anyway to test for the above function using PlayWright?

No, but it also sounds like this is testing the browser's native behavior/file upload functionality and not the website. Is the upload button the user is clicking the native button the browser's file browser shows, or something in app?

rwoll avatar Aug 08 '22 17:08 rwoll

@rwoll Thank you for your confirmation. I also think the above function seems like the browser's native behavior. At the moment, we will plan to test it manually (web browser and website both belong to "software under test", so we still want to test their behavior).

Is the upload button the user is clicking the native button the browser's file browser shows, or something in app?

It is not the native form upload button, it is customized with Javascript to perform AJAX-upload processing. Below is the sample code:

$('#btnFileUpload').on('click', function() {
	var readFile = new FileReader();
	selectedFile = $('input#upload').prop("files")[0];
	readFile.onload = function(e) {
		var formData = new FormData();
		formData.append('file', selectedFile);

		$.ajax({
			url: "/uploadFile",
			type: "POST",
			processData: false,
			contentType: false,
			data: formData
		});
	}

	// When cannot read the file
	readFile.onerror = function(e) {
		console.log(readFile.error);
		$("#errorMsg").html("Cannot read file, the file has been moved, edited, or deleted");
	}

	readFile.readAsText(selectedFile);

});

phibao37 avatar Aug 09 '22 04:08 phibao37

Thanks—converting to P3-collecting-feedback. It would be nice if Playwright could help you induce your page code:

	readFile.onerror = function(e) {
		console.log(readFile.error);
		$("#errorMsg").html("Cannot read file, the file has been moved, edited, or deleted");
	}

but the input file API it exposes will not work here today for this case.

rwoll avatar Aug 09 '22 22:08 rwoll

Why was this issue closed?

Thank you for your contribution to our project. This issue has been closed due to its limited upvotes and recent activity, and insufficient feedback for us to effectively act upon. Our priority is to focus on bugs that reflect higher user engagement and have actionable feedback, to ensure our bug database stays manageable.

Should you feel this closure was in error, please create a new issue and reference this one. We're open to revisiting it given increased support or additional clarity. Your understanding and cooperation are greatly appreciated.

pavelfeldman avatar Jun 30 '23 18:06 pavelfeldman