Day 3 of 100daysofcode : Testing Login Functionality with Selenium
Test Objective:
Test the login functionality on the “Form Authentication” page.
Website URL:
https://the-internet.herokuapp.com/login
Python Selenium Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Set up the WebDriver (use your appropriate driver like ChromeDriver, GeckoDriver, etc.)
driver = webdriver.Chrome() # Make sure you have installed the appropriate driver
# Navigate to the website
driver.get("https://the-internet.herokuapp.com/login")
# Locate the username and password fields and enter values
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.ID, "password")
username_field.send_keys("tomsmith") # Valid username
password_field.send_keys("SuperSecretPassword!") # Valid password
# Locate and click the login button
login_button = driver.find_element(By.CSS_SELECTOR, "button.radius")
login_button.click()
# Verify successful login
success_message = driver.find_element(By.CSS_SELECTOR, ".flash.success")
assert "You logged into a secure area!" in success_message.text
print("logged in successfully")
# Close the browser
#driver.quit()
How It Works:
- Navigate to the Login Page: The script opens the login page of the website.
- Enter Credentials: It fills in the username (
tomsmith
) and password (SuperSecretPassword!
), which are valid test credentials provided by the website. - Click Login: The login button is clicked to submit the form.
- Verify Success: It checks if the success message is displayed after login.
- Close Browser: The browser session is terminated.