Sample Code For Login Page In Selenium Webdriver Java

Java Programming

As a software engineer, I frequently encounter projects that involve automating web tasks. A typical task is automating the login procedure for web apps. In this article, I will guide you through a sample code for a login page utilizing Selenium WebDriver in Java. I will share my own perspectives and offer thorough explanations throughout the process.

Setting up the Environment

Before we dive into the code, let’s make sure we have the necessary tools and libraries installed:

  1. Java Development Kit (JDK): Install the latest version of JDK from the official Oracle website.
  2. Eclipse IDE: Download and install Eclipse IDE, a popular Java development environment.
  3. Selenium WebDriver: Add the Selenium WebDriver library to your Java project. You can download it from the official Selenium website or add it as a Maven dependency.

Creating the Login Page

First, let’s create a new Java class called “LoginPage.”


public class LoginPage {
    public static void main(String[] args) {
        // WebDriver setup and browser initialization code
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.example.com/login");
    }
}

In the code above, we create a new WebDriver object and initialize it with the ChromeDriver. We then maximize the browser window and navigate to the login page URL.

Filling in the form

Now, let’s fill in the username and password fields on the login page.


driver.findElement(By.id("username")).sendKeys("myusername");
driver.findElement(By.id("password")).sendKeys("mypassword");

In the code snippet above, we use the findElement method to locate the username and password fields on the page using their respective IDs. We then use the sendKeys method to enter our desired username and password values.

Submitting the form

After filling in the form, we need to submit it to complete the login process.


driver.findElement(By.id("loginButton")).click();

In the code snippet above, we use the findElement method to locate the login button on the page using its ID. We then use the click method to simulate a button click and submit the form.

Verifying Successful Login

Finally, we can verify whether the login was successful by checking if the user is redirected to the homepage.


String currentURL = driver.getCurrentUrl();
if (currentURL.equals("https://www.example.com/home")) {
    System.out.println("Login successful!");
} else {
    System.out.println("Login failed!");
}

In the code snippet above, we use the getCurrentUrl method to retrieve the current URL after submitting the form. We then compare it to the expected URL of the homepage. If they match, we print “Login successful!” to indicate a successful login; otherwise, we print “Login failed!” to indicate a failed login.

Conclusion

Automating the login process for a web application can greatly enhance our productivity as software engineers. In this article, we explored a sample code for a login page using Selenium WebDriver in Java. We discussed how to set up the environment, create the login page, fill in the form, submit the form, and verify the login. Remember, this code is just a starting point, and you can customize it to fit your specific needs and requirements. Happy coding!