0
0
Selenium Javatesting~10 mins

Submitting forms in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code
Selenium Java
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();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Navigates to https://example.com/contactContact form page is loaded with input fields and submit buttonPage loaded with form elements presentPASS
3Finds the name input field by id 'name' and enters 'John Doe'Name field contains text 'John Doe'Name input field is present and text enteredPASS
4Finds 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 enteredPASS
5Finds the message textarea by id 'message' and enters the test messageMessage field contains the test message textMessage input field is present and text enteredPASS
6Finds the submit button by id 'submit-btn' and clicks itForm is submitted, page processes submissionSubmit button is clickable and clickedPASS
7Waits for confirmation message with id 'confirmation' to be visibleConfirmation message is displayed on the pageConfirmation message text equals 'Thank you for your message!'PASS
8Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The confirmation message element with id 'confirmation' does not appear after form submission
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after clicking the submit button?
AImmediately closes the browser
BRefreshes the page
CWaits for the confirmation message to appear
DClears all input fields
Key Result
Always wait explicitly for elements that appear after an action like form submission to ensure the test checks the correct page state.