更新 app/test/test_bug561_wanzikun.py

Bug561-WanZiKun
万梓锟 2024-05-27 11:04:19 +08:00
parent a662626dc4
commit 7af9b4656f
1 changed files with 69 additions and 72 deletions

View File

@ -1,72 +1,69 @@
import time import time
import pytest import pytest
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver import ActionChains from selenium.webdriver import ActionChains
# 导入 app/test/conftest.py 中的 signup 函数 # 导入 app/test/conftest.py 中的 signup 函数
from app.test.conftest import signup from app.test.conftest import signup
def select_text_in_article(driver): def select_text_in_article(driver):
elem = driver.find_element(By.CSS_SELECTOR, '#article') elem = driver.find_element(By.CSS_SELECTOR, '#article')
action = ActionChains(driver) action = ActionChains(driver)
# 从左上角点击并按住 # 从左上角点击并按住
action.move_to_element_with_offset(elem, 0, 0) action.move_to_element_with_offset(elem, 0, 0)
action.click_and_hold() action.click_and_hold()
# 移动到右下角的某个位置例如右下角偏移100, 100 # 移动到右下角的某个位置例如右下角偏移100, 100
action.move_by_offset(100, 100) action.move_by_offset(100, 100)
action.release() action.release()
action.perform() action.perform()
def verify_highlight(driver): def verify_highlight(driver):
# 检查是否有高亮的元素 # 检查是否有高亮的元素
highlighted_elements = driver.execute_script(""" highlighted_elements = driver.execute_script("""
return Array.from(document.querySelectorAll('#article mark')).map(mark => mark.innerText); return Array.from(document.querySelectorAll('#article mark')).map(mark => mark.innerText);
""") """)
print("Highlighted elements:", highlighted_elements) print("Highlighted elements:", highlighted_elements)
return len(highlighted_elements) > 0 return len(highlighted_elements) > 0
def test_bug561(URL, driver): def test_bug561(URL, driver):
try: try:
driver.maximize_window() driver.maximize_window()
username, password = signup(URL, driver) username, password = signup(URL, driver)
time.sleep(3) # 等待页面加载完成 time.sleep(3) # 等待页面加载完成
select_text_in_article(driver) select_text_in_article(driver)
if verify_highlight(driver): if verify_highlight(driver):
print("Text highlighted after selection") print("Text highlighted after selection")
else: else:
print("No text highlighted after selection") print("No text highlighted after selection")
# 点击“下一页”按钮 # 点击“下一页”按钮
next_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_next_article'))) next_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_next_article')))
next_button.click() next_button.click()
print("Clicked next article button.") print("Clicked next article button.")
# 等待“上一页”按钮加载完成 # 等待“上一页”按钮加载完成
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_pre_article'))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_pre_article')))
print("Next article loaded.") print("Next article loaded.")
# 点击“上一页”按钮 # 点击“上一页”按钮
prev_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_pre_article'))) prev_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_pre_article')))
prev_button.click() prev_button.click()
print("Clicked previous article button.") print("Clicked previous article button.")
# 确认高亮是否保留 # 确认高亮是否保留
if verify_highlight(driver): if verify_highlight(driver):
print("Highlighted text preserved after switching articles") print("Highlighted text preserved after switching articles")
else: else:
print("Highlighted text not preserved after switching articles") print("Highlighted text not preserved after switching articles")
except Exception as e: except Exception as e:
print(f"Error during test: {e}") print(f"Error during test: {e}")
finally: finally:
driver.quit() driver.quit()
print("Driver closed.") print("Driver closed.")
if __name__ == "__main__":
pytest.main([__file__])