Download a File using Playwright Java

Tutorial 19​​ ​​ Download a File using​​ Playwright​​ Java​​ 

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

  • Download a file using Playwright Java

  • File download path

  • Code snippets

Download a file using PW

Let us​​ now see how to​​ download a file using Playwright Java.

We will download a file from​​ the​​ test website page

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

Let us inspect file​​ ‘LambdaTest.txt’​​ 

The ‘waitForDownload()’ function can be used to ensure that playwright waits for the download to complete ​​ 

We​​ than​​ pass a lambda function as an argument​​ to the above​​ method.​​ This lambda function simply performs the ‘click’ operation to download the file. As can be seen in the method description, the​​ ‘waitForDownload()’ function​​ returns an object of ‘Download’

​​ 

Our code so far:

Next, let us log the​​ url of​​ the file​​ that​​ gets downloaded

Save and execute.

Notice that the file gets downloaded​​ 

The file url (alongwith the filename) too can be seen in the console

So this is how​​ we can download a file using Playwright Java.

File download path

To know the path where the file gets downloaded during runtime, there is a ‘path’ method that we can use​​ 

Save and execute.

Notice below the temporary path getting printed in console

To download a file at specific location, we can use the ‘saveAs’ method

Save and execute.

Refresh the project. Notice that the zip file​​ gets downloaded inside the project

Code snippet​​ (without file download path)

package​​ com.w2a.pwjava;

 

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Download;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ DownloadFilePW {

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/download");

 Download​​ dload​​ =​​ page.waitForDownload(() -> {

page.click("text=LambdaTest.txt");

 });

 System.out.println(dload.url());

 }

 

}

 

Code snippet (file download path)

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.Download;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ DownloadFilePW {

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/download");

 Download​​ dload​​ =​​ page.waitForDownload(() -> {

page.click("text=LambdaTest.txt");

 });

 System.out.println(dload.url());

 System.out.println(dload.path().toString());

dload.saveAs(Paths.get("temp.zip"));

 }

 

}

 

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