YouTip LogoYouTip

Selenium Practical

In modern software development, automated testing has become one of the important means to ensure software quality.

\\n

Selenium is a widely used automated testing tool that supports multiple programming languages and browsers, and can simulate user operations for functional testing, regression testing, and more.

\\n

This article will provide a detailed introduction on how to use Selenium for automated testing, and demonstrate through practical projects how to implement automated testing for common functions such as login, registration, and search (using Baidu as an example).

\\n

1. Practical Project: Automated Testing Cases

\\n

1.1 Login Function Testing

\\n

Assume we have a login page containing username and password input fields and a login button. We will write an automated test script to simulate user login operations.

\\n

Example

\\n
from selenium import webdriver\\n\\nfrom selenium.webdriver.chrome.service import Service as ChromeService\\n\\nfrom selenium.webdriver.common.by import By\\n\\nfrom selenium.webdriver.common.keys import Keys\\n\\nchrome_options = Options()\\n\\n# Set correct driver path\\n\\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\\n\\n driver = webdriver.Chrome(service=service, options=chrome_options)\\n\\n# Open the login page\\n\\n driver.get("https://example.com/login")\\n\\n# Locate the username input field and enter the username\\n\\n username_input = driver.find_element(By.ID,"username")\\n\\n username_input.send_keys("testuser")\\n\\n# Locate the password input field and enter the password\\n\\n password_input = driver.find_element(By.ID,"password")\\n\\n password_input.send_keys("password123")\\n\\n# Locate the login button and click it\\n\\n login_button = driver.find_element(By.ID,"login-button")\\n\\n login_button.click()\\n\\n# Verify if login is successful\\n\\nassert"Welcome"in driver.page_source\\n\\n# Close browser\\n\\n driver.quit()\\n
\\n

1.2 Registration Function Testing

\\n

Next, we will write an automated test script to simulate user registration operations.

\\n

Example

\\n
from selenium import webdriver\\n\\nfrom selenium.webdriver.chrome.service import Service as ChromeService\\n\\nfrom selenium.webdriver.common.by import By\\n\\n# Configure Chrome options\\n\\n chrome_options = Options()\\n\\n# Set correct driver path\\n\\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\\n\\n driver = webdriver.Chrome(service=service, options=chrome_options)\\n\\n# Open the registration page\\n\\n driver.get("https://example.com/register")\\n\\n# Locate the username input field and enter the username\\n\\n username_input = driver.find_element(By.ID,"username")\\n\\n username_input.send_keys("newuser")\\n\\n# Locate the email input field and enter the email\\n\\n email_input = driver.find_element(By.ID,"email")\\n\\n email_input.send_keys("newuser@example.com")\\n\\n# Locate the password input field and enter the password\\n\\n password_input = driver.find_element(By.ID,"password")\\n\\n password_input.send_keys("newpassword123")\\n\\n# Locate the confirm password input field and enter the password\\n\\n confirm_password_input = driver.find_element(By.ID,"confirm-password")\\n\\n confirm_password_input.send_keys("newpassword123")\\n\\n# Locate the registration button and click it\\n\\n register_button = driver.find_element(By.ID,"register-button")\\n\\n register_button.click()\\n\\n# Verify if registration is successful\\n\\nassert"Registration successful"in driver.page_source\\n\\n# Close browser\\n\\n driver.quit()\\n
\\n

1.3 Search Function Testing (Baidu)

\\n

Finally, we will wr

← Python Sum NumbersSelenium Advanced β†’