Uploading File using Selenium WebDriver in Java (#13947)

* Uploading File using Selenium WebDriver in Java

* Uploading File using Selenium WebDriver in Java

* Uploading File using Selenium WebDriver in Java

* Uploading File using Selenium WebDriver in Java
This commit is contained in:
Michael Olayemi 2023-05-08 03:38:08 +00:00 committed by GitHub
parent 65cf74f42f
commit 7f15d46d00
5 changed files with 114 additions and 0 deletions

View File

@ -40,6 +40,7 @@
<module>rest-assured</module> <module>rest-assured</module>
<module>rest-testing</module> <module>rest-testing</module>
<module>selenium-junit-testng</module> <module>selenium-junit-testng</module>
<module>selenium-webdriver</module>
<module>spring-mockito</module> <module>spring-mockito</module>
<module>spring-testing-2</module> <module>spring-testing-2</module>
<module>spring-testing</module> <module>spring-testing</module>

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -0,0 +1 @@
### Relevant Articles:

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>selenium-webdriver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>selenium-webdriver</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>testing-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-java.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<testng.version>6.10</testng.version>
<selenium-java.version>4.8.3</selenium-java.version>
<webdrivermanager.version>5.3.2</webdrivermanager.version>
</properties>
</project>

View File

@ -0,0 +1,53 @@
package com.baeldung.selenium.webdriver.fileupload;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class FileUploadWebDriverUnitTest {
private WebDriver driver;
private static final String URL = "http://www.csm-testcenter.org/test?do=show&subdo=common&test=file_upload";
private static final String INPUT_NAME = "file_upload";
@BeforeEach
public void setUp() {
WebDriverManager.firefoxdriver()
.setup();
driver = new FirefoxDriver();
}
@AfterEach
public void tearDown() {
driver.quit();
}
@Test
public void givenFileUploadPage_whenInputFilePath_thenFileUploadEndsWithFilename() {
driver.get(URL);
String filePath = System.getProperty("user.dir") + "/1688web.png";
WebElement inputElement = driver.findElement(By.name(INPUT_NAME));
WebElement submitButton = driver.findElement(By.name("http_submit"));
inputElement.sendKeys(filePath);
String actualFilePath = inputElement.getAttribute("value");
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
submitButton.click();
Assert.assertTrue(actualFilePath.endsWith(fileName));
}
}