Wednesday 8 March 2023

Playwright: upload a file

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

 

Example

String filePath = resourceFilePath("files" + File.separator + "emp.json");
page.locator("#myFile").setInputFiles(Paths.get(filePath));

Find the below working application.

 

emp.json

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

fileUpload.html

<!DOCTYPE html>
<html>

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

	<script>
		function init() {
			document.getElementById('myFile').addEventListener('change', handleFileSelect, false);
		}

		function handleFileSelect(event) {
			const reader = new FileReader()
			reader.onload = handleFileLoad;
			reader.readAsText(event.target.files[0])
		}

		function handleFileLoad(event) {
			console.log(event);
			document.getElementById('fileContent').textContent = event.target.result;
		}
	</script>
</head>

<body onload="init()">

	<p>Click on the "Choose File" button to upload a file:</p>


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


	<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();
		}

	}
}

FileUploadDemo.java

package com.sample.app.miscellaneous;

import java.io.File;
import java.io.IOException;
import java.net.URL;
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 FileUploadDemo {

	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 + "fileUpload.html");

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

			String filePath = resourceFilePath("files" + File.separator + "emp.json");
			page.locator("#myFile").setInputFiles(Paths.get(filePath));

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

		}
	}
}

Output

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

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment