test update
parent
5fa2951df7
commit
310e883d35
|
@ -15,13 +15,13 @@ ymlPath = path_prefix + 'static/config.yml'
|
||||||
|
|
||||||
# partial文件夹路径
|
# partial文件夹路径
|
||||||
partialPath = path_prefix + 'layout/partial/'
|
partialPath = path_prefix + 'layout/partial/'
|
||||||
f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件
|
f = open(ymlPath, 'r', encoding='utf-8')
|
||||||
cont = f.read() # 以文本形式读取YAML
|
cont = f.read()
|
||||||
|
|
||||||
yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML
|
yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML
|
||||||
|
|
||||||
with open(partialPath + 'header.html', 'r', encoding='utf-8') as f:
|
with open(partialPath + 'header.html', 'r', encoding='utf-8') as f:
|
||||||
yml['header'] = f.read() # header内的文本会被直接添加到所有页面的head标签内
|
yml['header'] = f.read() # header内的文本会被直接添加到所有页面的head标签内
|
||||||
|
|
||||||
with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
|
with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
|
||||||
yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部
|
yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部
|
||||||
|
|
|
@ -1,88 +1,89 @@
|
||||||
from selenium.webdriver.common.alert import Alert
|
import pytest
|
||||||
|
from selenium import webdriver
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from webdriver_manager.chrome import ChromeDriverManager
|
||||||
|
|
||||||
|
# 配置测试环境
|
||||||
|
BASE_URL = "http://localhost:8000" # 替换为您的应用URL
|
||||||
|
|
||||||
|
|
||||||
# 对用户名不能为中文进行测试
|
@pytest.fixture
|
||||||
def test_register_username_with_chinese(driver, URL):
|
def driver():
|
||||||
try:
|
# 使用 Chrome 浏览器并自动管理驱动
|
||||||
driver.get(URL + "/signup")
|
options = webdriver.ChromeOptions()
|
||||||
|
options.add_argument("--headless") # 无头模式,不显示浏览器窗口
|
||||||
# 等待用户名输入框出现
|
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
|
||||||
username_elem = WebDriverWait(driver, 10).until(
|
yield driver
|
||||||
EC.presence_of_element_located((By.ID, 'username'))
|
driver.quit() # 测试结束后关闭浏览器
|
||||||
)
|
|
||||||
username_elem.send_keys("测试用户") # 输入中文用户名
|
|
||||||
|
|
||||||
# 等待密码输入框出现
|
|
||||||
password_elem = WebDriverWait(driver, 10).until(
|
|
||||||
EC.presence_of_element_located((By.ID, 'password'))
|
|
||||||
)
|
|
||||||
password_elem.send_keys("validPassword123") # 输入有效密码
|
|
||||||
|
|
||||||
# 等待确认密码输入框出现
|
|
||||||
password2_elem = WebDriverWait(driver, 10).until(
|
|
||||||
EC.presence_of_element_located((By.ID, 'password2'))
|
|
||||||
)
|
|
||||||
password2_elem.send_keys("validPassword123") # 输入有效确认密码
|
|
||||||
|
|
||||||
# 等待注册按钮出现并点击
|
|
||||||
signup_button = WebDriverWait(driver, 10).until(
|
|
||||||
EC.element_to_be_clickable((By.XPATH, '//button[@onclick="signup()"]'))
|
|
||||||
)
|
|
||||||
signup_button.click()
|
|
||||||
|
|
||||||
# 等待警告框出现并接受
|
|
||||||
WebDriverWait(driver, 10).until(EC.alert_is_present())
|
|
||||||
alert = driver.switch_to.alert
|
|
||||||
alert_text = alert.text
|
|
||||||
print(f"警告文本: {alert_text}")
|
|
||||||
assert alert_text == "Chinese characters are not allowed in the user name." # 根据实际的警告文本进行断言
|
|
||||||
alert.accept()
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"发生错误: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
# 对注册时密码不能是中文进行测试
|
# 测试用户名不能包含中文
|
||||||
def test_register_password_with_chinese(driver, URL):
|
def test_username_cannot_contain_chinese(driver):
|
||||||
try:
|
# 打开注册页面
|
||||||
driver.get(URL + "/signup")
|
driver.get(f"{BASE_URL}/signup")
|
||||||
|
|
||||||
# 等待用户名输入框出现
|
# 填写用户名(包含中文)
|
||||||
username_elem = WebDriverWait(driver, 10).until(
|
username = WebDriverWait(driver, 10).until(
|
||||||
EC.presence_of_element_located((By.ID, 'username'))
|
EC.presence_of_element_located((By.ID, "username"))
|
||||||
)
|
)
|
||||||
username_elem.send_keys("validUsername123") # 输入有效用户名
|
username.send_keys("测试用户")
|
||||||
|
|
||||||
# 等待密码输入框出现
|
# 填写有效密码
|
||||||
password_elem = WebDriverWait(driver, 10).until(
|
password = WebDriverWait(driver, 10).until(
|
||||||
EC.presence_of_element_located((By.ID, 'password'))
|
EC.presence_of_element_located((By.ID, "password"))
|
||||||
)
|
)
|
||||||
password_elem.send_keys("测试密码") # 输入中文密码
|
password.send_keys("ValidPassword123!")
|
||||||
|
|
||||||
# 等待确认密码输入框出现
|
# 确认密码
|
||||||
password2_elem = WebDriverWait(driver, 10).until(
|
confirm_password = WebDriverWait(driver, 10).until(
|
||||||
EC.presence_of_element_located((By.ID, 'password2'))
|
EC.presence_of_element_located((By.ID, "password2"))
|
||||||
)
|
)
|
||||||
password2_elem.send_keys("测试密码") # 输入中文确认密码
|
confirm_password.send_keys("ValidPassword123!")
|
||||||
|
|
||||||
# 等待注册按钮出现并点击
|
# 点击注册按钮
|
||||||
signup_button = WebDriverWait(driver, 10).until(
|
register_button = WebDriverWait(driver, 10).until(
|
||||||
EC.element_to_be_clickable((By.XPATH, '//button[@onclick="signup()"]'))
|
EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "注册")]'))
|
||||||
)
|
)
|
||||||
signup_button.click()
|
register_button.click()
|
||||||
|
|
||||||
# 等待警告框出现并接受
|
# 验证警告提示
|
||||||
WebDriverWait(driver, 10).until(EC.alert_is_present())
|
alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
|
||||||
alert = driver.switch_to.alert
|
assert "中文" in alert.text or "Chinese" in alert.text, "未显示用户名中文限制提示"
|
||||||
alert_text = alert.text
|
alert.accept()
|
||||||
print(f"警告文本: {alert_text}")
|
|
||||||
assert alert_text == "Chinese characters are not allowed in the password." # 根据实际的警告文本进行断言
|
|
||||||
alert.accept()
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"发生错误: {e}")
|
# 测试密码不能包含中文
|
||||||
raise
|
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()
|
|
@ -7,8 +7,6 @@ from selenium.webdriver.support.ui import WebDriverWait
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
|
||||||
from helper import signup
|
from helper import signup
|
||||||
|
|
||||||
|
|
||||||
def has_punctuation(s):
|
def has_punctuation(s):
|
||||||
return any(c in string.punctuation for c in s)
|
return any(c in string.punctuation for c in s)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
(.venv) PS D:\2025软件项目管理\spm_vocabulary\spm_vocabulary> pytest test_vocabulary.py
|
||||||
|
===================================================================== test session starts ======================================================================
|
||||||
|
platform win32 -- Python 3.12.4, pytest-8.3.5, pluggy-1.5.0
|
||||||
|
rootdir: D:\2025软件项目管理\spm_vocabulary\spm_vocabulary
|
||||||
|
collected 16 items
|
||||||
|
|
||||||
|
test_vocabulary.py ................ [100%]
|
||||||
|
|
||||||
|
====================================================================== 16 passed in 0.02s ======================================================================
|
||||||
|
(
|
Loading…
Reference in New Issue