Automating Login Page with Selenium Java

 Automating a login page is one of the most common and essential tasks in Selenium test automation. It helps testers verify that the authentication mechanism of an application works correctly under different conditions. Using Selenium with Java, we can automate the process of opening a web browser, navigating to the login page, entering credentials, and verifying the outcome. Here’s a step-by-step guide to automating a login page using Selenium WebDriver and Java.

1. Set Up the Environment

Before writing the script, make sure you have the following tools installed:

Java JDK

Eclipse or IntelliJ IDEA

Selenium WebDriver

Browser Driver (e.g., ChromeDriver for Google Chrome)

Add the Selenium JAR files to your project build path to use Selenium classes.

2. Identify the Web Elements

Use the browser’s Developer Tools (F12) to inspect and locate elements on the login page, such as:

Username input field

Password input field

Login button

You can locate elements using locators like id, name, className, xpath, or cssSelector.

3. Sample Code to Automate Login

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) {

        // Set path to ChromeDriver

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Launch browser

        WebDriver driver = new ChromeDriver();

        driver.get("https://example.com/login");

        // Maximize window

        driver.manage().window().maximize();

        // Locate and enter username

        WebElement username = driver.findElement(By.id("username"));

        username.sendKeys("yourUsername");

        // Locate and enter password

        WebElement password = driver.findElement(By.id("password"));

        password.sendKeys("yourPassword");

        // Click on login button

        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        loginButton.click();

        // Optional: Validate login success

        if (driver.getCurrentUrl().equals("https://example.com/dashboard")) {

            System.out.println("Login successful");

        } else {

            System.out.println("Login failed");

        }

        // Close browser

        driver.quit();

    }

}

4. Best Practices

Use Page Object Model (POM) to improve reusability and maintenance.

Avoid hardcoding credentials; use properties files or environment variables.

Add explicit waits to handle dynamic elements.

Conclusion

Automating a login page using Selenium with Java is a foundational step toward mastering web test automation. With proper element identification, structure, and validations, you can build reliable login test scripts that ensure your application’s authentication is working as expected.

Learn Selenium with Java Training Course

Read more

Locating Elements with XPath in Java

Handling Buttons and Textboxes in Selenium

Handling Radio Buttons and Checkboxes

Using Implicit Waits in Selenium Java

Visit our Quality Thought Training Institute

Comments

Popular posts from this blog

Understanding the useEffect Hook

What Is Tosca? A Beginner’s Guide

Exception Handling in Java