diff --git a/Lib/Driver.py b/Lib/Driver.py index db2584f..30d31e5 100644 --- a/Lib/Driver.py +++ b/Lib/Driver.py @@ -2,6 +2,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.alert import Alert from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait import subprocess @@ -719,44 +720,60 @@ class Browser(browser_webdriver): """ return self.execute_script('window.location.href=%s;' % json.dumps(url, indent=None, ensure_ascii=True), None) - def find(self, path, wait_for=False, timeout: float = 5.0, freq: float = 0.5, delay: float = 0.0): + def find(self, path, wait_for=False, timeout: float = 5.0, freq: float = 0.5, delay: float = 0.0) -> WebElement: """ Use XPath to find an element. """ - ele = self.webdriver_wait(timeout, freq).until(EC.presence_of_element_located((By.XPATH, path))) if wait_for else self.find_element(By.XPATH, path) + element = self.webdriver_wait(timeout, freq).until(EC.presence_of_element_located((By.XPATH, path))) if wait_for else self.find_element(By.XPATH, path) delay and self.wait(delay) - ele = self.find_element(By.XPATH, path) if delay else ele - self.element_prominent(ele, '#f8be5f') - return ele + element = self.find_element(By.XPATH, path) if delay else element + self.element_appear(element, '#F8BE5F') + return element - def find_mult(self, path): + def find_mult(self, path) -> list: """ Use XPath to find elements. """ - ele = self.find_elements(By.XPATH, path) - len(ele) > 0 and [self.element_prominent(e, '#f8be5f') for e in ele] - return ele + element = self.find_elements(By.XPATH, path) + for this_element in element: self.element_appear(this_element, '#F8BE5F') + return element + + def find_mult_random_choice(self, path) -> WebElement: + """ + Use XPath to find elements then random_choice one. + """ + element = self.find_elements(By.XPATH, path) + element = random.choice(element) + self.element_appear(element, '#F8BE5F') + return element def find_element_by(self, sentence): """ Custom find element, pass into a tuple or list. """ - ele = self.find_element(*sentence) - self.element_prominent(ele, '#f8be5f') - return ele + element = self.find_element(*sentence) + self.element_appear(element, '#F8BE5F') + return element def click(self, element): """ - Click element for desktop version. + Click element. """ - self.element_prominent(element, '#ff0000') + self.element_appear(element, '#FF0000') + element.click() + + def click_simu(self, element): + """ + Click element for simulate. + """ + self.element_effect(element) self.action_chains().reset_actions() self.action_chains().click(element).perform() self.wait(0.1) def touch(self, x, y): """ - Click on the coordinates for Mobile edition. + Click on the coordinate. """ self.action_chains().reset_actions() self.action_chains().move_by_offset(x, y).click().perform() @@ -766,7 +783,7 @@ class Browser(browser_webdriver): """ Enter the content to the element. """ - self.element_prominent(element, '#00b6f1') + self.element_appear(element, '#00B6F1') self.action_chains().reset_actions() self.action_chains().send_keys_to_element(element, content).perform() self.wait(0.1) @@ -775,7 +792,7 @@ class Browser(browser_webdriver): """ Park the mouse here. """ - self.element_prominent(element, '#49dc07') + self.element_appear(element, '#49DC07') self.action_chains().reset_actions() self.action_chains().move_to_element(element).perform() @@ -884,7 +901,7 @@ class Browser(browser_webdriver): 'let e=arguments[0];e.style.display="inline-block";e.style.visibility="visible";e.setAttribute("hidden","false");', element ) - def element_prominent(self, element, color='#ff0000', dura=2500): + def element_appear(self, element=None, color='#ff0000', dura=2500): """ Make the element highlight. """ @@ -903,7 +920,51 @@ class Browser(browser_webdriver): e.prominent=true; setTimeout(function(args){try{args[0].prominent=null;args[0].style.background=args[1][0];args[0].style.border=args[1][1]}catch(e){}},%s,[e,o]); } - }catch(e){}''' % (color, r, g, b, dura), element + }catch(e){} + ''' % (color, r, g, b, dura), element + ) + + def element_effect(self, element=None, x: int = 0, y: int = 0): + """ + Make a coordinate click effect. + """ + self.execute_script(''' + let e=arguments[0]; + let r; + let x; + let y; + if(e!==null){ + r=e.getBoundingClientRect(); + x=r.left+r.width/2+"px"; + y=r.top+r.height/2+"px"; + } + else{ + x=arguments[1]+"px"; + y=arguments[2]+"px"; + } + let c=document.createElement("div"); + c.style="width:%spx;height:%spx;border-radius:50%%;background-color:rgba(255,0,0,0.18);position:absolute;transform:translate(-50%%,-50%%);transition:opacity 0.5s;border:1px solid #ff3c3c;pointer-events:none"; + c.style.zIndex=9999; + c.style.left=x;c.style.top=y; + document.body.appendChild(c); + setTimeout(function(){c.style.opacity=0;setTimeout(function(){document.body.removeChild(c)},999)},200); + let w=%s; + let h=%s; + let d=false; + let i=setInterval(function(){ + if((w>%s||h>%s)||d){ + d=true; + w-=2; + h-=2; + } + else{ + w+=5; + h+=5; + } + c.style.width=w+"px";c.style.height=h+"px"; + if((w<=12||h<=12)&&d){clearInterval(i)} + },20); + ''' % (0, 0, 0, 0, 30, 30), element, x, y ) def webdriver_wait(self, timeout: float, poll_frequency: float = 0.5, ignored_exceptions=None): diff --git a/pytest.ini b/pytest.ini index dfc806f..0191b30 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,6 +1,6 @@ [pytest] testpaths = Testcase -addopts = -vs --strict-markers --reruns=2 --reruns-delay=1 +addopts = -vs --strict-markers --reruns=0 --reruns-delay=1 log_cli = 1 pythonpath = ./ cache_dir = pytest_cache