Login Page Selenium Java Code

Java Programming

Being a technical writer, I frequently come across the need to use different automation frameworks to simplify repetitive tasks. My preferred framework is Selenium, an efficient tool that enables me to automate web browsers for testing and web scraping. In this article, I will lead you through the steps of developing a login page automation script using Selenium and Java.

Before we dive into the code, it’s essential to understand the prerequisites for implementing this automation script. Make sure you have the following:

  • Java Development Kit (JDK) installed on your machine
  • Integrated Development Environment (IDE) such as Eclipse or IntelliJ IDEA
  • Selenium WebDriver Java bindings
  • ChromeDriver or any other browser driver

Once you have everything set up, you can begin writing the login page automation code. Let’s start by importing the necessary libraries:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

Next, we need to set up the WebDriver and specify the path to the browser driver:


System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

Now, we can navigate to the login page using the get() method:


String loginPageURL = "https://www.example.com/login";
driver.get(loginPageURL);

Once we are on the login page, we need to locate the username and password input fields and enter our credentials. We can achieve this using the findElement() method along with the By class:


String username = "myUsername";
String password = "myPassword";

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

After entering the credentials, we can submit the login form by locating the submit button and using the click() method:


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

At this point, the script will attempt to log in with the provided credentials. To validate whether the login was successful or not, we can add an assertion to check if a specific element or text exists on the page:


if (driver.getPageSource().contains("Welcome, " + username)) {
System.out.println("Login successful!");
} else {
System.out.println("Login failed!");
}

Finally, we need to clean up and close the browser driver:


driver.quit();

With the code in place, you can now run your login page automation script. Make sure to replace the placeholder values with your own login page URL, username, and password. You can also enhance the script by incorporating error handling, such as capturing and logging any exceptions that occur during the automation process.

Conclusion

Automating login pages using Selenium and Java can save you a significant amount of time and effort, especially when dealing with multiple websites or test scenarios. By following the steps outlined in this article, you can create a robust automation script that navigates to a login page, enters credentials, and validates the login success.

To further explore the capabilities of Selenium and Java automation, I encourage you to experiment with other web elements and interactions beyond the login page. Happy coding!