Skip to main content

_selenide

TL;DR - Show Me The Code

2-download-a-file/code/selenide/src/test/java/com/elemental/selenium/DownloadTest.java
package com.elemental.selenium;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;

import static com.codeborne.selenide.Selectors.byText;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static org.assertj.core.api.Assertions.assertThat;

public class DownloadTest {
@BeforeEach
public void openAUT() {
open("https://selenide.org/test-page/download.html");
}

@Test
public void downloadFile() {
File file = $(byText("hello-world.txt")).download();

assertThat(file).hasName("hello-world.txt");
assertThat(file).content().contains("Hello, world!");
}
}

Code Walkthrough

Importing Libraries

First, we pull in our requisite classes:

  • test runner (e.g., org.junit.jupiter.api.BeforeEach, etc.),
  • opening the browser with Selenide (e.g., com.codeborne.selenide.Selenide.open),
  • matchers for assertions (e.g., org.assertj.core.api.Assertions.assertThat)

NB! You don't need to add the typical "selenium*" dependencies. Just one dependency is enough: com.codeborne:selenide. It will pull down all the needed Selenium dependencies automatically.

Setup and Teardown

Next, we are setting up the browser instance. The openAUT opens the application under test before each test.

The Test

Our downloadFile test, after visiting the page, we find the download link and call method download for it. This method does its magic and returns the downloaded file. Now you can verify its name or content.

NB! You don't need to find the downloaded file in any directories. Method $.download() returns the file. It's already available for you, so you can verify it right away.

More About Downloading Files in Selenide

Selenide sample for downloading files is straightforward because Selenide does all the work for you:

  • creates a uniquely named temp directory
  • configures the browser to make it automatically download files to that unique directory
  • clicks a web element
  • waits until the file arises in the downloads folder

In addition, Selenide has more ways to download files:

  • HTTPGET (default one)
  • FOLDER
  • CDP
  • PROXY

Learn more about downloading files in Selenide from Andrei Solntsev's YouTube video and blog post.

Executing the Test

Before executing the test, we need to make sure the required dependencies are declared on the pom.xml file.

Toggle to see the pom.xml file.
2-download-a-file/code/selenide/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips-selenide-download-file</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>7.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Finally, we can run the test by executing mvn test from the command-line.