forked from mrlan/EnglishPal
				
			test and fix
							parent
							
								
									9b3551bbc8
								
							
						
					
					
						commit
						101506a511
					
				| 
						 | 
					@ -22,6 +22,7 @@ function getWord() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function highLight() {
 | 
					function highLight() {
 | 
				
			||||||
    if (!isHighlight) return;
 | 
					    if (!isHighlight) return;
 | 
				
			||||||
 | 
					    let word = (getWord() + "").trim();
 | 
				
			||||||
    let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器
 | 
					    let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器
 | 
				
			||||||
    let pickedWords = document.getElementById("selected-words");  // words picked to the text area
 | 
					    let pickedWords = document.getElementById("selected-words");  // words picked to the text area
 | 
				
			||||||
    let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
 | 
					    let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
 | 
				
			||||||
| 
						 | 
					@ -45,13 +46,23 @@ function highLight() {
 | 
				
			||||||
    let totalSet = new Set();
 | 
					    let totalSet = new Set();
 | 
				
			||||||
    for (let i = 0; i < list.length; ++i) {
 | 
					    for (let i = 0; i < list.length; ++i) {
 | 
				
			||||||
        list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符
 | 
					        list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符
 | 
				
			||||||
        if (list[i] != "" && !totalSet.has(list[i])) {
 | 
					        list[i] = list[i].replace('|', "");
 | 
				
			||||||
 | 
					        list[i] = list[i].replace('?', "");
 | 
				
			||||||
 | 
					        if (list[i] != "" && !totalSet.has(list[i]) && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) {
 | 
				
			||||||
            // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。
 | 
					            // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。
 | 
				
			||||||
            let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi")));
 | 
					            let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi")));
 | 
				
			||||||
 | 
					            let articleContent_fb = articleContent;  //文章副本
 | 
				
			||||||
            if (matches.has("mark")) {
 | 
					            if (matches.has("mark")) {
 | 
				
			||||||
                // 优先处理单词为 "mark" 的情况
 | 
					                // 优先处理单词为 "mark" 的情况
 | 
				
			||||||
                totalSet = new Set(["mark", ...totalSet]);
 | 
					                totalSet = new Set(["mark", ...totalSet]);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){
 | 
				
			||||||
 | 
					                //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | 
				
			||||||
 | 
					                const index = articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase());
 | 
				
			||||||
 | 
					                list[i] = articleContent_fb.substring(index, index + list[i].length);
 | 
				
			||||||
 | 
					                articleContent_fb = articleContent_fb.substring(index + list[i].length);    // 使用副本中list[i]之后的子串替换掉副本
 | 
				
			||||||
 | 
					                articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            totalSet = new Set([...totalSet, ...matches]);
 | 
					            totalSet = new Set([...totalSet, ...matches]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -291,7 +291,7 @@
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
<style>
 | 
					<style>
 | 
				
			||||||
    mark {
 | 
					    mark {
 | 
				
			||||||
        color: #{{ yml['highlight']['color'] }};
 | 
					        color: red;
 | 
				
			||||||
        background-color: rgba(0, 0, 0, 0);
 | 
					        background-color: rgba(0, 0, 0, 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
</style>
 | 
					</style>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,74 @@
 | 
				
			||||||
 | 
					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 has_punctuation(s):
 | 
				
			||||||
 | 
					    return any(c in string.punctuation for c in s)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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(5)
 | 
				
			||||||
 | 
					    # 找到包含要选择文字的元素
 | 
				
			||||||
 | 
					    text_element = driver.find_element(By.ID, 'article_title')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # 创建ActionChains对象
 | 
				
			||||||
 | 
					    actions = ActionChains(driver)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # 模拟鼠标移动到文本元素上
 | 
				
			||||||
 | 
					    actions.move_to_element(text_element)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # 模拟鼠标按下并拖动以选择文本
 | 
				
			||||||
 | 
					    # 假设我们想要选择从第10个字符开始的5个字符
 | 
				
			||||||
 | 
					    actions.double_click()
 | 
				
			||||||
 | 
					    actions.perform()
 | 
				
			||||||
 | 
					    #actions.move_by_offset(100, 0)
 | 
				
			||||||
 | 
					    #actions.move_by_offset(10, 0)
 | 
				
			||||||
 | 
					    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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # 模拟鼠标按下并拖动以选择文本
 | 
				
			||||||
 | 
					        # 假设我们想要选择从第10个字符开始的5个字符
 | 
				
			||||||
 | 
					        actions.double_click()
 | 
				
			||||||
 | 
					        actions.perform()
 | 
				
			||||||
 | 
					        #actions.move_by_offset(10, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # 获取选中的文本
 | 
				
			||||||
 | 
					        selected_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
 | 
				
			||||||
 | 
					        assert selected_words != "","选中单词被放置框中"
 | 
				
			||||||
 | 
					        #再次选取
 | 
				
			||||||
 | 
					        select(driver)
 | 
				
			||||||
 | 
					        time.sleep(6)
 | 
				
			||||||
 | 
					        selected_second_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
 | 
				
			||||||
 | 
					        assert selected_second_words.strip() == "", "选中的单词被删除"
 | 
				
			||||||
 | 
					    finally:
 | 
				
			||||||
 | 
					        driver.quit()
 | 
				
			||||||
		Loading…
	
		Reference in New Issue