Test Overview
This test opens a web page with a form, fills in the input fields, submits the form, and verifies that the submission was successful by checking the confirmation message.
This test opens a web page with a form, fills in the input fields, submits the form, and verifies that the submission was successful by checking the confirmation message.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; 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.assertEquals; public class FormSubmitTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10)); } @Test public void testFormSubmission() { driver.get("https://example.com/contact"); WebElement nameInput = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("name"))); nameInput.sendKeys("John Doe"); WebElement emailInput = driver.findElement(By.id("email")); emailInput.sendKeys("john@example.com"); WebElement messageInput = driver.findElement(By.id("message")); messageInput.sendKeys("Hello, this is a test message."); WebElement submitButton = driver.findElement(By.id("submit-btn")); submitButton.click(); WebElement confirmation = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmation"))); String confirmationText = confirmation.getText(); assertEquals("Thank you for your message!", confirmationText); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | - | PASS |
| 2 | Navigates to https://example.com/contact | Contact form page is loaded with input fields and submit button | Page loaded with form elements present | PASS |
| 3 | Finds the name input field by id 'name' and enters 'John Doe' | Name field contains text 'John Doe' | Name input field is present and text entered | PASS |
| 4 | Finds the email input field by id 'email' and enters 'john@example.com' | Email field contains text 'john@example.com' | Email input field is present and text entered | PASS |
| 5 | Finds the message textarea by id 'message' and enters the test message | Message field contains the test message text | Message input field is present and text entered | PASS |
| 6 | Finds the submit button by id 'submit-btn' and clicks it | Form is submitted, page processes submission | Submit button is clickable and clicked | PASS |
| 7 | Waits for confirmation message with id 'confirmation' to be visible | Confirmation message is displayed on the page | Confirmation message text equals 'Thank you for your message!' | PASS |
| 8 | Test ends and browser closes | Browser window is closed | - | PASS |