{"id":3262,"date":"2025-01-16T04:28:56","date_gmt":"2025-01-16T04:28:56","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/upload-file-using-playwright-java\/"},"modified":"2025-01-16T04:28:56","modified_gmt":"2025-01-16T04:28:56","slug":"upload-file-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/upload-file-using-playwright-java\/","title":{"rendered":"Upload file using Playwright Java"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3262\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>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.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Upload existing file<\/li>\n<li>Generating and uploading a fresh file dynamically during runtime<\/li>\n<li>Upload multiple files<\/li>\n<li>Source code (upload existing file)<\/li>\n<li>Source code (create\/upload file during runtime)<\/li>\n<li>Source code (upload multiple files)<\/li>\n<\/ul>\n<p><strong>Upload existing file<\/strong><\/p>\n<p>We will now look at the process of uploading a pre-existing file with Playwright Java.<\/p>\n<p>Create dummy pdf file in the playwright project, see below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d60cccf\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3265\" data-css=\"tve-u-1946d60db9b\" data-height=\"292\" data-id=\"3265\" data-init-height=\"628\" data-init-width=\"870\" data-width=\"404\" decoding=\"async\" height=\"292\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.00.15\u202fAM.png\" style=\"aspect-ratio: auto 870 \/ 628;\" title=\"Screenshot 2025-01-16 at 10.00.15\u202fAM\" width=\"404\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We are going to upload this file to the site<\/p>\n<p><a class=\"\" href=\"https:\/\/the-internet.herokuapp.com\/upload\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/the-internet.herokuapp.com\/upload<\/a>\u00a0<\/p>\n<p>Let us inspect \u2018Choose File\u2019 button<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d61dfb5\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3267\" data-css=\"tve-u-1946d6239c0\" data-height=\"272\" data-id=\"3267\" data-init-height=\"628\" data-init-width=\"1172\" data-width=\"508\" decoding=\"async\" height=\"272\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.01.09\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 628;\" title=\"Screenshot 2025-01-16 at 10.01.09\u202fAM\" width=\"508\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0There is a \u2018<strong><em>setInputFiles<\/em><\/strong>\u2019 method in playwright that can be used to set the input file<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d62b233\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3268\" data-css=\"tve-u-1946d62e244\" data-height=\"97\" data-id=\"3268\" data-init-height=\"194\" data-init-width=\"1172\" data-width=\"586\" decoding=\"async\" height=\"97\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.02.18\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 194;\" title=\"Screenshot 2025-01-16 at 10.02.18\u202fAM\" width=\"586\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Two arguments are required for this method: the selector, which is the locator of the &#8216;Choose File&#8217; button in the current case, and the file name (which is the input file that needs to be uploaded).<\/p>\n<p>Note: <em>import java.nio.file.Paths;<\/em>\u00a0<\/p>\n<p>Code snippet so far<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d637490\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3269\" data-css=\"tve-u-1946d63a324\" data-height=\"164\" data-id=\"3269\" data-init-height=\"310\" data-init-width=\"1172\" data-width=\"619\" decoding=\"async\" height=\"164\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.03.01\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 310;\" title=\"Screenshot 2025-01-16 at 10.03.01\u202fAM\" width=\"619\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>Note below that the file has been chosen successfully<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d6416f3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3270\" data-css=\"tve-u-1946d6427df\" data-height=\"233\" data-id=\"3270\" data-init-height=\"528\" data-init-width=\"1172\" data-width=\"517\" decoding=\"async\" height=\"233\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.03.54\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 528;\" title=\"Screenshot 2025-01-16 at 10.03.54\u202fAM\" width=\"517\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Next we will click the \u2018Upload\u2019 button to upload the file.<\/p>\n<p>It is advisable to introduce a brief waiting period before proceeding to click on the &#8216;Upload&#8217; button<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d64a33a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3271\" data-css=\"tve-u-1946d64b6a6\" data-height=\"145\" data-id=\"3271\" data-init-height=\"302\" data-init-width=\"1172\" data-width=\"563\" decoding=\"async\" height=\"145\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.04.23\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 302;\" title=\"Screenshot 2025-01-16 at 10.04.23\u202fAM\" width=\"563\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.\u00a0<\/p>\n<p>Note below that file has been uploaded<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d653eda\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3272\" data-css=\"tve-u-1946d65586a\" data-height=\"203\" data-id=\"3272\" data-init-height=\"406\" data-init-width=\"1172\" data-width=\"585\" decoding=\"async\" height=\"203\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.05.08\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 406;\" title=\"Screenshot 2025-01-16 at 10.05.08\u202fAM\" width=\"585\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is how uploading of an existing file is made simple with Playwright.<\/p>\n<p><strong>Generating and uploading a fresh file dynamically during runtime<\/strong><\/p>\n<p>Let us now create a new file and upload it at runtime.<\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/cgi-lib.berkeley.edu\/ex\/fup.html\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/cgi-lib.berkeley.edu\/ex\/fup.html<\/a>\u00a0<\/p>\n<p>We have to first inspect \u2018Choose File\u2019<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d65ecf9\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3273\" data-css=\"tve-u-1946d65fdb4\" data-height=\"324\" data-id=\"3273\" data-init-height=\"750\" data-init-width=\"1172\" data-width=\"506\" decoding=\"async\" height=\"324\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.05.49\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 750;\" title=\"Screenshot 2025-01-16 at 10.05.49\u202fAM\" width=\"506\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0For creating a new file at runtime, we can again make use of the \u2018<em>setInputFiles<\/em>\u2019 method with different set of arguments<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d668532\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3274\" data-css=\"tve-u-1946d66a515\" data-height=\"273\" data-id=\"3274\" data-init-height=\"544\" data-init-width=\"1172\" data-width=\"588\" decoding=\"async\" height=\"273\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.06.30\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 544;\" title=\"Screenshot 2025-01-16 at 10.06.30\u202fAM\" width=\"588\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute. Note below that the file is successfully chosen<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d673106\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3275\" data-css=\"tve-u-1946d6747ac\" data-height=\"422\" data-id=\"3275\" data-init-height=\"940\" data-init-width=\"1172\" data-width=\"526\" decoding=\"async\" height=\"422\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.07.17\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 940;\" title=\"Screenshot 2025-01-16 at 10.07.17\u202fAM\" width=\"526\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We now press the \u2018Press\u2019 button to upload file<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d67dbab\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3276\" data-css=\"tve-u-1946d67fcb6\" data-height=\"40\" data-id=\"3276\" data-init-height=\"96\" data-init-width=\"1172\" data-width=\"489\" decoding=\"async\" height=\"40\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.07.59\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 96;\" title=\"Screenshot 2025-01-16 at 10.07.59\u202fAM\" width=\"489\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.\u00a0<\/p>\n<p>File gets uploaded, the file contents are clearly visible, see below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d68890a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3277\" data-css=\"tve-u-1946d689e75\" data-height=\"300\" data-id=\"3277\" data-init-height=\"694\" data-init-width=\"1172\" data-width=\"507\" decoding=\"async\" height=\"300\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.08.45\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 694;\" title=\"Screenshot 2025-01-16 at 10.08.45\u202fAM\" width=\"507\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Upload multiple files<\/strong><\/p>\n<p>Let us now see how to upload multiple files.\u00a0<\/p>\n<p>First of all, let us add 2 files under src\/test\/resources\/files folder<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d699110\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3278\" data-css=\"tve-u-1946d69a391\" data-height=\"211\" data-id=\"3278\" data-init-height=\"518\" data-init-width=\"1172\" data-width=\"477\" decoding=\"async\" height=\"211\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.09.53\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 518;\" title=\"Screenshot 2025-01-16 at 10.09.53\u202fAM\" width=\"477\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We would be uploading these 2 files using playwright.<\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/www.w3schools.com\/jsref\/tryit.asp?filename=tryjsref_fileupload_multiple\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/www.w3schools.com\/jsref\/tryit.asp?filename=tryjsref_fileupload_multiple<\/a>\u00a0<\/p>\n<p>Inspect \u2018Choose Files\u2019 button. Notice that this element is a part of frame and the frame id is as seen below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d6a6d2c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3279\" data-css=\"tve-u-1946d6a7e7d\" data-height=\"380\" data-id=\"3279\" data-init-height=\"890\" data-init-width=\"1172\" data-width=\"501\" decoding=\"async\" height=\"380\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.10.45\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 890;\" title=\"Screenshot 2025-01-16 at 10.10.45\u202fAM\" width=\"501\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Make sure to import below 2 libraries<\/p>\n<p><em>import java.nio.file.Path;<br \/>import java.nio.file.Paths;<\/em><\/p>\n<p>We can again make use of <em>setInputFiles<\/em> method with a different set of arguments as shown below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d6ae791\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3280\" data-css=\"tve-u-1946d6b04d4\" data-height=\"158\" data-id=\"3280\" data-init-height=\"322\" data-init-width=\"1172\" data-width=\"574\" decoding=\"async\" height=\"158\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.11.20\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 322;\" title=\"Screenshot 2025-01-16 at 10.11.20\u202fAM\" width=\"574\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>Notice below that 2 files get uploaded.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d6b652f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3281 tcb-moved-image\" data-css=\"tve-u-1946d6b7931\" data-height=\"267\" data-id=\"3281\" data-init-height=\"612\" data-init-width=\"1172\" data-width=\"511\" decoding=\"async\" height=\"267\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.11.51\u202fAM.png\" style=\"aspect-ratio: auto 1172 \/ 612;\" title=\"Screenshot 2025-01-16 at 10.11.51\u202fAM\" width=\"511\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is how we can upload multiple files.<\/p>\n<p><strong>Source code (upload existing file)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> java.nio.file.Paths;<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog15_UploadExistingFile {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() <strong>throws<\/strong> InterruptedException {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/upload&#8221;);<\/p>\n<p><\/p>\n<p>pg.setInputFiles(&#8220;#file-upload&#8221;, Paths.<em>get<\/em>(&#8220;pwfile.pdf&#8221;));<\/p>\n<p>Thread.<em>sleep<\/em>(2000);<\/p>\n<p>pg.locator(&#8220;text = Upload&#8221;).last().click(); \/\/Upload<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (create\/upload file during runtime)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> java.nio.charset.StandardCharsets;<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.options.FilePayload;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog15_CreateUploadFileDuringRuntime {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() <strong>throws<\/strong> InterruptedException {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/cgi-lib.berkeley.edu\/ex\/fup.html&#8221;);<\/p>\n<p>pg.setInputFiles(&#8220;input[name=&#8217;upfile&#8217;]&#8221;, <strong>new<\/strong> FilePayload(&#8220;pwfile.text&#8221;,<\/p>\n<p>&#8220;text\/plain&#8221;,&#8221;Rahul Shetty Academy https:\/\/rahulshettyacademy.com\/&#8221;.getBytes(StandardCharsets.<strong><em>UTF_8<\/em><\/strong>)));<\/p>\n<p>pg.locator(&#8220;text = Press&#8221;).click();<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (upload multiple files)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> java.nio.file.Path;<\/p>\n<p><strong>import<\/strong> java.nio.file.Paths;<\/p>\n<p><strong>import<\/strong> org.junit.jupiter.api.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog15_MultipleFilesUpload {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() <strong>throws<\/strong> InterruptedException {<\/p>\n<p>Playwright pt = Playwright.<em>create<\/em>();<\/p>\n<p>LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/p>\n<p>lo.setChannel(&#8220;msedge&#8221;);<\/p>\n<p>lo.setHeadless(<strong>false<\/strong>);<\/p>\n<p>BrowserType btype = pt.chromium();<\/p>\n<p>Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/p>\n<p>Page pg = b.newPage();<\/p>\n<p>pg.navigate(&#8220;https:\/\/www.w3schools.com\/jsref\/tryit.asp?filename=tryjsref_fileupload_multiple&#8221;);<\/p>\n<p>pg.frameLocator(&#8220;#iframeResult&#8221;).locator(&#8220;#myFile&#8221;).setInputFiles(<strong>new<\/strong> Path[] {<\/p>\n<p>Paths.<em>get<\/em>(&#8220;.\/src\/test\/resources\/files\/file1.pdf&#8221;),<\/p>\n<p>Paths.<em>get<\/em>(&#8220;.\/src\/test\/resources\/files\/file2.jpeg&#8221;)<\/p>\n<p>});<\/p>\n<p><\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>Thanks.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3262","post","type-post","status-publish","format-standard","hentry","category-java","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3262"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3262\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}