Skip to main content

_java

TL;DR - Show Me The Code

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;

public class UploadTest {
WebDriver driver;

@BeforeEach
public void beforeEach() {
driver = new FirefoxDriver();
}

@AfterEach
public void afterEach() {
if (driver != null) {
driver.quit();
}
}

@Test
public void uploadFile() throws Exception {
String fileName = "some-file.txt";
File file = new File(fileName);
String path = file.getAbsolutePath();
driver.get("https://the-internet.herokuapp.com/upload");
driver.findElement(By.id("file-upload")).sendKeys(path);
driver.findElement(By.id("file-submit")).click();
String text = driver.findElement(By.id("uploaded-files")).getText();
Assertions.assertEquals(fileName, text);
}

}

Code Walkthrough

Importing Libraries

Lines 3 to 10 are pulling in our requisite classes for annotations (e.g., org.junit.jupiter.api.BeforeEach, etc.), driving the browser with Selenium (e.g., org.openqa.selenium.WebDriver, etc.), matchers for assertions (e.g., org.junit.jupiter.api.Assertions;, etc. ), and something to handle local files (e.g., java.io.File).

Setup and Teardown

Lines 15 to 25 are setting up and tearing down the browser instance. The beforeEach method, will execute before each test. In it, we are launching a new instance of Firefox with Selenium and storing it in a class variable that we'll use throughout all tests. After our test executes the second method, afterEach, will execute. This calls driver.quit() which ends the session by closing the browser instance.

The Test

Lines 29 to 36 are the test itself.

We create an uploadFile() method and annotate it with @Test so it is run as a test. In it, we create a new file called some-file.txt in the present working directory and get it's absolute path.

Next we visit the page with the upload form, input the string value of path (e.g., the full path to the file plus the filename with it's extension), 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).

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/java/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips</artifactId>
<version>1.0.0</version>

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

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

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