In today’s fast-paced development cycles, ensuring that critical user workflows like login, registration, and form submissions function properly is essential. Manual testing can become time-consuming and error-prone when repeated across multiple releases. Selenium offers a robust framework to automate such validations with flexibility and language-agnostic support.
This blog explores how to use Selenium WebDriver to test a login form with field validations. It’s tailored for intermediate testers who understand basic testing principles and want to scale up their automation capabilities using Selenium.
Why Selenium?
Selenium is a well-established, open-source browser automation tool that supports multiple programming languages like Java, Python, C#, and JavaScript. It offers features like:
- Cross-browser support
- Flexible locators and element interaction
- Integration with frameworks like TestNG, JUnit, and NUnit
- Support for grid execution and cloud platforms
Selenium provides WebDriver, which controls the browser by simulating real user interactions.
Use Case: Login Form Validation
Assume the application under test has a login form with the following fields:
- Email (required, valid email format)
- Password (required, minimum 6 characters)
Sample HTML
<form id=”loginForm”>
<input type=”email” id=”email” required>
<input type=”password” id=”password” required minlenght=”6”
<button type=”submit”>Login</button>
</form>
Selenium Setup (Java + Maven Example)
Step 1: Add Dependencies
pom.xml
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>
Step 2: Basic WebDriver Code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get(“http://localhost:3000″);
WebElement email = driver.findElement(By.id(“email”));
WebElement password = driver.findElement(By.id(“password”));
WebElement submit = driver.findElement(By.cssSelector(“button[type=’submit’]”));
// Test case: Empty fields
submit.click();
System.out.println(“Validation messages should be shown.”);
// Test case: Invalid email
email.sendKeys(“invalid-email”);
password.sendKeys(“pass123”);
submit.click();
// Test case: Valid input
email.clear();
password.clear()
email.sendKeys(“user@example.com”);
password.sendKeys(“securePass123”);
submit.click();
// Validate navigation or success message
System.out.println(“Login attempted.”);
driver.quit();
}
}
Debugging & Enhancements
- Use logging libraries like Log4j for test output
- Use WebDriverWait for handling dynamic content
- Use Page Object Model (POM) to organize test structure
- Integrate Selenium with CI tools like Jenkins, GitHub Actions, or Azure Pipelines
Conclusion
Selenium continues to be a cornerstone of test automation frameworks across industries. With its flexibility, wide community support, and integration capabilities, it’s ideal for testers seeking scalable automation solutions.