Skip to main content
  1. Articles/

Download file using Playwright Java

·3 mins

Files

Introduction #

While testing websites, we may have come across a scenario where we need to verify whether the download functionality is working as expected or not. Many websites support downloading of invoices and PDF files. Clicking on any link/ file name/ Invoice/ PDF file on the webpage triggers the download and as a tester, we need to verify that it is downloaded in the system. To verify that it is downloaded we may verify its presence by navigating to the Downloads section of the system and checking the file manually.

In Selenium there are different ways to handle file download by using the AutoIT tool, Robot Class, or Browser Profile Setting. In Playwright, things are quite simple!

File download in Playwright #

In Playwright, we just have to pass the click method which invokes the download as a Runnable callback function in the waitForDownload() method of the Page class.

Download download=page.waitForDownload(()-> {
page.locator(“a#windows-downloadbutton”).click();
});

waitforDownload() performs the desired action and waits for the download to complete. It will throw an error if the page is closed before the download event is fired.

Unlike Selenium, there is no need to write any wait condition until the download is completed. It keeps track internally of whether the file is downloaded or not.

waitForDownload() method returns a Download object. After the download is finished, we can fetch download details from this generated Download object (download). The download path becomes available once the download completes.

Let us take an example of downloading Node.js from its official website https://nodejs.org/en/download/ and verify whether it gets downloaded or not.

public class DownloadFile {
protected static Page page;
Playwright playwright;
@BeforeClass
public void setUp() {
playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
page = browser.newPage();
}
@Test
public void download() throws InterruptedException {
page.navigate("https://nodejs.org/en/download/");
Download download = page.waitForDownload(() -> {
page.locator("a#windows-downloadbutton").click();
});
System.out.println("Download url: " + download.url());
System.out.println("Download page title: " + download.page().title());
System.out.println("Download path : " + download.path().toString());
download.saveAs(Paths.get("Nodejs.msi"));
System.out.println("Suggested file name: " + download.suggestedFilename());
}
@AfterClass
public void tearDown() {
page.context().browser().close();
}
}

Methods of Download class #

  1. Download.url(): Returns downloaded URL as a String
download.url();
  1. Download.page(): Get the page that the download belongs to.

Download.page().title(): Returns the download page title.

download.page().title()
  1. Download.path(): Returns path to the downloaded file in case of successful download. The method will wait for the download to finish if necessary. The method throws when connected remotely. Use path().toString to return the String representation of the path.
download.path().toString()
  1. Download.suggestedFilename(): Returns suggested filename for this download. This filename can be later used for assertion.

This downloads the file at a temporary location (C:\Users<username>\AppData\Local\Temp). To save as a file at the desired location, use the saveAs() method of Download Class.

download.suggestedFilename()
  1. Download.saveAs(path): Copy the download to a user-specified path. It is safe to call this method while the download is still in progress. Will wait for the download to finish if necessary.

saveAs() method takes the Path variable as a parameter.

download.saveAs(Paths.get(“Nodejs.msi”));
  1. Download.cancel(): Cancels a download. Will not fail if the download is already finished or canceled. Upon successful cancellations, download.failure() would resolve to “canceled”.

  2. Download.failure(): Returns download error if any. Will wait for the download to finish if necessary.

download.failure()
@Test
public void download() throws InterruptedException {
page.navigate("https://nodejs.org/en/download/");
Download download = page.waitForDownload(() -> {
page.locator("a#windows-downloadbutton").click();
});
download.cancel();
System.out.println("Download failure message: " + download.failure());
}
  1. Download.delete(): Deletes the downloaded file. Will wait for the download to finish if necessary.
download.delete()