Login automation using Selenium Webdriver: Tutorial
By Neha Vaidya, Community Contributor - February 10, 2023
Execution of automation testing requires a comprehensive understanding of numerous automation tools and frameworks. Among these tools, Selenium is the most popular due to its ease of use and relevant features. Selenium is used to automate tests on websites to monitor their performance.
A fundamental feature of many websites is their login function. Automating the testing of this feature is simple enough when using Selenium WebDriver, as this article will demonstrate.
Let’s get started.
Selenium WebDriver allows testers to choose a programming language to create test scripts. It cannot handle window components, but this drawback can be overcome by using tools like Sikuli, Auto IT, etc.
Bear in mind that Selenium tests must be run on real browsers and devices for accurate results. Start running tests on 3000+ real browsers and devices on BrowserStack’s real device cloud. Run parallel tests on a Cloud Selenium Grid to get faster results without compromising on accuracy. Testers can also conduct Cypress testing on 30+ real browser versions across Windows and macOS. Detect bugs before users do by testing software in real user conditions with BrowserStack.
Run Selenium Tests on Real Browsers Free
Prerequisites for Login Automation using Selenium Webdriver
- Download and Install JDK(Java Development Kit)
- Install Eclipse from the official website
- Download the Selenium Java Client version
- Configure the drivers depending on the browser. The example here will be using a chrome driver for Chrome
Read More: How to configure Selenium in Eclipse
Steps for Login Automation using Selenium WebDriver
Before performing automation testing for the login functionality, there are a couple of basic steps that need to be followed for the test case to be written:
- Create a Selenium WebDriver instance
- Configure browser if required
- Navigate to the required web page
- Locate the relevant web element
- Perform action on the web element
- Verify and validate the action
Now let’s walk through each of these steps in detail.
1. Create a Selenium WebDriver instance
To launch the website in the desired browser, set the system properties to the path of the driver for the required browser. This example will use ChromeDriver for Login Automation using Selenium Webdriver. The syntax for the same will be:
Webdriver driver = new ChromeDriver(); System.setProperty("webdriver.chrome.driver", "Path of the chrome driver");
2. Configure the Web browser
Usually, the web page will be in a minimized format when the test case is run. Maximize the browser for a clear picture of the test cases executed. Use the command below to do the same:
driver.manage.window.maximize();
3. Navigate to the web URL
Open the browser with the desired URL. Use the command below to open the URL in the desired instantiated browser:
driver.get("https://www.browserstack.com/users/sign_in");
4. Locating the Web Element
Locators are an essential part of every Selenium script as they identify the elements that the test script will interact with to replicate user actions.
For example, let’s try to locate the email and password field of the login form of Browserstack sign in page.
Inspect the email field to locate the element using the ID locator, as shown in the image below:
Locate it via the ID locator in Selenium WebDriver:
driver.findElement(By.id("user_email_login"));
Since this returns a webelement, store it in webelement variable with:
WebElement username=driver.findElement(By.id("user_email_login"));
Read More: findElement vs findElements in Selenium
Repeat the same steps for the password field.
driver.findElement(By.id("user_password")); WebElement password=driver.findElement(By.id("user_password"));
5. Perform Action on the Located Web Element
After locating the element, testers need to perform the desired action. In this case, the action is entering text in the email and password fields and hitting the login button. For this, the script uses sendKeys and click methods, as shown below:
username.sendKeys("abc@gmail.com"); password.sendKeys("your_password"); login.click();
Read More: How to perform Double Click in Selenium
6. Verify & Validate The Action
To validate the results, use assertion. Assertions are important for comparing the expected results to the actual results. If it matches, the test case passes. If not, then the test case fails. The syntax below will help to assert (validate) the outcome from actions by performing login automation:
Assert.assertEquals(String actual, String expected);
Save the actual URL post-login into a string value, which is:
String actualUrl="https://www.browserstack.com/users/sign_in";
The Expected URL can be identified by using the method below:
String expectedUrl= driver.getCurrentUrl();
The final assertion would look like:
Assert.assertEquals(actualUrl, expectedUrl);
If the test case is passed, it will retrieve the same. Else it will return as failed.
Given below is the full selenium code for automating login page in chrome using Selenium WebDriver:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class LoginAutomation { @Test public void login() { System.setProperty("webdriver.chrome.driver", "path of driver"); WebDriver driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.browserstack.com/users/sign_in"); WebElement username=driver.findElement(By.id("user_email_Login")); WebElement password=driver.findElement(By.id("user_password")); WebElement login=driver.findElement(By.name("commit")); username.sendKeys("abc@gmail.com"); password.sendKeys("your_password"); login.click(); String actualUrl="https://live.browserstack.com/dashboard"; String expectedUrl= driver.getCurrentUrl(); Assert.assertEquals(expectedUrl,actualUrl); } }
On executing the code, Selenium will navigate to the Chrome browser and open the BrowserStack login page. Then, it will log in using the relevant credentials. It will also check the test case status using Assert and try to match the URL.
Follow the steps and protocol detailed above to automate the login function of websites with Selenium. Remember to leverage the power of real browsers and devices along with Selenium’s many abilities to create immediately effective test scripts that generate desirable results with minimal time and effort.
It is always recommended to run Selenium Tests on real devices to take real user conditions into account and ensure better accuracy. BrowserStack Real Device Cloud provides access to 3000+ real devices and browsers for a seamless and comprehensive testing experience. You can also run multiple tests on different browser-device combinations with parallel testing using BrowserStack Cloud Selenium Grid.