import random
import string
import time
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.webdriver.common.action_chains import ActionChains

from helper import signup


def select(driver):
    text_element = driver.find_element(By.ID, 'article')

    # 创建ActionChains对象
    actions = ActionChains(driver)

    # 模拟鼠标移动到文本元素上
    actions.move_to_element(text_element)

    # 模拟鼠标按下并拖动以选择文本
    # 假设我们想要选择从第10个字符开始的5个字符
    actions.click()
    actions.perform()
    time.sleep(1)
    # 找到包含要选择文字的元素
    text_element = driver.find_element(By.ID, 'article_title')

    # 创建ActionChains对象
    actions = ActionChains(driver)

    # 模拟鼠标移动到文本元素上
    actions.move_to_element(text_element)

    # 模拟鼠标按下并拖动以选择文本
    actions.double_click()
    actions.perform()
    actions.release()             # 释放鼠标按钮

def test_selected_second_word(driver, URL):
    try:
        username, password = signup(URL, driver)
        # 找到包含要选择文字的元素
        text_element = driver.find_element(By.ID, 'article_title')

        # 创建ActionChains对象
        actions = ActionChains(driver)

        # 模拟鼠标移动到文本元素上
        actions.move_to_element(text_element)

        # 模拟鼠标按下并拖动以选择文本
        actions.double_click()
        actions.perform()

        # 获取选中的文本
        selected_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
        assert selected_words != "","选中单词被放置框中"
        #再次选取
        select(driver)
        time.sleep(1)
        selected_second_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
        assert selected_second_words.strip() == "", "选中的单词被删除"
    finally:
        driver.quit()