1
0
Fork 0
EnglishPal/app/test/test_bug476_LiMengdie.py

119 lines
4.0 KiB
Python

import time
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException, \
ElementClickInterceptedException, NoSuchElementException, TimeoutException
from helper import signup
def is_textarea_empty(driver):
# 获取<textarea>元素的内容,判断是否加入了生词
textarea_element = driver.find_element(By.ID, 'selected-words')
content = textarea_element.get_attribute("value")
return content is None or content.strip() == ""
def get_user_level(driver):
return float(driver.find_element(By.ID, 'user_level').text)
def scroll(total_time, driver):
try:
half_time = (total_time - 2) / 2 # 分成两半,一半时间滚到底,一半时间滚回顶,模拟阅读时间
# 滚动到底部
start_time = time.time()
end_time = start_time + half_time
while time.time() < end_time:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 确保滚动更平滑
time.sleep(1)
# 滚动回顶部
start_time = time.time()
end_time = start_time + half_time
while time.time() < end_time:
driver.execute_script("window.scrollTo(0, -document.body.scrollHeight);")
time.sleep(1)
except ElementClickInterceptedException:
print("加载出错")
def read_test_with_time(driver, total_time):
# 获取点击前的user_level和text_level
before_user_level = get_user_level(driver)
before_text_level = float(driver.find_element(By.ID, 'text_level').text)
# 检查是否加入了生词簿
check = is_textarea_empty(driver)
scroll(total_time, driver)
# 定位“下一页”按钮
next_button = driver.find_element(By.ID, 'load_next_article')
# 模拟点击
next_button.click()
# 关闭弹窗
try:
WebDriverWait(driver, 1).until(EC.alert_is_present())
driver.switch_to.alert.accept()
except (UnexpectedAlertPresentException, NoAlertPresentException):
pass
# 等待页面加载完毕,正确地获取更新后的值
time.sleep(2)
# 获取点击后的user_level
after_user_level = get_user_level(driver)
# 等待3秒方便观察测试
time.sleep(3)
return before_user_level, after_user_level, before_text_level, check
def test_score_with_enough_time(driver, URL):
try:
# 登录并跳转到测试主页面
signup(URL, driver)
# 测试阅读到足够时间的等级变化
before_user_level, after_user_level, before_text_level, check = read_test_with_time(driver, 18)
# print(check, before_user_level, after_user_level, before_text_level)
if before_user_level < before_text_level and check:
assert before_user_level < after_user_level, 'The user level dose not increase after reading an article with a high level and clicking to navigate away without adding any new vocabulary. '
except (NoSuchElementException, TimeoutException) as e:
print("Error occurs: " + str(e))
finally:
driver.quit()
def test_score_without_enough_time(driver, URL):
try:
# 登录并跳转到测试主页面
signup(URL, driver)
# 测试阅读时间不足够的等级变化
before_user_level, after_user_level, before_text_level, check = read_test_with_time(driver, 6)
# print(check, before_user_level, after_user_level, before_text_level)
if before_user_level < before_text_level and check:
assert before_user_level == after_user_level, 'After the user quickly skipped through the article, the user level does not remain unchanged. '
except (NoSuchElementException, TimeoutException) as e:
print("Error occurs: " + str(e))
finally:
driver.quit()
if __name__ == '__main__':
pytest.main(["--html=report.html"])