How to download a file from remote machine to your local system using Java TestNG?

How do I download a file from remote machine to my local system using Java TestNG?

Hi Emma,

You can use our lambda download hooks to download the base64 encoded content of the downloaded file and then decode it to the same file in your local machine.

Sample code: In the below-given sample code, we are downloading the chromedriver from: https://chromedriver.storage.googleapis.com/index.html?path=2.0/

Once you click on the download button, you can add the following code snippet to download the same file onto your local machine.

  • Downloads file from remote to the local machine:

      String base64EncodedFile = ((JavascriptExecutor) driver).executeScript("lambda-file-content=chromedriver_win32.zip").toString(); // file content download
      byte[] byteArray = Base64.decodeBase64(base64EncodedFile.getBytes());
    
  • Creates a file on your local system

      FileOutputStream fos;
      File file=new File("/Users/vishnu/IdeaProjects/testNG_Suite/chromedriver_win32.zip");
      fos = new FileOutputStream(file);
      fos.write(byteArray);
      fos.close();