How To Write Automation Script For Login Page In Selenium

As a technical enthusiast and someone who values efficiency, I have always been fascinated by automation. One area where automation can significantly improve our daily tasks is in website testing. Selenium, a widely-used open-source framework, provides a powerful and flexible platform for automating browser actions. In this article, I will guide you through the process of writing an automation script for a login page using Selenium. So, grab a cup of coffee and let’s dive into the exciting world of scripting!

Setting Up the Environment

Before we start writing our automation script, we need to ensure that we have the necessary tools set up. Here’s what you’ll need:

  1. Selenium WebDriver: You can download the required WebDriver for your preferred browser, such as Chrome, Firefox, or Safari.
  2. An Integrated Development Environment (IDE) like Eclipse, IntelliJ, or Visual Studio Code.
  3. A programming language of your choice. Selenium supports multiple languages, including Java, Python, C#, and Ruby. For this article, we’ll be using Java.
  4. Maven (optional): This build automation tool can help manage dependencies for your project. It simplifies the process of adding and updating libraries.

Writing the Automation Script

Now that we have our environment set up, let’s start writing our automation script. Here’s a step-by-step guide:

Step 1: Create a New Java Class

In your IDE, create a new Java class. You can name it something like “LoginAutomation” or any name that suits your project.

Step 2: Import the Necessary Libraries

Import the required Selenium WebDriver libraries at the beginning of your code. These libraries will provide the necessary functionality for interacting with the browser.


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

Step 3: Set Up WebDriver

Next, we need to configure the WebDriver to use the browser of our choice. Let’s set up the WebDriver for Chrome:


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

Step 4: Navigate to the Login Page

Use the WebDriver’s get() method to navigate to the login page.


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

Step 5: Find and Interact with Login Elements

Now, let’s find the login elements on the page and interact with them. For example, let’s assume we have text input fields for the username and password, and a submit button to log in.


WebElement usernameInput = driver.findElement(By.id("username"));
WebElement passwordInput = driver.findElement(By.id("password"));
WebElement submitButton = driver.findElement(By.id("submit"));

usernameInput.sendKeys("myUsername");
passwordInput.sendKeys("myPassword");
submitButton.click();

Step 6: Verify the Login

To ensure that the login was successful, we can perform some verification checks. For example, we can check if a specific element is present on the page after logging in.


WebElement welcomeMessage = driver.findElement(By.id("welcome"));
if (welcomeMessage.isDisplayed()) {
    System.out.println("Login successful!");
} else {
    System.out.println("Login failed!");
}

Conclusion

Automating the login process using Selenium can save us time and effort. By following the steps outlined in this article, you can easily create an automation script for any login page. Remember to customize the script according to the specific elements and actions on your target page. Happy scripting!