Download a File using Selenide

Tutorial 16​​ ​​ Download​​ a​​ file​​ using​​ Selenide​​ 

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

  • Download​​ a​​ file​​ (example 1)

  • Download a file (example 2)

  • Code snippets

Download a file (example 1)

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

Go to ​​ https://www.selenium.dev/downloads/​​ ​​ 

When we click the file version manually, the jar file gets downloaded

To automate the process,​​ we start with writing​​ the code to fetch and print the​​ path of the folder where the file will get downloaded

Let us now inspect the file version

The custom​​ xpath is path:

body > div > main > div:nth-child(3) > div.col-sm-6.py-3.pl-0.pr-3 > div > div > p:nth-child(1) > a

We than write the code to actually download the file and print the path of downloaded file and asserting if the file name contains the string “selenium”

Execute

 

 

Below is the actual file location

Download​​ a file (example 2)

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

Let us try to download a file

We can identify the file using linkText

Execute, the file gets downloaded

This is show​​ we can​​ download​​ a​​ file using Selenide.

Code snippet​​ (example 1)

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.$;

import​​ static​​ com.codeborne.selenide.Selenide.open;

 

import​​ java.io.File;

import​​ java.io.FileNotFoundException;

import​​ java.net.MalformedURLException;

 

import​​ org.openqa.selenium.By;

//import org.openqa.selenium.chrome.ChromeOptions;

import​​ org.openqa.selenium.firefox.FirefoxOptions;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.Configuration;

import​​ com.codeborne.selenide.FileDownloadMode;

 

public​​ class​​ ElementsTest {

private​​ FirefoxOptions​​ browserCapabilities;

//private ChromeOptions browserCapabilities;

private​​ String​​ browser;

private​​ boolean​​ holdBrowserOpen;

private​​ String​​ downloadsFolder;

private​​ String​​ baseUrl;

 

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

 FirefoxOptions​​ options​​ =​​ new​​ FirefoxOptions();

//ChromeOptions options = new ChromeOptions();

options.addArguments("remote-allow-origins=*");

 Configuration.browser​​ =​​ "firefox";

browserCapabilities​​ =​​ options;

//browser = "chrome";

browser​​ =​​ "firefox";

holdBrowserOpen=true;

 Configuration.fileDownload​​ = FileDownloadMode.FOLDER;

downloadsFolder​​ =​​ "Downloads";

 File​​ folder​​ =​​ new​​ File(downloadsFolder).getAbsoluteFile();

 System.out.println(folder);

//baseUrl = "https://www.selenium.dev/downloads/";

open("https://www.selenium.dev/downloads/");

 File​​ downloadedFile;

try​​ {

 

downloadedFile​​ =​​ $(By.cssSelector("body > div > main > div:nth-child(3) > div.col-sm-6.py-3.pl-0.pr-3 > div > div > p:nth-child(1) > a")).download(20000);

 System.out.println(downloadedFile.getAbsoluteFile());

 System.out.println(downloadedFile.getAbsoluteFile().toString().contains("selenium"));

 }​​ catch​​ (FileNotFoundException​​ e1) {

e1.printStackTrace();

 }

try​​ {

 Thread.sleep(5000);

 }​​ catch​​ (InterruptedException​​ e) {

//​​ TODO​​ Auto-generated catch block

e.printStackTrace();

 }

 }

}

 

Code snippet (example 2)

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.$;

import​​ static​​ com.codeborne.selenide.Selenide.open;

 

import​​ java.net.MalformedURLException;

 

import​​ org.openqa.selenium.By;

import​​ org.openqa.selenium.firefox.FirefoxOptions;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.Configuration;

 

public​​ class​​ ElementsTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

 Configuration.browser​​ =​​ "firefox";

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

$(By.linkText("LambdaTest.txt")).click();

 }

}

 

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 - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 25th April 2024