Test Overview
This test automates uploading a file by sending the file path to a file input element. It verifies that the file input contains the correct file path after upload.
This test automates uploading a file by sending the file path to a file input element. It verifies that the file input contains the correct file path after upload.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class FileUploadTest { private WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); } @Test public void testFileUpload() { driver.get("https://example.com/upload"); WebElement fileInput = driver.findElement(By.id("file-upload")); String filePath = System.getProperty("user.dir") + "/src/test/resources/sample.txt"; fileInput.sendKeys(filePath); String uploadedFileName = fileInput.getAttribute("value"); assertTrue(uploadedFileName.contains("sample.txt"), "Uploaded file name should contain 'sample.txt'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test framework initializes and prepares to run the test | - | PASS |
| 2 | Browser opens Chrome and maximizes window | Chrome browser window is open and maximized | - | PASS |
| 3 | Navigates to 'https://example.com/upload' | Upload page is loaded with a file input element having id 'file-upload' | - | PASS |
| 4 | Finds file input element by id 'file-upload' | File input element is located on the page | - | PASS |
| 5 | Sends file path to file input element using sendKeys | File input element now contains the file path string | - | PASS |
| 6 | Retrieves the value attribute of the file input element | Value attribute contains the uploaded file path | Check that the value contains 'sample.txt' | PASS |
| 7 | Assertion checks if uploaded file name contains 'sample.txt' | Assertion verifies the file input value | assertTrue(uploadedFileName.contains("sample.txt")) | PASS |
| 8 | Test ends and browser closes | Browser is closed and resources are cleaned up | - | PASS |