Ex.
No: 3 Acceptance Testing using Selenium
AIM:
To perform acceptance testing using Selenium to validate the functionality and
user experience of a web application.
TOOLS REQUIRED:
• Selenium: A web automation tool used for testing web applications by
automating browser actions.
• WebDriver Manager: A library to automatically manage browser drivers
for Selenium.
• Google Chrome: A web browser used for running the Selenium tests.
INSTALLATION:
1. Install Selenium:
o Use the following pip command:
pip install selenium
2. Install WebDriver Manager:
o Use the following pip command:
pip install webdriver-manager
3. Install Google Chrome:
o Download and install Google Chrome from the official website.
SELENIUM SETUP:
Type python in the command prompt to check if Python is properly installed.
SOURCE CODE:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
# Set up the WebDriver
driver =
webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
try:
# Open the login page
driver.get("https://the-internet.herokuapp.com/login")
# Locate the username and password fields
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.ID, "password")
# Enter the credentials
entered_username = "tomsmith"
entered_password = "SuperSecretPassword!"
username_field.send_keys(entered_username)
password_field.send_keys(entered_password)
# Submit the form
login_button = driver.find_element(By.CSS_SELECTOR,
"button[type='submit']")
login_button.click()
# Check if the username is 'tomsmith' before verifying login success
if entered_username == "harish":
success_message = driver.find_element(By.CSS_SELECTOR,
".flash.success")
assert "You logged into a secure area!" in success_message.text
# Close the browser on successful login
driver.quit()
except Exception as e:
# Print the exception if any occurs
print(f"An error occurred: {e}")
SELENIUM COMMANDS:
1. Opening the Login Page:
• Command: driver.get("https://the-internet.herokuapp.com/login")
2. Locating Fields:
• Username Field: driver.find_element(By.ID, "username")
• Password Field: driver.find_element(By.ID, "password")
3. Entering Credentials:
• Username: "tomsmith"
• Password: "SuperSecretPassword!"
4. Submitting the Form:
• Login Button: driver.find_element(By.CSS_SELECTOR,
"button[type='submit']")
5. Verifying Login Success:
• Success Message: driver.find_element(By.CSS_SELECTOR,
".flash.success")
• Assertion: assert "You logged into a secure area!" in
success_message.text
6. Closing the Browser:
• Command: driver.quit()
Sample I/O:
Input:
Username: tomsmith
Password: SuperSecretPassword!
Output:
You logged into a secure area!