2025-05-22 14:18:30 +08:00
|
|
|
import pytest
|
|
|
|
from selenium import webdriver
|
2025-04-17 01:32:45 +08:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
2025-05-22 14:18:30 +08:00
|
|
|
from webdriver_manager.chrome import ChromeDriverManager
|
|
|
|
|
|
|
|
# 配置测试环境
|
|
|
|
BASE_URL = "http://localhost:8000" # 替换为您的应用URL
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def driver():
|
|
|
|
# 使用 Chrome 浏览器并自动管理驱动
|
|
|
|
options = webdriver.ChromeOptions()
|
|
|
|
options.add_argument("--headless") # 无头模式,不显示浏览器窗口
|
|
|
|
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
|
|
|
|
yield driver
|
|
|
|
driver.quit() # 测试结束后关闭浏览器
|
|
|
|
|
|
|
|
|
|
|
|
# 测试用户名不能包含中文
|
|
|
|
def test_username_cannot_contain_chinese(driver):
|
|
|
|
# 打开注册页面
|
|
|
|
driver.get(f"{BASE_URL}/signup")
|
|
|
|
|
|
|
|
# 填写用户名(包含中文)
|
|
|
|
username = WebDriverWait(driver, 10).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "username"))
|
|
|
|
)
|
|
|
|
username.send_keys("测试用户")
|
|
|
|
|
|
|
|
# 填写有效密码
|
|
|
|
password = WebDriverWait(driver, 10).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "password"))
|
|
|
|
)
|
|
|
|
password.send_keys("ValidPassword123!")
|
|
|
|
|
|
|
|
# 确认密码
|
|
|
|
confirm_password = WebDriverWait(driver, 10).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "password2"))
|
|
|
|
)
|
|
|
|
confirm_password.send_keys("ValidPassword123!")
|
|
|
|
|
|
|
|
# 点击注册按钮
|
|
|
|
register_button = WebDriverWait(driver, 10).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "注册")]'))
|
|
|
|
)
|
|
|
|
register_button.click()
|
|
|
|
|
|
|
|
# 验证警告提示
|
|
|
|
alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
|
|
|
|
assert "中文" in alert.text or "Chinese" in alert.text, "未显示用户名中文限制提示"
|
|
|
|
alert.accept()
|
|
|
|
|
|
|
|
|
|
|
|
# 测试密码不能包含中文
|
|
|
|
def test_password_cannot_contain_chinese(driver):
|
|
|
|
# 打开注册页面
|
|
|
|
driver.get(f"{BASE_URL}/signup")
|
|
|
|
|
|
|
|
# 填写有效用户名
|
|
|
|
username = WebDriverWait(driver, 10).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "username"))
|
|
|
|
)
|
|
|
|
username.send_keys("validuser123")
|
|
|
|
|
|
|
|
# 填写密码(包含中文)
|
|
|
|
password = WebDriverWait(driver, 10).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "password"))
|
|
|
|
)
|
|
|
|
password.send_keys("测试密码")
|
|
|
|
|
|
|
|
# 确认密码(包含中文)
|
|
|
|
confirm_password = WebDriverWait(driver, 10).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "password2"))
|
|
|
|
)
|
|
|
|
confirm_password.send_keys("测试密码")
|
|
|
|
|
|
|
|
# 点击注册按钮
|
|
|
|
register_button = WebDriverWait(driver, 10).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "注册")]'))
|
|
|
|
)
|
|
|
|
register_button.click()
|
|
|
|
|
|
|
|
# 验证警告提示
|
|
|
|
alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
|
|
|
|
assert "中文" in alert.text or "Chinese" in alert.text, "未显示密码中文限制提示"
|
|
|
|
alert.accept()
|