From 310e883d35f9a0f6b9aec9a1ce610f5715aeace0 Mon Sep 17 00:00:00 2001 From: Desuver <13868901+desuver@user.noreply.gitee.com> Date: Thu, 22 May 2025 14:18:30 +0800 Subject: [PATCH] test update --- app/Yaml.py | 8 +- app/test/test_bug536_jiangwangzhe.py | 151 +++++++++++++------------- app/test/test_bug544_tangxinyuan.py | 2 - test.py | 1 + test_vocabulary_output_2025_04_10.txt | 10 ++ 5 files changed, 91 insertions(+), 81 deletions(-) create mode 100644 test.py create mode 100644 test_vocabulary_output_2025_04_10.txt diff --git a/app/Yaml.py b/app/Yaml.py index 00974aa..d8e5925 100644 --- a/app/Yaml.py +++ b/app/Yaml.py @@ -15,13 +15,13 @@ ymlPath = path_prefix + 'static/config.yml' # partial文件夹路径 partialPath = path_prefix + 'layout/partial/' -f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件 -cont = f.read() # 以文本形式读取YAML +f = open(ymlPath, 'r', encoding='utf-8') +cont = f.read() yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML 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: - yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部 + yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部 diff --git a/app/test/test_bug536_jiangwangzhe.py b/app/test/test_bug536_jiangwangzhe.py index 4862486..4a3e1e8 100644 --- a/app/test/test_bug536_jiangwangzhe.py +++ b/app/test/test_bug536_jiangwangzhe.py @@ -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.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from webdriver_manager.chrome import ChromeDriverManager + +# 配置测试环境 +BASE_URL = "http://localhost:8000" # 替换为您的应用URL -# 对用户名不能为中文进行测试 -def test_register_username_with_chinese(driver, URL): - try: - driver.get(URL + "/signup") - - # 等待用户名输入框出现 - username_elem = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, 'username')) - ) - 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 +@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_register_password_with_chinese(driver, URL): - try: - driver.get(URL + "/signup") +# 测试用户名不能包含中文 +def test_username_cannot_contain_chinese(driver): + # 打开注册页面 + driver.get(f"{BASE_URL}/signup") - # 等待用户名输入框出现 - username_elem = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, 'username')) - ) - username_elem.send_keys("validUsername123") # 输入有效用户名 + # 填写用户名(包含中文) + username = WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, "username")) + ) + username.send_keys("测试用户") - # 等待密码输入框出现 - password_elem = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, 'password')) - ) - password_elem.send_keys("测试密码") # 输入中文密码 + # 填写有效密码 + password = WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, "password")) + ) + password.send_keys("ValidPassword123!") - # 等待确认密码输入框出现 - password2_elem = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, 'password2')) - ) - password2_elem.send_keys("测试密码") # 输入中文确认密码 + # 确认密码 + confirm_password = WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, "password2")) + ) + confirm_password.send_keys("ValidPassword123!") - # 等待注册按钮出现并点击 - signup_button = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.XPATH, '//button[@onclick="signup()"]')) - ) - signup_button.click() + # 点击注册按钮 + register_button = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "注册")]')) + ) + register_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 password." # 根据实际的警告文本进行断言 - alert.accept() + # 验证警告提示 + alert = WebDriverWait(driver, 10).until(EC.alert_is_present()) + assert "中文" in alert.text or "Chinese" in alert.text, "未显示用户名中文限制提示" + 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() \ No newline at end of file diff --git a/app/test/test_bug544_tangxinyuan.py b/app/test/test_bug544_tangxinyuan.py index 2cffdd4..3f7053e 100644 --- a/app/test/test_bug544_tangxinyuan.py +++ b/app/test/test_bug544_tangxinyuan.py @@ -7,8 +7,6 @@ from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from helper import signup - - def has_punctuation(s): return any(c in string.punctuation for c in s) diff --git a/test.py b/test.py new file mode 100644 index 0000000..8e23576 --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +print("Hello World") \ No newline at end of file diff --git a/test_vocabulary_output_2025_04_10.txt b/test_vocabulary_output_2025_04_10.txt new file mode 100644 index 0000000..dc6c79c --- /dev/null +++ b/test_vocabulary_output_2025_04_10.txt @@ -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 ====================================================================== +( \ No newline at end of file