Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

How To Automate Login Page Using Selenium WebDriver?

Shalini Baskaran

Posted On: February 11, 2022

view count409677 Views

Read time12 Min Read

Selenium has always been the most preferred test automation framework for testing web applications. This open-source framework supports popular programming languages (e.g. Java, JavaScript, Python, C#, etc.), browsers, and operating systems. It can also be integrated with other test automation frameworks like JUnit, TestNG, PyTest, PyUnit, amongst others. As per the State of open source testing survey, Selenium is still the king for web automation testing, with 81% of organizations preferring it over other frameworks.

When it comes to designing a responsive website, the most important feature is the login page. Login functionality plays a critical role from the security perspective of a user or customer. If the login feature acts up, the other website functionalities might not hold good.

In this blog on how to automate login page using Selenium WebDriver, we will take a look at different login mechanisms. Along with it, we will also deep dive into how to automate login page using Selenium WebDriver on a Selenium Grid cloud like LambdaTest for realizing Selenium automation testing.

Starting your journey with Selenium WebDriver? Check out this step-by-step guide to perform Automation testing using Selenium WebDriver.

So, let’s get started!

Top Authentication Mechanisms

There are different mechanisms used for login purposes. Some of them are mentioned below:

  • Password-based authentication
  • Multi-factor authentication
  • Token-based authentication
  • Certificate-based authentication

Based on its type, the implementation of the Login feature would differ. For example, the user would be asked to authenticate his/her identity in different ways to maintain a high-security level on some websites.

Of all these types, we have the latest authentication type called OAuth2.0 that is designed to allow a web page or application to access resources hosted by another web application on behalf of a user.

There may be multiple scenarios to be handled while testing web applications. However, out of those tests, the most important scenario that should be handled is the login scenario, without which nothing can be tested in the web application. Therefore, the login page has to be tested effectively to test the website (or application) from usability and security perspectives.

To test the login feature of any web application with password authentication, we need to fulfil the below requirements before writing the test case.

  1. Valid URL to test the web application
  2. Valid URL to test the web application

However, we will have different steps for handling different kinds of authentication.

  • For password-based authentication, a combination of username and password has to be passed in the test script
  • For multi-factor authentication, different steps for handling the authentication. Once the user has entered his/her credentials in some web pages, he would be prompted to respond to the push notification sent to the mobile. In such cases, we have to automate the steps for handling the notification.
  • In OAuth authentication, we need to provide various parameters like client ID, secret client ID, etc. Here, tests have to be designed based on the authentication mechanism used in the web application.

Here are a few scenarios that have to be covered as a part of testing login page using Selenium WebDriver:

  1. Testing with different data combinations like upper case, special character, maximum and minimum length and verifying the error message in case of incorrect usage
  2. Masking the password in the password field and verifying the password by clicking the Show Password option
  3. Verifying if the password is stored in an encrypted form
  4. Verifying if the user can copy and paste the password
  5. Verifying two-way authentication (or 2FA) through email and mobile in highly secured websites
  6. Verifying if a logged-in user lands on the login page when the same URL is copied & pasted in a new browser window

We have seen some high-level scenarios to be tested on the login page. In the next section of this article on how to automate login page using Selenium WebDriver, let us see how to implement and test the login functionality.

Login automation using Selenium WebDriver

Consider the below scenario to test the login feature of the LambdaTest web page.

Step 1: Launch the LambdaTest website. (https://www.lambdatest.com)

Step 2: Click Login.

Step 3: Enter a valid email and password.

Step 4: Click the Log in button.

Step 5: Verify the title of the page to ensure that the user has successfully logged into the website.

Now let’s see how to write our login test in Selenium WebDriver.

Also read – What is Selenium?

To begin with, we first need to specify the driver’s path and then instantiate the appropriate browser for that driver. Then, depending on our browser choice, we can instantiate the drivers.

Chrome

Firefox

Edge

Internet Explorer

To implement our test scenario, we have to identify four web elements:

  • The Login button in the main web page to navigate to the login page
  • Email
  • Password
  • Log in

This is how our web page looks upon launching the site.

So our first step would be identifying the Login element on the main page. To do that, right-click on the element and click Inspect.

Upon clicking Inspect, you could see the attributes displayed for the web element in the Elements tab.

We have eight different locators in Selenium WebDriver that are supported to locate the elements. They are

  • ID
  • Name
  • className
  • tagName
  • linkText
  • partialLinkText
  • CssSelector
  • XPath

We can use any one of the Selenium locators to identify the element in our script.

To identify the login option in the webpage, let us use the LinkText locator in our code and then perform a click action on that element.

Upon navigating to the login page, the user must enter the valid credentials in the Email and Password textbox.

There are also other options to log in to this web page, i.e., by clicking the Log in with Google and Log in with GitHub. Here, we will consider Log in via Google.

Upon clicking this you will be asked to log in through your Gmail account that is used in the device.

If you don’t have any accounts, you will have to provide your email and password, and then the notification will be sent to your mobile as it is the first time you have signed in through this device.

Now let’s proceed with the usual login with email and password.

To identify the Email and Password web elements, right-click and inspect them.

We can use id, name, className, or even XPath to identify this web element.

ID:

Name:

className:

XPath:

Once the web elements are identified, we can use the Selenium sendKeys() method to enter the values. But before entering the values, it is always recommended to clear the text fields, which is considered one of the best practices for Selenium test automation. To do that, we can use the clear() method.

Next, let us inspect the Password text box.

After entering the credentials, we have to click the Login button.

To do that, we have to click the Log in button and Inspect it to identify the Log in web element.

Now we have identified all the web elements and methods required to automate login page using Selenium WebDriver in the web application.

The final step is to get the web page’s title to ensure that the login has been successful and the user has been logged into the web page to perform further actions.

To get the title of the current web page, we can use the getTitle() method.

We can use the assertions class to compare the results.

So whenever the titles don’t match, we can handle them by throwing an error message, “The actual and expected title don’t match”.

And at last, when the test execution is completed, we have to close the browsers used for executing the tests.

You can close the browser by using two different Selenium WebDriver commands, these are:

  • close(): Closes the current browser window.
  • quit(): Quit all the instantiated browser instances.

Code Snippet to automate login page using Selenium WebDriver

To run the tests locally, right-click your class and click Run.

This certification is for anyone who wants to stay ahead among professionals who are growing their career in test automation using Selenium.

Here’s a short glimpse of the Selenium 101 certification from LambdaTest:

You can follow the LambdaTest YouTube Channel and stay updated with the latest tutorials around Selenium testing, Cypress testing, CI/CD, and more.

How to automate login page using Selenium Grid cloud?

With a cloud-based Selenium Grid like LambdaTest, you can now run your Selenium automated test cases on a scalable Selenium infrastructure running real browsers and operating systems. Selenium testing tools like LambdaTest allows you to perform cross browser testing on more than 3000+ online browsers and OS combinations.

To run our tests on Selenium Server, we must first ensure that the prerequisites are met.

The basic setup for running our tests are mentioned below.

Step 1. Create a LambdaTest account

To begin with, you need to create a LambdaTest Account. Upon successful account creation, you can sign in to your account and navigate to the LambdaTest Dashboard. Next, go to the LambdaTest Profile section under the Settings.

Here you can find your username and access token, which can be used in your tests to run them in the LambdaTest grid.

These credentials can be defined in your tests.

Step 2. Setting up Desired Capabilities

To run our tests in different browsers and operating systems, we must define the Desired Capabilities.

In our tests, I have mentioned the desired capabilities for running our tests in a chrome browser with version 93 in Windows 10 operating system.

Similarly, you can also define the desired capabilities for other browsers and operating systems. If you are not sure of writing your own desired capabilities, still LambdaTest extends its hand to assist you with it!

You can generate the desired capabilities of different browser and OS combinations using the LambdaTest Desired Capabilities Generator.

Code Snippet to automate login page using Selenium Grid cloud

TestNG.xml

In the LambdaTest dashboard, you can see the test which has run.

On further navigating inside, you can see the browser and operating system details on which the test had been run, and also, a video recording of the test execution has been attached for debugging purposes.

Also read – How To Handle Login Pop-up In Selenium WebDriver Using Java?

How to automate login page using Selenium WebDriver in Parallel?

LambdaTest supports more than 3000+ browsers and OS combinations that give the ability to perform Selenium automation testing in parallel on any of these combinations. The combinations can be defined in Desired Capabilities, and the rest all remain the same.
The Login feature has to be the same across the browsers and operating systems. Running our tests across different platforms will ensure that the functionality is the same across the platforms. The functionality and the user should have the same experience of using the feature on different platforms.

Also read – What Is Parallel Testing And Why Is It Important?

You can find the list of operating systems and list of browsers for running your tests in the grid. In addition to that, we can also run our tests in parallel on different platforms, which will be very effective in reducing Selenium test execution time.

Also read – Innovative Interfaces Uses LambdaTest To Realize Faster TTM By Reducing Test Execution Time

Let us try running the login tests in two different browsers – Chrome and Firefox.

You can also refer to the below video tutorial on how to handle Authentication Popup in Selenium.

Code Snippet to automate login page using Selenium WebDriver in parallel

TestNG.xml

In the TestNG.xml file, we defined the browser name to execute our tests in different browsers.

Execution

In the output console, you can see that the tests are executed in two different browsers.

In the LambdaTest dashboard, we can see the tests and the browser configuration in which they ran.

LambdaTest also enables the users with the freedom to get a clear view of their application’s performance and quality. You can now track the health of your automated test cases by going to LambdaTest Analytics under the Automation tab. This page allows you to see which tests have failed and lets you download a report for your records.

Finally..!

The login feature has always been considered the most important feature of any web application. Handling this feature is simple and pretty straightforward. While testing the login functionality, negative case testing contributes equally to the positive case scenario. In this article on how to automate login page using Selenium WebDriver, we have taken the LambaTest web page to test the Login functionality, and we have seen the way to automate the login functionality using Selenium WebDriver. We have also used cloud testing solutions to run the login test in different browsers to check the cross browser compatibility.

I hope this article on performing login automation using Selenium is really helpful in implementing a login automation scenario using the Selenium WebDriver. Feel free to provide your feedback in the comment section. Try your hands on this and keep exploring. Happy testing…!

Frequently Asked Questions (FAQs)

How does Selenium Webdriver verify successful login?

  1. Create an instance for Selenium WebDriver.
  2. Configure browser if needed.
  3. Navigate to the web page.
  4. Locate the desired web element.
  5. Perform some actions on the web element.
  6. Verify and validate the action.

How do I login to multiple users in Selenium?

  1. Create a 2D array list of multiple user IDs and passwords.
  2. Add process commands within 2 loops.
  3. Login.
  4. Submit something
  5. Logout the user through some command.

Author’s Profile

Shalini Baskaran

Shalini works as a Technical Writer at LambdaTest. She loves to explore recent trends in test automation. Other than test automation, Shalini is always up for travel & adventure.

Blogs: 23



linkedintwitter

Test your websites, web-apps or mobile apps seamlessly with LambdaTest.

  • Selenium, Cypress, Playwright & Puppeteer Testing
  • Real Devices Cloud
  • Native App Testing
  • Appium Testing
  • Live Interactive Testing
  • Smart Visual UI Testing
Book a Demo