{"id":3284,"date":"2025-01-16T04:44:06","date_gmt":"2025-01-16T04:44:06","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3284"},"modified":"2025-01-17T10:40:06","modified_gmt":"2025-01-17T10:40:06","slug":"file-download-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/file-download-using-playwright-java\/","title":{"rendered":"File Download using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will utilize playwright-java to download a file and save it at temporary as well as specific location.<\/em><em>&nbsp;<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>File download at default\/temporary location<\/li>\n<li>File download at specific desired location (inside playwright project)<\/li>\n<li>File download example from chromedriver storage<\/li>\n<li>Source code (default download path)<\/li>\n<li>Source code (specific download path)<\/li>\n<li>Source code (chromedriver storage)<\/li>\n<\/ul>\n<p><strong>File download at default temporary location<\/strong><\/p>\n<p>Playwright Java allows us to easily download files from a test website page.<\/p>\n<p>Go to the website<\/p>\n<p><a href=\"https:\/\/the-internet.herokuapp.com\/download\" target=\"_blank\" rel=\"noopener\">https:\/\/the-internet.herokuapp.com\/download<\/a>&nbsp;<\/p>\n<p>There are many test files on this page as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3287\" width=\"348\" data-init-width=\"886\" height=\"275\" data-init-height=\"700\" title=\"Screenshot 2025-01-16 at 10.15.23\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.15.23\u202fAM.png\" data-width=\"348\" data-height=\"275\" style=\"aspect-ratio: auto 886 \/ 700;\"><\/span><\/p>\n<p>&nbsp;Inspect any file, say for example \u2018json.json\u2019&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3288\" width=\"505\" data-init-width=\"1168\" height=\"230\" data-init-height=\"532\" title=\"Screenshot 2025-01-16 at 10.16.14\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.16.14\u202fAM.png\" data-width=\"505\" data-height=\"230\" style=\"aspect-ratio: auto 1168 \/ 532;\"><\/span><\/p>\n<p>&nbsp;The function &#8216;<em>waitForDownload()<\/em>&#8216; can be utilized to guarantee that playwright waits until the download is finished.&nbsp;<\/p>\n<p>By calling the &#8216;waitForDownload()&#8217; function, you will receive an object of type &#8216;Download&#8217; &nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3289\" width=\"572\" data-init-width=\"1168\" height=\"172\" data-init-height=\"352\" title=\"Screenshot 2025-01-16 at 10.21.27\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.21.27\u202fAM.png\" data-width=\"572\" data-height=\"172\" style=\"aspect-ratio: auto 1168 \/ 352;\"><\/span><\/p>\n<p>&nbsp;The lambda function in the above code performs the \u2018click\u2019 operation to download the file&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3290\" width=\"526\" data-init-width=\"1168\" height=\"370\" data-init-height=\"822\" title=\"Screenshot 2025-01-16 at 10.22.09\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.22.09\u202fAM.png\" data-width=\"526\" data-height=\"370\" style=\"aspect-ratio: auto 1168 \/ 822;\"><\/span><\/p>\n<p>&nbsp;Execute, note below that file gets downloaded<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3291\" width=\"486\" data-init-width=\"1168\" height=\"209\" data-init-height=\"502\" title=\"Screenshot 2025-01-16 at 10.24.14\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.24.14\u202fAM.png\" data-width=\"486\" data-height=\"209\" style=\"aspect-ratio: auto 1168 \/ 502;\"><\/span><\/p>\n<p>&nbsp;Logging the URL of the downloaded file can be done as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3292\" width=\"578\" data-init-width=\"1168\" height=\"200\" data-init-height=\"404\" title=\"Screenshot 2025-01-16 at 10.24.56\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.24.56\u202fAM.png\" data-width=\"578\" data-height=\"200\" style=\"aspect-ratio: auto 1168 \/ 404;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>The console displays the file URL, including the filename<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3293\" width=\"533\" data-init-width=\"1168\" height=\"87\" data-init-height=\"190\" title=\"Screenshot 2025-01-16 at 10.25.33\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.25.33\u202fAM.png\" data-width=\"533\" data-height=\"87\" style=\"aspect-ratio: auto 1168 \/ 190;\"><\/span><\/p>\n<p>&nbsp;The file gets downloaded to the default path.&nbsp;<\/p>\n<p>We can make use of the \u2018path\u2019 method to find out the temporary location of the file during runtime&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3294\" width=\"571\" data-init-width=\"1168\" height=\"174\" data-init-height=\"356\" title=\"Screenshot 2025-01-16 at 10.26.36\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.26.36\u202fAM.png\" data-width=\"571\" data-height=\"174\" style=\"aspect-ratio: auto 1168 \/ 356;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>The temporary path being displayed in the console can be observed below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3295\" width=\"572\" data-init-width=\"1168\" height=\"144\" data-init-height=\"294\" title=\"Screenshot 2025-01-16 at 10.27.05\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.27.05\u202fAM.png\" data-width=\"572\" data-height=\"144\" style=\"aspect-ratio: auto 1168 \/ 294;\"><\/span><\/p>\n<p>&nbsp;<strong>File download at specific desired location (inside playwright project)<\/strong><\/p>\n<p>\u2018<em>saveAs<\/em>\u2019 method can be used to download a file at specific location inside playwright project&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3296\" width=\"558\" data-init-width=\"1168\" height=\"151\" data-init-height=\"316\" title=\"Screenshot 2025-01-16 at 10.27.55\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.27.55\u202fAM.png\" data-width=\"558\" data-height=\"151\" style=\"aspect-ratio: auto 1168 \/ 316;\"><\/span><\/p>\n<p>&nbsp;Execute and refresh project.&nbsp;<\/p>\n<p>The zip file is downloaded inside project<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3297\" width=\"546\" data-init-width=\"1168\" height=\"316\" data-init-height=\"676\" title=\"Screenshot 2025-01-16 at 10.29.22\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.29.22\u202fAM.png\" data-width=\"546\" data-height=\"316\" style=\"aspect-ratio: auto 1168 \/ 676;\"><\/span><\/p>\n<p>&nbsp;To download it inside <em>src\/test\/resources\/files\/<\/em> folder, simply change the path as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3298\" width=\"547\" data-init-width=\"1168\" height=\"360\" data-init-height=\"768\" title=\"Screenshot 2025-01-16 at 10.29.58\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.29.58\u202fAM.png\" data-width=\"547\" data-height=\"360\" style=\"aspect-ratio: auto 1168 \/ 768;\"><\/span><\/p>\n<p>&nbsp;<strong>File download example from chromedriver storage<\/strong><\/p>\n<p>Go to<\/p>\n<p><a href=\"https:\/\/chromedriver.storage.googleapis.com\/index.html?path=102.0.5005.27\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/chromedriver.storage.googleapis.com\/index.html?path=102.0.5005.27\/<\/a>&nbsp;<\/p>\n<p>Let us try to download the highlighted zip file<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3299\" width=\"451\" data-init-width=\"1168\" height=\"385\" data-init-height=\"996\" title=\"Screenshot 2025-01-16 at 10.30.38\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.30.38\u202fAM.png\" data-width=\"451\" data-height=\"385\" style=\"aspect-ratio: auto 1168 \/ 996;\"><\/span><\/p>\n<p>&nbsp;Execute the code, notice that the file gets downloaded<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3300\" width=\"508\" data-init-width=\"1168\" height=\"297\" data-init-height=\"684\" title=\"Screenshot 2025-01-16 at 10.31.30\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.31.30\u202fAM.png\" data-width=\"508\" data-height=\"297\" style=\"aspect-ratio: auto 1168 \/ 684;\"><\/span><\/p>\n<p>&nbsp;So this is how we can download the files using playwright.<\/p>\n<p><strong>Source code (default download path)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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.Download;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog16_DownloadFileTempPath {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><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\/download&#8221;);<\/p>\n<p>Download dl = pg.waitForDownload(() -&gt; {<\/p>\n<p>pg.click(&#8220;text=json.json&#8221;);<\/p>\n<p>});<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;The file url is&#8211;&gt;&#8221; + dl.url());<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Temporary\/default file path&#8211;&gt;&#8221; + dl.path().toString());<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>Source code (specific download path)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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.Download;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog16_DownloadFilePlaywrightProject {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><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\/download&#8221;);<\/p>\n<p>Download dl = pg.waitForDownload(() -&gt; {<\/p>\n<p>pg.click(&#8220;text=json.json&#8221;);<\/p>\n<p>});<\/p>\n<p>\/\/dl.saveAs(Paths.get(&#8220;playwrightfiledummy.zip&#8221;));<\/p>\n<p>dl.saveAs(Paths.<em>get<\/em>(&#8220;.\/src\/test\/resources\/files\/playwrightfiledummy.zip&#8221;));<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>Source code (chromedriver storage)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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.Download;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog16_FileDownloadChromeStorage {<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><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:\/\/chromedriver.storage.googleapis.com\/index.html?path=102.0.5005.27\/&#8221;);<\/p>\n<p>Download dl = pg.waitForDownload(() -&gt; {<\/p>\n<p>pg.click(&#8220;text=chromedriver_win32.zip&#8221;);<\/p>\n<p>});<\/p>\n<p>dl.saveAs(Paths.<em>get<\/em>(&#8220;.\/src\/test\/resources\/files\/chromedriver_win32.zip&#8221;));<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(dl.suggestedFilename());<\/p>\n<p>pg.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will utilize playwright-java to download a file and save it at temporary as well as specific location.&nbsp; Topics that we will cover: File download at default\/temporary location File download at specific desired location (inside playwright project) File download example from chromedriver storage Source code (default download path) Source code (specific download [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3284","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=3284"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3284\/revisions"}],"predecessor-version":[{"id":3717,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3284\/revisions\/3717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}