2024-03-27 12:39:42 +08:00
|
|
|
import random
|
|
|
|
import string
|
2024-05-26 11:32:39 +08:00
|
|
|
import time
|
|
|
|
|
2024-04-09 22:09:50 +08:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
2024-03-27 12:39:42 +08:00
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
|
2024-04-19 08:50:41 +08:00
|
|
|
from helper import signup
|
2024-04-15 11:08:06 +08:00
|
|
|
|
2024-03-27 12:39:42 +08:00
|
|
|
|
2024-04-09 22:09:50 +08:00
|
|
|
def has_punctuation(s):
|
|
|
|
return any(c in string.punctuation for c in s)
|
2024-03-27 12:39:42 +08:00
|
|
|
|
|
|
|
|
2024-04-09 22:09:50 +08:00
|
|
|
def login(driver, home, uname, password):
|
|
|
|
driver.get(home)
|
|
|
|
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, '登录'))).click()
|
|
|
|
driver.find_element(By.ID, 'username').send_keys(uname)
|
|
|
|
driver.find_element(By.ID, 'password').send_keys(password)
|
|
|
|
driver.find_element(By.XPATH, '//button[text()="登录"]').click()
|
|
|
|
WebDriverWait(driver, 10).until(EC.title_is(f"EnglishPal Study Room for {uname}"))
|
2024-03-27 12:39:42 +08:00
|
|
|
|
|
|
|
|
2024-04-09 22:09:50 +08:00
|
|
|
def select_valid_word(driver):
|
|
|
|
elem = driver.find_element(By.ID, 'text-content')
|
|
|
|
essay_content = elem.text
|
|
|
|
valid_word = random.choice([word for word in essay_content.split() if len(word) >= 6 and not has_punctuation(
|
|
|
|
word) and 'font>' not in word and 'br>' not in word and 'p>' not in word])
|
|
|
|
driver.find_element(By.ID, 'selected-words').send_keys(valid_word)
|
|
|
|
return valid_word
|
2024-03-27 12:39:42 +08:00
|
|
|
|
|
|
|
|
2024-04-15 11:08:06 +08:00
|
|
|
def test_save_selected_word(driver, URL):
|
2024-03-27 12:39:42 +08:00
|
|
|
try:
|
2024-04-15 11:08:06 +08:00
|
|
|
username, password = signup(URL, driver)
|
2024-04-09 22:09:50 +08:00
|
|
|
word = select_valid_word(driver)
|
2024-03-27 12:39:42 +08:00
|
|
|
stored_words = driver.execute_script('return localStorage.getItem("selectedWords");')
|
2024-04-09 22:09:50 +08:00
|
|
|
assert word == stored_words, "Selected word not saved to localStorage correctly"
|
|
|
|
# 退出并重新登录以检查存储的单词
|
2024-05-27 14:26:54 +08:00
|
|
|
driver.find_element(By.LINK_TEXT, '退出').click()
|
2024-03-27 12:39:42 +08:00
|
|
|
driver.execute_script("window.open('');window.close();")
|
|
|
|
|
|
|
|
# 等待一会儿,让浏览器有足够的时间关闭标签页
|
2024-04-09 22:09:50 +08:00
|
|
|
WebDriverWait(driver, 2)
|
2024-03-27 12:39:42 +08:00
|
|
|
|
|
|
|
# 重新打开一个新的标签页
|
|
|
|
driver.execute_script("window.open('');")
|
|
|
|
driver.switch_to.window(driver.window_handles[-1]) # 切换到新打开的标签页
|
|
|
|
|
2024-04-15 11:08:06 +08:00
|
|
|
login(driver, URL, username, password)
|
2024-04-09 22:09:50 +08:00
|
|
|
textarea_content = driver.find_element(By.ID, 'selected-words').get_attribute('value')
|
|
|
|
assert word == textarea_content, "Selected word not preserved after re-login"
|
2024-03-27 12:39:42 +08:00
|
|
|
finally:
|
|
|
|
driver.quit()
|