0
0
Selenium Javatesting~15 mins

Submitting forms in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Submit a contact form and verify submission success
Preconditions (3)
Step 1: Enter 'John Doe' in the Name field
Step 2: Enter 'john.doe@example.com' in the Email field
Step 3: Enter 'Hello, this is a test message.' in the Message field
Step 4: Click the Submit button
Step 5: Wait for the success message element with id='successMsg' to be visible
✅ Expected Result: The success message 'Thank you for contacting us!' is displayed on the page
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Verify the success message element is displayed
Verify the success message text equals 'Thank you for contacting us!'
Best Practices:
Use explicit waits to wait for elements
Use By.id locators for form fields and buttons
Use Page Object Model to separate page interactions
Use TestNG assertions for validation
Automated Solution
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.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.time.Duration;

public class ContactFormTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver executable if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();
    }

    @Test
    public void testSubmitContactForm() {
        driver.get("https://example.com/contact");

        // Fill in the Name field
        WebElement nameField = wait.until(ExpectedConditions.elementToBeClickable(By.id("name")));
        nameField.clear();
        nameField.sendKeys("John Doe");

        // Fill in the Email field
        WebElement emailField = driver.findElement(By.id("email"));
        emailField.clear();
        emailField.sendKeys("john.doe@example.com");

        // Fill in the Message field
        WebElement messageField = driver.findElement(By.id("message"));
        messageField.clear();
        messageField.sendKeys("Hello, this is a test message.");

        // Click the Submit button
        WebElement submitButton = driver.findElement(By.id("submitBtn"));
        submitButton.click();

        // Wait for the success message to appear
        WebElement successMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("successMsg")));

        // Verify the success message is displayed
        Assert.assertTrue(successMsg.isDisplayed(), "Success message should be displayed");

        // Verify the success message text
        String expectedText = "Thank you for contacting us!";
        Assert.assertEquals(successMsg.getText().trim(), expectedText, "Success message text should match");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test uses Selenium WebDriver with Java and TestNG to automate submitting a contact form.

Setup: We create a ChromeDriver instance and a WebDriverWait for explicit waits.

Test steps: We open the contact page URL, then locate each form field by its id. We clear any existing text and enter the test data exactly as specified.

We click the submit button, then wait explicitly for the success message element to become visible.

Assertions: We check that the success message is displayed and that its text matches the expected message.

Teardown: We close the browser after the test.

This approach uses explicit waits to avoid timing issues, uses clear locators by id, and uses TestNG assertions for clear validation messages.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not clearing input fields before sending keys
Not quitting the WebDriver after tests
Bonus Challenge

Now add data-driven testing with 3 different sets of form inputs (name, email, message) using TestNG data provider.

Show Hint