Thursday 9 March 2023

Playwright: upload multiple files

Using 'setInputFiles' method, we can set the value of the file input.

 

Example

Path[] paths = { Paths.get(filePath1), Paths.get(filePath2) };
page.locator("#myFile").setInputFiles(paths);

 Find the below working application.

 

emp.json

{
	"id": 1,
	"name": "Krishna",
	"age": 32
}

address.json

{
	"city": "Bangalore",
	"country": "India"
}

multiFileUpload.html

<!DOCTYPE html>
<html>

<head>
	<title>File upload example</title>

	<script>
		function appendFileContent(content) {
			const node = document.createElement("p");
			const textnode = document.createTextNode(content);
			node.appendChild(textnode);
			document.getElementById("fileContent").appendChild(node);
		}
		function readFile(file) {
			const reader = new FileReader();
			reader.onload = evt => {
				appendFileContent(reader.result);
			};
			reader.readAsText(file);
		}
		function init() {
			const fileSelector = document.getElementById("myFile");
			fileSelector.addEventListener("change", event => {
				const fileList = event.target.files;
				for (const file of fileList) {
					readFile(file);
				}
			});
		}
	</script>
</head>

<body onload="init()">
	<p>Click on the "Choose File" button to upload one or files:</p>

	<input type="file" id="myFile" name="filename" multiple />

	<pre id="fileContent"></pre>
</body>

</html>

FileUtil.java

package com.sample.app.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {
	
	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}
}

UploadMultipleFiles.java

package com.sample.app.miscellaneous;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class UploadMultipleFiles {

	public static String resourceFilePath(String resourceFile) {
		ClassLoader classLoader = FileUploadDemo.class.getClassLoader();
		URL url = classLoader.getResource(resourceFile);
		return url.getPath();
	}

	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
			final String content = FileUtil.resourceAsString("miscellaneous" + File.separator + "multiFileUpload.html");

			Page page = browser.newPage();
			page.setContent(content);

			String filePath1 = resourceFilePath("files" + File.separator + "emp.json");
			String filePath2 = resourceFilePath("files" + File.separator + "address.json");
			Path[] paths = { Paths.get(filePath1), Paths.get(filePath2) };
			page.locator("#myFile").setInputFiles(paths);

			String text = page.locator("#fileContent").textContent();
			System.out.println("files content : \n" + text);

		}
	}
}

Output

file content : 
{
	"id": 1,
	"name": "Krishna",
	"age": 32
}{
	"city": "Bangalore",
	"country": "India"
}

 


 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment