In this blog we will utilize playwright-java to download a file and save it at temporary as well as specific location. 

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 path)
  • Source code (chromedriver storage)

File download at default temporary location

Playwright Java allows us to easily download files from a test website page.

Go to the website

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

There are many test files on this page as shown below

 Inspect any file, say for example ‘json.json’ 

 The function 'waitForDownload()' can be utilized to guarantee that playwright waits until the download is finished. 

By calling the 'waitForDownload()' function, you will receive an object of type 'Download'  

 The lambda function in the above code performs the ‘click’ operation to download the file 

 Execute, note below that file gets downloaded

 Logging the URL of the downloaded file can be done as shown below

 Execute.

The console displays the file URL, including the filename

 The file gets downloaded to the default path. 

We can make use of the ‘path’ method to find out the temporary location of the file during runtime 

 Execute.

The temporary path being displayed in the console can be observed below

 File download at specific desired location (inside playwright project)

saveAs’ method can be used to download a file at specific location inside playwright project 

 Execute and refresh project. 

The zip file is downloaded inside project

 To download it inside src/test/resources/files/ folder, simply change the path as shown below

 File download example from chromedriver storage

Go to

https://chromedriver.storage.googleapis.com/index.html?path=102.0.5005.27/ 

Let us try to download the highlighted zip file

 Execute the code, notice that the file gets downloaded

 So this is how we can download the files using playwright.

Source code (default download path)

package com.rsa.playwrightjava;


import org.junit.jupiter.api.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.BrowserType.LaunchOptions;

import com.microsoft.playwright.Download;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog16_DownloadFileTempPath {

@Test

public void PlaywrightJTest() throws InterruptedException {

Playwright pt = Playwright.create();

LaunchOptions lo = new LaunchOptions();

lo.setChannel("msedge");

lo.setHeadless(false);

BrowserType btype = pt.chromium();

Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));

Page pg = b.newPage();

pg.navigate("https://the-internet.herokuapp.com/download");

Download dl = pg.waitForDownload(() -> {

pg.click("text=json.json");

});

System.out.println("The file url is-->" + dl.url());

System.out.println("Temporary/default file path-->" + dl.path().toString());

pg.pause();

}

}

Source code (specific download path)

package com.rsa.playwrightjava;


import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.BrowserType.LaunchOptions;

import com.microsoft.playwright.Download;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog16_DownloadFilePlaywrightProject {

@Test

public void PlaywrightJTest() throws InterruptedException {

Playwright pt = Playwright.create();

LaunchOptions lo = new LaunchOptions();

lo.setChannel("msedge");

lo.setHeadless(false);

BrowserType btype = pt.chromium();

Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));

Page pg = b.newPage();

pg.navigate("https://the-internet.herokuapp.com/download");

Download dl = pg.waitForDownload(() -> {

pg.click("text=json.json");

});

//dl.saveAs(Paths.get("playwrightfiledummy.zip"));

dl.saveAs(Paths.get("./src/test/resources/files/playwrightfiledummy.zip"));

pg.pause();

}

}


Source code (chromedriver storage)

package com.rsa.playwrightjava;


import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.BrowserType.LaunchOptions;

import com.microsoft.playwright.Download;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog16_FileDownloadChromeStorage {

@Test

public void PlaywrightJTest() throws InterruptedException {

Playwright pt = Playwright.create();

LaunchOptions lo = new LaunchOptions();

lo.setChannel("msedge");

lo.setHeadless(false);

BrowserType btype = pt.chromium();

Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));

Page pg = b.newPage();

pg.navigate("https://chromedriver.storage.googleapis.com/index.html?path=102.0.5005.27/");

Download dl = pg.waitForDownload(() -> {

pg.click("text=chromedriver_win32.zip");

});

dl.saveAs(Paths.get("./src/test/resources/files/chromedriver_win32.zip"));

System.out.println(dl.suggestedFilename());

pg.pause();

}

}


Thanks.


Tags


You may also like

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}