How To Test A Login Page In Selenium

How To Articles

Testing a login page is an essential part of any software testing process. As a quality assurance engineer, I have had my fair share of experiences testing login pages using Selenium. In this article, I will share with you my insights and techniques on how to effectively test a login page using Selenium.

Understanding the Login Page

Before diving into the testing process, it is important to have a good understanding of the login page itself. A typical login page consists of two input fields: one for the username and another for the password. Additionally, there is usually a “Login” button that triggers the authentication process. It is important to identify these elements in the HTML code of the login page, as we will need to interact with them during the testing process.

Setting Up Selenium

To begin testing the login page, we need to set up Selenium. First, make sure you have the Selenium WebDriver library installed. You can download it from the official Selenium website. Once installed, import the necessary classes and set up a WebDriver instance to interact with the browser. You can choose from various browsers such as Chrome, Firefox, or Safari. For this example, let’s use Chrome:

from selenium import webdriver

# Set up Chrome WebDriver instance
driver = webdriver.Chrome()

Writing Test Cases

Once the Selenium setup is complete, we can start writing test cases for the login page. Test cases are a set of steps that verify the expected behavior of the login page. Let’s start with a simple test case that verifies successful login using valid credentials:

def test_successful_login():
# Open the login page

# Enter valid username and password

# Click the login button

# Verify successful login by checking the presence of a welcome message
assert 'Welcome' in driver.page_source

In the above test case, we first navigate to the login page using the `get()` method of the WebDriver instance. Then, we locate the username and password input fields using their respective IDs and enter valid credentials. Finally, we click the login button and verify the presence of a welcome message on the resulting page.

Handling Invalid Login Attempts

Testing for invalid login attempts is equally important. Let’s write another test case that verifies the correct behavior when invalid credentials are entered:

def test_invalid_login():
# Open the login page

# Enter invalid username and password

# Click the login button

# Verify error message for invalid login
error_message = driver.find_element_by_id('error-message').text
assert 'Invalid credentials' in error_message

In this test case, we simulate an invalid login attempt by entering incorrect credentials. We then click the login button and verify the presence of an error message indicating that the login attempt was unsuccessful.


Testing a login page in Selenium is a crucial step in ensuring the security and functionality of web applications. By setting up Selenium, writing test cases, and handling both successful and unsuccessful login attempts, we can thoroughly test the login page and identify any potential issues. Remember to always consider different scenarios and edge cases when testing. Happy testing!