Create and upload a new file during runtime using Playwright Java

Tutorial 18​​ ​​ Create and upload a​​ new file​​ during runtime​​ using Playwright​​ Java​​ 

What you will Learn​​ in this​​ blog:

  • Upload​​ an existing​​ file using Playwright​​ java

  • Creating and uploading a new file on the fly (runtime)

  • Code snippets

Upload​​ an existing​​ file using Playwright​​ Java

Let us​​ first see​​ how to​​ upload an existing​​ file using PW​​ java.

We will​​ upload a​​ test​​ file​​ from our local machine​​ to​​ the below site

https://the-internet.herokuapp.com/upload​​ 

First we will​​ choose a file and click ‘Upload’.

Inspect ‘Choose File’ button

There is a ‘setInputFiles’ method that we can use to set the input file

This method accepts 2 arguments – selector (the locator of ‘Choose File’ button in current case) and file name.

Let us write the first argument using css selector

Next, create a file having extention .pdf​​ 

Mention​​ the relative path​​ of the file​​ as second argument

After setting the input file, let us give some wait time and than click ‘Upload’ button

The entire script looks like below:

Save​​ and execute.

Observe below that the file gets uploaded

This is show we can upload the file using PW.

Creating and uploading a new file on the fly (runtime)

Launch​​ https://cgi-lib.berkeley.edu/ex/fup.html​​ 

We will create a new file during runtime

Inspect ‘Choose File’

Below is the syntax​​ of creating a new file at runtime. There is a ‘setInputFiles’ method that accepts few arguments having the description below

The code looks like

After selecting the file, we have to press the ‘Press’ button to upload the same

Save and execute.​​ 

Notice below that file gets uploaded and the content of the file is displayed

Code snippet​​ (upload file)

package​​ com.w2a.pwjava;

 

import​​ java.nio.file.Paths;

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ UploadFilePW {

public​​ static​​ void​​ main(String[]​​ args)​​ throws​​ InterruptedException {

 Playwright​​ pw​​ = Playwright.create();

 Browser​​ browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

 

 BrowserContext​​ browserContext​​ =​​ browser.newContext();

 Page​​ page​​ =​​ browserContext.newPage();

page.navigate("https://the-internet.herokuapp.com/upload");

//syntax-page.setInputFiles(selector, files);

page.setInputFiles("#file-upload", Paths.get("test.pdf"));

 Thread.sleep(3000);

  ​​ ​​​​ // Click Upload

page.locator("text = Upload").last().click();

 }

 

}

 

Code snippet (create and upload file during runtime)

package​​ com.w2a.pwjava;

 

import​​ java.nio.charset.StandardCharsets;

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

import​​ com.microsoft.playwright.options.FilePayload;

 

public​​ class​​ UploadFilePW {

public​​ static​​ void​​ main(String[]​​ args)​​ throws​​ InterruptedException {

 Playwright​​ pw​​ = Playwright.create();

 Browser​​ browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

 BrowserContext​​ browserContext​​ =​​ browser.newContext();

 Page​​ page​​ =​​ browserContext.newPage();

page.navigate("https://cgi-lib.berkeley.edu/ex/fup.html");

page.setInputFiles("input[name='upfile']",​​ new​​ FilePayload("hello.text",

"text/plain","this is a test file".getBytes(StandardCharsets.UTF_8)));

page.locator("text = Press").click();

 }

 

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024