test: update test file, update username/password config, add pytest to requirements.txt
parent
83491ce28c
commit
b5bde9d33d
Binary file not shown.
|
@ -11,3 +11,13 @@ def URL():
|
||||||
def driver():
|
def driver():
|
||||||
my_driver = webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
|
my_driver = webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
|
||||||
return my_driver
|
return my_driver
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def UNAME():
|
||||||
|
return 'lanhui'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def PASSWORD():
|
||||||
|
return 'l0ve1t'
|
||||||
|
|
|
@ -1,98 +1,56 @@
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import time
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
from selenium import webdriver
|
|
||||||
from selenium.webdriver.support.wait import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from conftest import driver, URL, UNAME, PASSWORD
|
||||||
|
|
||||||
HOME_PAGE = "http://127.0.0.1:5000/"
|
|
||||||
|
|
||||||
word = []
|
def has_punctuation(s):
|
||||||
uname = 'lanhui'
|
return any(c in string.punctuation for c in s)
|
||||||
password = 'l0ve1t'
|
|
||||||
def has_punctuation(s): #用于检查单词是否包含标点符号
|
|
||||||
return [c for c in s if c in string.punctuation] != []
|
|
||||||
|
|
||||||
def login(driver):
|
|
||||||
driver.get(HOME_PAGE)
|
|
||||||
assert 'English Pal - Learn English smartly!' in driver.page_source
|
|
||||||
# 登录
|
|
||||||
login_elem = driver.find_element_by_link_text('登录')
|
|
||||||
login_elem.click()
|
|
||||||
|
|
||||||
username_input = driver.find_element_by_id('username')
|
def login(driver, home, uname, password):
|
||||||
username_input.send_keys(uname)
|
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}"))
|
||||||
|
|
||||||
password_input = driver.find_element_by_id('password')
|
|
||||||
password_input.send_keys(password)
|
|
||||||
|
|
||||||
login_btn = driver.find_element_by_xpath('//button[text()="登录"]') # 找到登录按钮
|
def select_valid_word(driver):
|
||||||
login_btn.click()
|
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
|
||||||
|
|
||||||
# 确保页面加载完
|
|
||||||
|
def test_save_selected_word(driver, URL, UNAME, PASSWORD):
|
||||||
try:
|
try:
|
||||||
WebDriverWait(driver, 10).until(
|
login(driver, URL, UNAME, PASSWORD)
|
||||||
EC.title_is("EnglishPal Study Room for " + uname) # 替换为实际的页面标题
|
word = select_valid_word(driver)
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print("页面加载失败:", e)
|
|
||||||
driver.quit()
|
|
||||||
exit()
|
|
||||||
assert 'EnglishPal Study Room for ' + uname in driver.title
|
|
||||||
|
|
||||||
def test_save_selected_word():
|
|
||||||
global word
|
|
||||||
# 调用本地chromedriver
|
|
||||||
driver = webdriver.Chrome(executable_path="C:\Program Files\Google\Chrome\Application\chromedriver.exe")
|
|
||||||
try:
|
|
||||||
login(driver)
|
|
||||||
#点击单词添加到已选单词列表
|
|
||||||
# 获取文章内容
|
|
||||||
elem = driver.find_element_by_id('text-content')
|
|
||||||
essay_content = elem.text
|
|
||||||
|
|
||||||
#随机选择单词 长度不小于6个字符,并且不包含标点符号
|
|
||||||
elem = driver.find_element_by_id('selected-words')
|
|
||||||
word = random.choice(essay_content.split())
|
|
||||||
while 'font>' in word or 'br>' in word or 'p>' in word or len(word) < 6 or has_punctuation(word):
|
|
||||||
word = random.choice(essay_content.split())
|
|
||||||
print(type(word)) #str
|
|
||||||
elem.send_keys(word) #单词输入到输入框
|
|
||||||
|
|
||||||
# 检查单词是否已成功添加到已选列表
|
|
||||||
# 再次获取<textarea>中的文本内容,以确认单词已被输入
|
|
||||||
updated_textarea_content = elem.get_attribute('value')
|
|
||||||
# 检查<textarea>中的内容是否包含输入的单词
|
|
||||||
assert word == updated_textarea_content
|
|
||||||
# 检查是否保存到 localStorage
|
|
||||||
stored_words = driver.execute_script('return localStorage.getItem("selectedWords");')
|
stored_words = driver.execute_script('return localStorage.getItem("selectedWords");')
|
||||||
assert word == stored_words
|
assert word == stored_words, "Selected word not saved to localStorage correctly"
|
||||||
#检查退出登录、关闭web页面后是否保存已选单词
|
# 退出并重新登录以检查存储的单词
|
||||||
# 退出登录
|
driver.find_element(By.LINK_TEXT, '退出').click()
|
||||||
elem = driver.find_element_by_link_text('退出')
|
|
||||||
elem.click()
|
|
||||||
|
|
||||||
# 关闭当前标签页
|
|
||||||
driver.execute_script("window.open('');window.close();")
|
driver.execute_script("window.open('');window.close();")
|
||||||
|
|
||||||
# 等待一会儿,让浏览器有足够的时间关闭标签页
|
# 等待一会儿,让浏览器有足够的时间关闭标签页
|
||||||
time.sleep(2)
|
WebDriverWait(driver, 2)
|
||||||
|
|
||||||
# 重新打开一个新的标签页
|
# 重新打开一个新的标签页
|
||||||
driver.execute_script("window.open('');")
|
driver.execute_script("window.open('');")
|
||||||
driver.switch_to.window(driver.window_handles[-1]) # 切换到新打开的标签页
|
driver.switch_to.window(driver.window_handles[-1]) # 切换到新打开的标签页
|
||||||
|
|
||||||
login(driver)
|
login(driver, URL, UNAME, PASSWORD)
|
||||||
elem = driver.find_element_by_id('selected-words')
|
textarea_content = driver.find_element(By.ID, 'selected-words').get_attribute('value')
|
||||||
textarea_content = elem.get_attribute('value')
|
assert word == textarea_content, "Selected word not preserved after re-login"
|
||||||
print(f'word:{word}')
|
|
||||||
print(f'selected:{textarea_content}')
|
|
||||||
assert word == textarea_content
|
|
||||||
except Exception as e:
|
|
||||||
print("An error occurred:", e)
|
|
||||||
finally:
|
finally:
|
||||||
# 关闭浏览器
|
|
||||||
driver.quit()
|
driver.quit()
|
||||||
|
|
||||||
test_save_selected_word()
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_save_selected_word(driver, URL, UNAME, PASSWORD)
|
||||||
|
|
|
@ -4,3 +4,5 @@ PyYAML~=6.0
|
||||||
pony==0.7.16
|
pony==0.7.16
|
||||||
snowballstemmer==2.2.0
|
snowballstemmer==2.2.0
|
||||||
Werkzeug==2.2.2
|
Werkzeug==2.2.2
|
||||||
|
|
||||||
|
pytest~=8.1.1
|
Loading…
Reference in New Issue