From 3cffb14039208d97ea7cfabc98f5bd66d82ab416 Mon Sep 17 00:00:00 2001 From: xrj <2023438860@qq.com> Date: Fri, 30 May 2025 12:38:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86test=5Fbug528?= =?UTF-8?q?=EF=BC=8C=E8=AE=A9=E8=BF=99=E4=B8=AAtest=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=B5=8B=E8=AF=95=E5=AF=86=E7=A0=81=E4=B8=AD?= =?UTF-8?q?=E5=90=AB=E6=9C=89=E7=A9=BA=E6=A0=BC=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/test/test_bug528_tangjiao.py | 160 ++++++++++++++++++------------- 1 file changed, 96 insertions(+), 64 deletions(-) diff --git a/app/test/test_bug528_tangjiao.py b/app/test/test_bug528_tangjiao.py index 802423c..9ddc9fc 100644 --- a/app/test/test_bug528_tangjiao.py +++ b/app/test/test_bug528_tangjiao.py @@ -5,91 +5,123 @@ from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException, TimeoutException - - +from selenium import webdriver +from selenium.webdriver.edge.service import Service +from selenium.webdriver.common.by import By # 测试登录页面输入密码包含空格的情况 -def test_login_password_with_space(driver, URL): - try: - driver.get(URL+"/login") +import unittest - # 输入用户名 - username_elem = driver.find_element_by_id('username') - username_elem.send_keys("test_user") +class Test_Bug528_Tangjiao(unittest.TestCase): + def test_login_password_with_space(self): + URL = 'http://127.0.0.1:5000' + service = Service(executable_path=r'D:\studey\webdrive\edge\msedgedriver.exe') + driver = webdriver.Edge(service=service) + try: + driver.get(URL + "/login") - # 输入包含空格的密码 - password_elem = driver.find_element_by_id('password') - password_elem.send_keys("password with space") + # 输入用户名 + username_elem = driver.find_element(By.ID, 'username') + username_elem.send_keys("test_user") - # 提交登录表单 - elem = driver.find_element_by_class_name('btn') # 找到提交按钮 - elem.click() + # 输入包含空格的密码 + password_elem = driver.find_element(By.ID, 'password') + password_elem.send_keys("password with space") - # 显式等待直到警告框出现 - WebDriverWait(driver, 10).until(EC.alert_is_present()) + # 提交登录表单 + elem = driver.find_element(By.ID, 'btn') # 找到提交按钮 + elem.click() - # 检查是否弹出警告框 - alert = driver.switch_to.alert - assert "输入不能包含空格!" in alert.text - except (NoSuchElementException, TimeoutException) as e: - pytest.fail("页面元素未找到或超时: {}".format(e)) + # 显式等待直到警告框出现 + WebDriverWait(driver, 10).until(EC.alert_is_present()) + # 检查是否弹出警告框 + alert = driver.switch_to.alert + assert "输入不能包含空格!" in alert.text + except (NoSuchElementException, TimeoutException) as e: + pytest.fail("页面元素未找到或超时: {}".format(e)) -# 测试注册页面输入密码包含空格的情况 + # 测试注册页面输入密码包含空格的情况 -def test_signup_password_with_space(driver, URL): - try: - driver.get(URL+"/signup") + def test_signup_password_with_space(self): + URL = 'http://127.0.0.1:5000' + service = Service(executable_path=r'D:\studey\webdrive\edge\msedgedriver.exe') + driver = webdriver.Edge(service=service) + try: + driver.get(URL + "/signup") - # 输入用户名 - username_elem = driver.find_element_by_id('username') - username_elem.send_keys("new_user") + # 输入用户名 + username_elem = driver.find_element(By.ID, 'username') + username_elem.send_keys("new_user") - # 输入包含空格的密码 - password_elem = driver.find_element_by_id('password') - password_elem.send_keys("password with space") + # 输入包含空格的密码 + password_elem = driver.find_element(By.ID, 'password') + password_elem.send_keys("password with space") - # 再次输入密码 - password2_elem = driver.find_element_by_id('password2') - password2_elem.send_keys("password with space") + # 再次输入密码 + password2_elem = driver.find_element(By.ID, 'password2') + password2_elem.send_keys("password with space") - # 提交注册表单 - elem = driver.find_element_by_class_name('btn') # 找到提交按钮 - elem.click() + # 提交注册表单 + elem = driver.find_element(By.ID, 'btn') # 找到提交按钮 + elem.click() - # 显式等待直到警告框出现 - WebDriverWait(driver, 10).until(EC.alert_is_present()) + # 显式等待直到警告框出现 + WebDriverWait(driver, 10).until(EC.alert_is_present()) - # 检查是否弹出警告框 - alert = driver.switch_to.alert - assert "输入不能包含空格!" in alert.text - except (NoSuchElementException, TimeoutException) as e: - pytest.fail("页面元素未找到或超时: {}".format(e)) + # 检查是否弹出警告框 + alert = driver.switch_to.alert + assert "输入不能包含空格!" in alert.text + except (NoSuchElementException, TimeoutException) as e: + pytest.fail("页面元素未找到或超时: {}".format(e)) + # 测试重设密码页面输入新密码包含空格的情况 + def test_reset_password_with_space(self): + URL = 'http://127.0.0.1:5000' + service = Service(executable_path=r'D:\studey\webdrive\edge\msedgedriver.exe') + driver = webdriver.Edge(service=service) -# 测试重设密码页面输入新密码包含空格的情况 + try: + # 1. 先访问登录页面 + driver.get(URL + "/login") + driver.maximize_window() -def test_reset_password_with_space(driver, URL): - try: - driver.get(URL+"/reset") + # 2. 完成登录 + WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, 'username')) + ).send_keys("student6") # 替换为有效的测试用户名 - # 输入用户名 - username_elem = driver.find_element_by_id('username') - username_elem.send_keys("test_user") + driver.find_element(By.ID, 'password').send_keys("ad1258@#$") # 替换为有效的密码 + driver.find_element(By.ID, 'btn').click() - # 输入包含空格的密码 - password_elem = driver.find_element_by_id('password') - password_elem.send_keys("password with space") + # 4. 现在访问重置密码页面 + driver.get(URL + "/reset") - # 提交重设密码表单 - elem = driver.find_element_by_class_name('btn') # 找到提交按钮 - elem.click() + # 5. 确保重置页面加载完成 + WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, 'old-password')) + ) - # 显式等待直到警告框出现 - WebDriverWait(driver, 10).until(EC.alert_is_present()) + # 6. 执行测试 - 输入包含空格的密码 + old_password = driver.find_element(By.ID, 'old-password') + old_password.send_keys("ad1258@#$") - # 检查是否弹出警告框 - alert = driver.switch_to.alert - assert "输入不能包含空格!" in alert.text - except (NoSuchElementException, TimeoutException) as e: - pytest.fail("页面元素未找到或超时: {}".format(e)) + # 填写其他字段(避免因其他验证失败而影响测试) + driver.find_element(By.ID, 'new-password').send_keys("new pass") + driver.find_element(By.ID, 're-new-password').send_keys("new pass") + + # 提交表单 + submit_btn = driver.find_element(By.ID, 'submit') + submit_btn.click() + + # 7. 验证预期结果 + WebDriverWait(driver, 10).until(EC.alert_is_present()) + alert = driver.switch_to.alert + assert "输入不能包含空格!" in alert.text + alert.accept() + + except Exception as e: + driver.save_screenshot("reset_error.png") + pytest.fail(f"测试失败: {e}") + finally: + driver.quit() \ No newline at end of file -- 2.17.1 From 8be875111d00e78413c73e4d6e1e43d658d622b6 Mon Sep 17 00:00:00 2001 From: xrj <2023438860@qq.com> Date: Fri, 30 May 2025 12:46:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86vocabulary?= =?UTF-8?q?=E5=92=8Ctest=5Fvocabulary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/test_vocabulary.py | 12 ++++++++ app/vocabulary.py | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/test_vocabulary.py create mode 100644 app/vocabulary.py diff --git a/app/test_vocabulary.py b/app/test_vocabulary.py new file mode 100644 index 0000000..b3ed0f8 --- /dev/null +++ b/app/test_vocabulary.py @@ -0,0 +1,12 @@ +from vocabulary import UserVocabularyLevel, ArticleVocabularyLevel + + +def test_article_level(): + ''' Boundary case test ''' + article = ArticleVocabularyLevel('') + assert article.level == 0 + +def test_user_level(): + ''' Boundary case test ''' + user = UserVocabularyLevel({}) + assert user.level == 0 \ No newline at end of file diff --git a/app/vocabulary.py b/app/vocabulary.py new file mode 100644 index 0000000..b6c2a08 --- /dev/null +++ b/app/vocabulary.py @@ -0,0 +1,63 @@ +''' + Estimate a user's vocabulary level given his vocabulary data + Estimate an English article's difficulty level given its content + Preliminary design + + Hui, 2024-09-23 + Last upated: 2024-09-25, 2024-09-30 +''' + +import pickle + + +def load_record(pickle_fname): + with open(pickle_fname, 'rb') as f: + d = pickle.load(f) + return d + + +class VocabularyLevelEstimator: + _test = load_record('words_and_tests.p') # map a word to the sources where it appears + + @property + def level(self): + total = len(self._test) + num = 0 + for word in self.word_lst: + num += 1 + if word in self._test: + print(f'{word} : {self._test[word]}') + else: + print(f'{word}') + if num == 0: + return 0 + return total/num + + +class UserVocabularyLevel(VocabularyLevelEstimator): + def __init__(self, d): + self.d = d + self.word_lst = list(d.keys()) + # just look at the most recently-added words + + +class ArticleVocabularyLevel(VocabularyLevelEstimator): + def __init__(self, content): + self.content = content + self.word_lst = content.lower().split() + # select the 10 most difficult words + + +if __name__ == '__main__': + d = load_record('frequency_zhangsan.pickle') + print(d) + #换行 + print('------------') + user = UserVocabularyLevel(d) + print(user.level) # level is a property + print('------------') + article = ArticleVocabularyLevel('This is an interesting article') + print(article.level) + + + -- 2.17.1 From 17a753062abe7e57e8913f1e0537ff8501883c57 Mon Sep 17 00:00:00 2001 From: xrj <2023438860@qq.com> Date: Fri, 30 May 2025 12:58:17 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AF=B9vocabulary=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=AE=A1=E6=9F=A5=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E5=86=85=E9=83=A8=E9=94=99=E8=AF=AF=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/vocabulary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/vocabulary.py b/app/vocabulary.py index b6c2a08..4d1d76a 100644 --- a/app/vocabulary.py +++ b/app/vocabulary.py @@ -17,7 +17,7 @@ def load_record(pickle_fname): class VocabularyLevelEstimator: - _test = load_record('words_and_tests.p') # map a word to the sources where it appears + _test = load_record('static\words_and_tests.p') # map a word to the sources where it appears @property def level(self): -- 2.17.1