Skip to main content

_selenide

TL;DR - Show Me The Code

1-upload-a-file/code/selenide/src/test/java/com/elemental/selenium/UploadTest.java
package com.elemental.selenium;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;

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

import java.io.File;

public class UploadTest {
@BeforeEach
void openAUT() {
open("https://the-internet.herokuapp.com/upload");
}

@Test
public void uploadFile() {
$("#file-upload").uploadFromClasspath("selenide-intro.txt");
$("#file-submit").click();
$("#uploaded-files").shouldHave(text("selenide-intro.txt"));
}

@Test
public void uploadFileUsingPath() {
$("#file-upload").uploadFile(new File("src/test/resources/selenide-intro.txt"));
$("#file-submit").click();
$("#uploaded-files").shouldHave(text("selenide-intro.txt"));
}
}

Code Walkthrough

Importing Libraries

Lines 3 to 7 are pulling in our requisite classes

  • for annotations (e.g., org.junit.jupiter.api.Test),
  • opening the browser with Selenide (e.g., com.codeborne.selenide.Selenide.open),
  • matchers for assertions (e.g., com.codeborne.selenide.Condition.text)
  • etc.

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 open a browser with given URL.

Under the hood, Selenide performs all the needed tasks:

  • detects the browser (can be customized if needed),
  • setups commons browser settings (can be customized if needed)

No teardown is needed because Selenide automatically closes the browser when the test finishes.

The Test

We call uploadFileFromClasspath() method and annotate it with @Test so it is run as a test.

Next, we visit the page with the upload form, find the input allowing to upload files, and submit the form.

After the file is uploaded to the page it will display the filename it just processed. We use this text to perform our assertion (making sure the uploaded file is what we expect).

NB! "Classpath" is a special folder in Java that (among others) contains all files from the folder src/test/resources.

That's why we store the file we want to upload to folder src/test/resources and call method uploadFromClasspath.

An Alternative test

If you want to avoid the term "classpath", or prefer to store test files in some other folders, you can call method uploadFile explicitly specifying file path.

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.
1-upload-a-file/code/selenide/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips-selenide-upload-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.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.