In this blog we will utilize playwright-java to dynamically generate and upload a fresh file at runtime. We will also learn to upload multiple files.
Topics that we will cover:
- Upload existing file
- Generating and uploading a fresh file dynamically during runtime
- Upload multiple files
- Source code (upload existing file)
- Source code (create/upload file during runtime)
- Source code (upload multiple files)
Upload existing file
We will now look at the process of uploading a pre-existing file with Playwright Java.
Create dummy pdf file in the playwright project, see below

We are going to upload this file to the site
https://the-internet.herokuapp.com/upload
Let us inspect ‘Choose File’ button

There is a ‘setInputFiles’ method in playwright that can be used to set the input file

Two arguments are required for this method: the selector, which is the locator of the 'Choose File' button in the current case, and the file name (which is the input file that needs to be uploaded).
Note: import java.nio.file.Paths;
Code snippet so far

Execute.
Note below that the file has been chosen successfully

Next we will click the ‘Upload’ button to upload the file.
It is advisable to introduce a brief waiting period before proceeding to click on the 'Upload' button

Execute.
Note below that file has been uploaded

This is how uploading of an existing file is made simple with Playwright.
Generating and uploading a fresh file dynamically during runtime
Let us now create a new file and upload it at runtime.
Go to https://cgi-lib.berkeley.edu/ex/fup.html
We have to first inspect ‘Choose File’

For creating a new file at runtime, we can again make use of the ‘setInputFiles’ method with different set of arguments

Execute. Note below that the file is successfully chosen

We now press the ‘Press’ button to upload file

Execute.
File gets uploaded, the file contents are clearly visible, see below

Upload multiple files
Let us now see how to upload multiple files.
First of all, let us add 2 files under src/test/resources/files folder

We would be uploading these 2 files using playwright.
Go to https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_multiple
Inspect ‘Choose Files’ button. Notice that this element is a part of frame and the frame id is as seen below

Make sure to import below 2 libraries
import java.nio.file.Path;
import java.nio.file.Paths;
We can again make use of setInputFiles method with a different set of arguments as shown below

Execute.
Notice below that 2 files get uploaded.

This is how we can upload multiple files.
Source code (upload existing file)
package com.rsa.playwrightjava;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog15_UploadExistingFile {
@Test
public void PlaywrightJTest() throws InterruptedException {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
pg.navigate("https://the-internet.herokuapp.com/upload");
pg.setInputFiles("#file-upload", Paths.get("pwfile.pdf"));
Thread.sleep(2000);
pg.locator("text = Upload").last().click(); //Upload
pg.pause();
}
}
Source code (create/upload file during runtime)
package com.rsa.playwrightjava;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.FilePayload;
public class Blog15_CreateUploadFileDuringRuntime {
@Test
public void PlaywrightJTest() throws InterruptedException {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
pg.navigate("https://cgi-lib.berkeley.edu/ex/fup.html");
pg.setInputFiles("input[name='upfile']", new FilePayload("pwfile.text",
"text/plain","Rahul Shetty Academy https://rahulshettyacademy.com/".getBytes(StandardCharsets.UTF_8)));
pg.locator("text = Press").click();
pg.pause();
}
}
Source code (upload multiple files)
package com.rsa.playwrightjava;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog15_MultipleFilesUpload {
@Test
public void PlaywrightJTest() throws InterruptedException {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
pg.navigate("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_multiple");
pg.frameLocator("#iframeResult").locator("#myFile").setInputFiles(new Path[] {
Paths.get("./src/test/resources/files/file1.pdf"),
Paths.get("./src/test/resources/files/file2.jpeg")
});
pg.pause();
}
}
Thanks.