Selenium Practical Project
In modern software development, automated testing has become one of the important means of ensuring software quality.
Selenium is a widely used automated testing tool that supports multiple programming languages and browsers, and can simulate user operations to perform functional testing, regression testing, and more.
This article will detail how to use Selenium for automated testing, and use practical projects to demonstrate how to implement automated testing of common functions such as login, registration, and search (taking Baidu as an example).
1. Practical Project: Automated Testing Case
1.1 Login Function Test
Suppose we have a login page that includes a username input field, a password input field, and a login button. We will write an automated test script to simulate the user login operation.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_options = Options()
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open the login page
driver.get("https://example.com/login")
# Locate the username input field and enter the username
username_input = driver.find_element(By.ID, "username")
username_input.send_keys("testuser")
# Locate the password input field and enter the password
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("password123")
# Locate the login button and click it
login_button = driver.find_element(By.ID, "login-button")
login_button.click()
# Verify whether login is successful
assert "Welcome" in driver.page_source
# Close the browser
driver.quit()
1.2 Registration Function Test
Next, we write an automated test script to simulate the user registration operation.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Configure Chrome options
chrome_options = Options()
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open the registration page
driver.get("https://example.com/register")
# Locate the username input field and enter the username
username_input = driver.find_element(By.ID, "username")
username_input.send_keys("newuser")
# Locate the email input field and enter the email
email_input = driver.find_element(By.ID, "email")
email_input.send_keys("newuser@example.com")
# Locate the password input field and enter the password
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("newpassword123")
# Locate the confirm password input field and enter the password
confirm_password_input = driver.find_element(By.ID, "confirm-password")
confirm_password_input.send_keys("newpassword123")
# Locate the registration button and click it
register_button = driver.find_element(By.ID, "register-button")
register_button.click()
# Verify whether registration is successful
assert "Registration successful" in driver.page_source
# Close the browser
driver.quit()
1.3 Search Function Test (Baidu)
Finally, we write an automated test script to simulate the user entering a keyword in the Baidu search box and performing a search.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Configure Chrome options
chrome_options = Options()
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open the Baidu homepage
driver.get("https://www.baidu.com")
# Locate the search box and enter a keyword
search_box = driver.find_element(By.NAME, "wd")
search_box.send_keys("example")
# Simulate pressing the Enter key to search
search_box.send_keys(Keys.RETURN)
# Verify whether the search results page contains the keyword
assert "example" in driver.page_source
# Close the browser
driver.quit()