def wait_for_element_present(self, selector, by=By.CSS_SELECTOR, timeout=settings.LARGE_TIMEOUT): """ Waits for an element to appear in the HTML of a page. The element does not need be visible (it may be hidden). """ if selector.startswith('/') or selector.startswith('./'): by = By.XPATH return page_actions.wait_for_element_present( self.driver, selector, by, timeout)
def wait_for_element_present(self, selector, by=By.CSS_SELECTOR, timeout=settings.LARGE_TIMEOUT): """ Waits for an element to appear in the HTML of a page. The element does not need be visible (it may be hidden). """ if selector.startswith('/') or selector.startswith('./'): by = By.XPATH return page_actions.wait_for_element_present(self.driver, selector, by, timeout)
def get_attribute(self, selector, attribute, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT): element = page_actions.wait_for_element_present( self.driver, selector, by, timeout) attribute_value = element.get_attribute(attribute) if attribute_value is not None: return attribute_value else: raise Exception("Element [%s] has no attribute [%s]!" % ( selector, attribute))
def get_attribute(self, selector, attribute, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT): element = page_actions.wait_for_element_present( self.driver, selector, by, timeout) attribute_value = element.get_attribute(attribute) if attribute_value is not None: return attribute_value else: raise Exception("Element [%s] has no attribute [%s]!" % (selector, attribute))
def play_introjs_tour(driver, tour_steps, browser, msg_dur, name=None, interval=0): """ Plays an IntroJS tour on the current website. """ instructions = "" for tour_step in tour_steps[name]: instructions += tour_step instructions += ("""] }); intro.setOption("disableInteraction", true); intro.setOption("overlayOpacity", .29); intro.setOption("scrollToElement", true); intro.setOption("keyboardNavigation", true); intro.setOption("exitOnEsc", false); intro.setOption("exitOnOverlayClick", false); intro.setOption("showStepNumbers", false); intro.setOption("showProgress", false); intro.start(); $tour = intro; }; // Start the tour startIntro(); """) autoplay = False if interval and interval > 0: autoplay = True interval = float(interval) if interval < 0.5: interval = 0.5 if not is_introjs_activated(driver): activate_introjs(driver) if len(tour_steps[name]) > 1: try: if "element: " in tour_steps[name][1]: selector = re.search( r"[\S\s]+element: '([\S\s]+)',[\S\s]+intro: '", tour_steps[name][1]).group(1) selector = selector.replace('\\', '') page_actions.wait_for_element_present( driver, selector, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT) else: selector = "html" except Exception: js_utils.post_messenger_error_message( driver, "Tour Error: {'%s'} was not found!" % selector, msg_dur, duration=settings.SMALL_TIMEOUT) raise Exception("Tour Error: {'%s'} was not found! " "Exiting due to failure on first tour step!" "" % selector) driver.execute_script(instructions) tour_on = True if autoplay: start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) latest_step = 0 while tour_on: try: time.sleep(0.01) if browser != "firefox": result = driver.execute_script("return $tour._currentStep") else: page_actions.wait_for_element_present(driver, ".introjs-tooltip", by=By.CSS_SELECTOR, timeout=0.4) result = True except Exception: tour_on = False result = None if result is not None: tour_on = True if autoplay: try: current_step = driver.execute_script( "return $tour._currentStep") except Exception: continue if current_step != latest_step: latest_step = current_step start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) now_ms = time.time() * 1000.0 if now_ms >= stop_ms: if current_step == latest_step: driver.execute_script("return $tour.nextStep()") try: latest_step = driver.execute_script( "return $tour._currentStep") start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) except Exception: pass continue else: try: time.sleep(0.01) if browser != "firefox": result = driver.execute_script("return $tour._currentStep") else: page_actions.wait_for_element_present(driver, ".introjs-tooltip", by=By.CSS_SELECTOR, timeout=0.4) result = True if result is not None: time.sleep(0.1) continue else: return except Exception: tour_on = False time.sleep(0.1)
def play_hopscotch_tour(driver, tour_steps, browser, msg_dur, name=None, interval=0): """ Plays a Hopscotch tour on the current website. """ instructions = "" for tour_step in tour_steps[name]: instructions += tour_step instructions += ("""] }; // Start the tour! hopscotch.startTour(tour); $tour = hopscotch;""") autoplay = False if interval and interval > 0: autoplay = True interval = float(interval) if interval < 0.5: interval = 0.5 if not is_hopscotch_activated(driver): activate_hopscotch(driver) if len(tour_steps[name]) > 1: try: if "target: " in tour_steps[name][1]: selector = re.search( r"[\S\s]+target: '([\S\s]+)',[\S\s]+title: '", tour_steps[name][1]).group(1) selector = selector.replace('\\', '').replace(':first', '') page_actions.wait_for_element_present( driver, selector, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT) else: selector = "html" except Exception: js_utils.post_messenger_error_message( driver, "Tour Error: {'%s'} was not found!" % selector, msg_dur, duration=settings.SMALL_TIMEOUT) raise Exception("Tour Error: {'%s'} was not found! " "Exiting due to failure on first tour step!" "" % selector) driver.execute_script(instructions) tour_on = True if autoplay: start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) latest_step = 0 while tour_on: try: time.sleep(0.01) if browser != "firefox": result = not driver.execute_script("return $tour.isActive") else: page_actions.wait_for_element_present(driver, ".hopscotch-bubble", by=By.CSS_SELECTOR, timeout=0.4) result = False except Exception: tour_on = False result = None if result is False: tour_on = True if autoplay: try: current_step = driver.execute_script( "return $tour.getCurrStepNum()") except Exception: continue if current_step != latest_step: latest_step = current_step start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) now_ms = time.time() * 1000.0 if now_ms >= stop_ms: if current_step == latest_step: driver.execute_script("return $tour.nextStep()") try: latest_step = driver.execute_script( "return $tour.getCurrStepNum()") start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) except Exception: pass continue else: try: time.sleep(0.01) if browser != "firefox": result = not driver.execute_script("return $tour.isActive") else: page_actions.wait_for_element_present(driver, ".hopscotch-bubble", by=By.CSS_SELECTOR, timeout=0.4) result = False if result is False: time.sleep(0.1) continue else: return except Exception: tour_on = False time.sleep(0.1)
def play_bootstrap_tour(driver, tour_steps, browser, msg_dur, name=None, interval=0): """ Plays a Bootstrap tour on the current website. """ instructions = "" for tour_step in tour_steps[name]: instructions += tour_step instructions += ("""]); // Initialize the tour tour.init(); // Start the tour tour.start(); // Fix timing issue by restarting tour immediately tour.restart(); // Save for later $tour = tour;""") if interval and interval > 0: if interval < 1: interval = 1 interval = str(float(interval) * 1000.0) instructions = instructions.replace('duration: 0,', 'duration: %s,' % interval) if not is_bootstrap_activated(driver): activate_bootstrap(driver) if len(tour_steps[name]) > 1: try: if "element: " in tour_steps[name][1]: selector = re.search( r"[\S\s]+element: '([\S\s]+)',[\S\s]+title: '", tour_steps[name][1]).group(1) selector = selector.replace('\\', '').replace(':first', '') page_actions.wait_for_element_present( driver, selector, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT) else: selector = "html" except Exception: js_utils.post_messenger_error_message( driver, "Tour Error: {'%s'} was not found!" % selector, msg_dur, duration=settings.SMALL_TIMEOUT) raise Exception("Tour Error: {'%s'} was not found! " "Exiting due to failure on first tour step!" "" % selector) driver.execute_script(instructions) tour_on = True while tour_on: try: time.sleep(0.01) if browser != "firefox": result = driver.execute_script("return $tour.ended()") else: page_actions.wait_for_element_present(driver, ".tour-tour", by=By.CSS_SELECTOR, timeout=0.4) result = False except Exception: tour_on = False result = None if result is False: tour_on = True else: try: time.sleep(0.01) if browser != "firefox": result = driver.execute_script("return $tour.ended()") else: page_actions.wait_for_element_present(driver, ".tour-tour", by=By.CSS_SELECTOR, timeout=0.4) result = False if result is False: time.sleep(0.1) continue else: return except Exception: tour_on = False time.sleep(0.1)
def play_shepherd_tour(driver, tour_steps, msg_dur, name=None, interval=0): """ Plays a Shepherd tour on the current website. """ instructions = "" for tour_step in tour_steps[name]: instructions += tour_step instructions += (""" // Start the tour tour.start(); $tour = tour;""") autoplay = False if interval and interval > 0: autoplay = True interval = float(interval) if interval < 0.5: interval = 0.5 if not is_shepherd_activated(driver): activate_shepherd(driver) if len(tour_steps[name]) > 1: try: selector = re.search(r"[\S\s]+{element: '([\S\s]+)', on: [\S\s]+", tour_steps[name][1]).group(1) selector = selector.replace('\\', '') page_actions.wait_for_element_present( driver, selector, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT) except Exception: js_utils.post_messenger_error_message( driver, "Tour Error: {'%s'} was not found!" % selector, msg_dur, duration=settings.SMALL_TIMEOUT) raise Exception("Tour Error: {'%s'} was not found! " "Exiting due to failure on first tour step!" "" % selector) driver.execute_script(instructions) tour_on = True if autoplay: start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) latest_element = None latest_text = None while tour_on: try: time.sleep(0.01) result = driver.execute_script( "return Shepherd.activeTour.currentStep.isOpen()") except Exception: tour_on = False result = None if result: tour_on = True if autoplay: try: element = driver.execute_script( "return Shepherd.activeTour.currentStep" ".options.attachTo.element") shep_text = driver.execute_script( "return Shepherd.activeTour.currentStep" ".options.text") except Exception: continue if element != latest_element or shep_text != latest_text: latest_element = element latest_text = shep_text start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) now_ms = time.time() * 1000.0 if now_ms >= stop_ms: if ((element == latest_element) and (shep_text == latest_text)): driver.execute_script("Shepherd.activeTour.next()") try: latest_element = driver.execute_script( "return Shepherd.activeTour.currentStep" ".options.attachTo.element") latest_text = driver.execute_script( "return Shepherd.activeTour.currentStep" ".options.text") start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) except Exception: pass continue else: try: time.sleep(0.01) selector = driver.execute_script( "return Shepherd.activeTour" ".currentStep.options.attachTo.element") try: js_utils.wait_for_css_query_selector( driver, selector, timeout=settings.SMALL_TIMEOUT) except Exception: remove_script = ("jQuery('%s').remove()" % "div.shepherd-content") driver.execute_script(remove_script) js_utils.post_messenger_error_message( driver, "Tour Error: {'%s'} was not found!" % selector, msg_dur, duration=settings.SMALL_TIMEOUT) time.sleep(0.1) driver.execute_script("Shepherd.activeTour.next()") if autoplay: start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) tour_on = True except Exception: tour_on = False time.sleep(0.1)
def wait_for_element_present(self, selector, by=By.CSS_SELECTOR, timeout=settings.LARGE_TIMEOUT): if selector.startswith('/') or selector.startswith('./'): by = By.XPATH return page_actions.wait_for_element_present( self.driver, selector, by, timeout)
def play_driverjs_tour(driver, tour_steps, browser, msg_dur, name=None, interval=0): """ Plays a DriverJS tour on the current website. """ instructions = "" for tour_step in tour_steps[name]: instructions += tour_step instructions += ("""] ); // Start the tour! tour.start(); $tour = tour;""") autoplay = False if interval and interval > 0: autoplay = True interval = float(interval) if interval < 0.5: interval = 0.5 if not is_driverjs_activated(driver): activate_driverjs(driver) if len(tour_steps[name]) > 1: try: if "element: " in tour_steps[name][1]: selector = re.search( r"[\S\s]+element: '([\S\s]+)',[\S\s]+popover: {", tour_steps[name][1]).group(1) selector = selector.replace('\\', '').replace(':first', '') page_actions.wait_for_element_present( driver, selector, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT) else: selector = "html" except Exception: js_utils.post_messenger_error_message( driver, "Tour Error: {'%s'} was not found!" % selector, msg_dur) raise Exception("Tour Error: {'%s'} was not found! " "Exiting due to failure on first tour step!" "" % selector) driver.execute_script(instructions) driver.execute_script( 'document.querySelector(".driver-next-btn").focus();') tour_on = True if autoplay: start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) latest_step = 0 while tour_on: try: time.sleep(0.01) if browser != "firefox": result = not driver.execute_script("return $tour.isActivated") else: page_actions.wait_for_element_present(driver, "#driver-popover-item", by=By.CSS_SELECTOR, timeout=0.4) result = False except Exception: tour_on = False result = None if result is False: tour_on = True driver.execute_script( 'document.querySelector(".driver-next-btn").focus();') if autoplay: try: current_step = driver.execute_script( "return $tour.currentStep") except Exception: continue if current_step != latest_step: latest_step = current_step start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) now_ms = time.time() * 1000.0 if now_ms >= stop_ms: if current_step == latest_step: driver.execute_script("$tour.moveNext()") try: latest_step = driver.execute_script( "return $tour.currentStep") start_ms = time.time() * 1000.0 stop_ms = start_ms + (interval * 1000.0) except Exception: pass continue else: try: time.sleep(0.01) if browser != "firefox": result = not driver.execute_script( "return $tour.isActivated") else: page_actions.wait_for_element_present( driver, "#driver-popover-item", by=By.CSS_SELECTOR, timeout=0.4) result = False if result is False: time.sleep(0.1) continue else: return except Exception: tour_on = False time.sleep(0.1)