How to use wait_for_partial_link_text method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...194 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:195 timeout = self._get_new_timeout(timeout)196 if self.browser == 'phantomjs':197 if self.is_partial_link_text_visible(partial_link_text):198 element = self.wait_for_partial_link_text(partial_link_text)199 element.click()200 return201 source = self.driver.page_source202 soup = BeautifulSoup(source)203 html_links = soup.fetch('a')204 for html_link in html_links:205 if partial_link_text in html_link.text:206 for html_attribute in html_link.attrs:207 if html_attribute[0] == 'href':208 href = html_attribute[1]209 if href.startswith('//'):210 link = "http:" + href211 elif href.startswith('/'):212 url = self.driver.current_url213 domain_url = self.get_domain_url(url)214 link = domain_url + href215 else:216 link = href217 self.open(link)218 return219 raise Exception(220 'Could not parse link from partial link_text '221 '[%s]' % partial_link_text)222 raise Exception(223 "Partial link text [%s] was not found!" % partial_link_text)224 # Not using phantomjs225 element = self.wait_for_partial_link_text(226 partial_link_text, timeout=timeout)227 self._demo_mode_highlight_if_active(228 partial_link_text, by=By.PARTIAL_LINK_TEXT)229 pre_action_url = self.driver.current_url230 try:231 element.click()232 except StaleElementReferenceException:233 self.wait_for_ready_state_complete()234 time.sleep(0.05)235 element = self.wait_for_partial_link_text(236 partial_link_text, timeout=timeout)237 element.click()238 if settings.WAIT_FOR_RSC_ON_CLICKS:239 self.wait_for_ready_state_complete()240 if self.demo_mode:241 if self.driver.current_url != pre_action_url:242 self._demo_mode_pause_if_active()243 else:244 self._demo_mode_pause_if_active(tiny=True)245 def get_text(self, selector, by=By.CSS_SELECTOR,246 timeout=settings.SMALL_TIMEOUT):247 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:248 timeout = self._get_new_timeout(timeout)249 self.wait_for_ready_state_complete()250 time.sleep(0.01)251 element = page_actions.wait_for_element_visible(252 self.driver, selector, by, timeout)253 try:254 element_text = element.text255 except StaleElementReferenceException:256 self.wait_for_ready_state_complete()257 time.sleep(0.06)258 element = page_actions.wait_for_element_visible(259 self.driver, selector, by, timeout)260 element_text = element.text261 return element_text262 def get_attribute(self, selector, attribute, by=By.CSS_SELECTOR,263 timeout=settings.SMALL_TIMEOUT):264 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:265 timeout = self._get_new_timeout(timeout)266 self.wait_for_ready_state_complete()267 time.sleep(0.01)268 element = page_actions.wait_for_element_present(269 self.driver, selector, by, timeout)270 try:271 attribute_value = element.get_attribute(attribute)272 except StaleElementReferenceException:273 self.wait_for_ready_state_complete()274 time.sleep(0.06)275 element = page_actions.wait_for_element_present(276 self.driver, selector, by, timeout)277 attribute_value = element.get_attribute(attribute)278 if attribute_value is not None:279 return attribute_value280 else:281 raise Exception("Element [%s] has no attribute [%s]!" % (282 selector, attribute))283 def refresh_page(self):284 self.driver.refresh()285 def get_current_url(self):286 return self.driver.current_url287 def get_page_source(self):288 return self.driver.page_source289 def get_page_title(self):290 return self.driver.title291 def go_back(self):292 self.driver.back()293 def go_forward(self):294 self.driver.forward()295 def get_image_url(self, selector, by=By.CSS_SELECTOR,296 timeout=settings.SMALL_TIMEOUT):297 """ Extracts the URL from an image element on the page. """298 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:299 timeout = self._get_new_timeout(timeout)300 return self.get_attribute(selector,301 attribute='src', by=by, timeout=timeout)302 def add_text(self, selector, new_value, by=By.CSS_SELECTOR,303 timeout=settings.SMALL_TIMEOUT):304 """ The more-reliable version of driver.send_keys()305 Similar to update_text(), but won't clear the text field first. """306 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:307 timeout = self._get_new_timeout(timeout)308 element = self.wait_for_element_visible(309 selector, by=by, timeout=timeout)310 self._demo_mode_highlight_if_active(selector, by)311 pre_action_url = self.driver.current_url312 try:313 if not new_value.endswith('\n'):314 element.send_keys(new_value)315 else:316 new_value = new_value[:-1]317 element.send_keys(new_value)318 element.send_keys(Keys.RETURN)319 if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:320 self.wait_for_ready_state_complete()321 except StaleElementReferenceException:322 self.wait_for_ready_state_complete()323 time.sleep(0.06)324 element = self.wait_for_element_visible(325 selector, by=by, timeout=timeout)326 if not new_value.endswith('\n'):327 element.send_keys(new_value)328 else:329 new_value = new_value[:-1]330 element.send_keys(new_value)331 element.send_keys(Keys.RETURN)332 if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:333 self.wait_for_ready_state_complete()334 if self.demo_mode:335 if self.driver.current_url != pre_action_url:336 self._demo_mode_pause_if_active()337 else:338 self._demo_mode_pause_if_active(tiny=True)339 def send_keys(self, selector, new_value, by=By.CSS_SELECTOR,340 timeout=settings.SMALL_TIMEOUT):341 """ Same as add_text() -> more reliable, but less name confusion. """342 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:343 timeout = self._get_new_timeout(timeout)344 self.add_text(selector, new_value, by=by, timeout=timeout)345 def update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,346 timeout=settings.SMALL_TIMEOUT, retry=False):347 """ This method updates an element's text value with a new value.348 @Params349 selector - the selector with the value to update350 new_value - the new value for setting the text field351 by - the type of selector to search by (Default: CSS)352 timeout - how long to wait for the selector to be visible353 retry - if True, use jquery if the selenium text update fails354 """355 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:356 timeout = self._get_new_timeout(timeout)357 element = self.wait_for_element_visible(358 selector, by=by, timeout=timeout)359 self._demo_mode_highlight_if_active(selector, by)360 try:361 element.clear()362 except StaleElementReferenceException:363 self.wait_for_ready_state_complete()364 time.sleep(0.06)365 element = self.wait_for_element_visible(366 selector, by=by, timeout=timeout)367 element.clear()368 self._demo_mode_pause_if_active(tiny=True)369 pre_action_url = self.driver.current_url370 try:371 if not new_value.endswith('\n'):372 element.send_keys(new_value)373 else:374 new_value = new_value[:-1]375 element.send_keys(new_value)376 element.send_keys(Keys.RETURN)377 if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:378 self.wait_for_ready_state_complete()379 except StaleElementReferenceException:380 self.wait_for_ready_state_complete()381 time.sleep(0.06)382 element = self.wait_for_element_visible(383 selector, by=by, timeout=timeout)384 element.clear()385 if not new_value.endswith('\n'):386 element.send_keys(new_value)387 else:388 new_value = new_value[:-1]389 element.send_keys(new_value)390 element.send_keys(Keys.RETURN)391 if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:392 self.wait_for_ready_state_complete()393 if (retry and element.get_attribute('value') != new_value and (394 not new_value.endswith('\n'))):395 logging.debug('update_text_value is falling back to jQuery!')396 selector = self.jq_format(selector)397 self.set_value(selector, new_value, by=by)398 if self.demo_mode:399 if self.driver.current_url != pre_action_url:400 self._demo_mode_pause_if_active()401 else:402 self._demo_mode_pause_if_active(tiny=True)403 def update_text(self, selector, new_value, by=By.CSS_SELECTOR,404 timeout=settings.SMALL_TIMEOUT, retry=False):405 """ The shorter version of update_text_value(), which406 clears existing text and adds new text into the text field.407 We want to keep the old version for backward compatibility. """408 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:409 timeout = self._get_new_timeout(timeout)410 self.update_text_value(selector, new_value, by=by,411 timeout=timeout, retry=retry)412 def is_element_present(self, selector, by=By.CSS_SELECTOR):413 if page_utils.is_xpath_selector(selector):414 by = By.XPATH415 return page_actions.is_element_present(self.driver, selector, by)416 def is_element_visible(self, selector, by=By.CSS_SELECTOR):417 if page_utils.is_xpath_selector(selector):418 by = By.XPATH419 return page_actions.is_element_visible(self.driver, selector, by)420 def is_link_text_visible(self, link_text):421 self.wait_for_ready_state_complete()422 time.sleep(0.01)423 return page_actions.is_element_visible(self.driver, link_text,424 by=By.LINK_TEXT)425 def is_partial_link_text_visible(self, partial_link_text):426 self.wait_for_ready_state_complete()427 time.sleep(0.01)428 return page_actions.is_element_visible(self.driver, partial_link_text,429 by=By.PARTIAL_LINK_TEXT)430 def is_text_visible(self, text, selector, by=By.CSS_SELECTOR):431 self.wait_for_ready_state_complete()432 time.sleep(0.01)433 if page_utils.is_xpath_selector(selector):434 by = By.XPATH435 return page_actions.is_text_visible(self.driver, text, selector, by)436 def find_visible_elements(self, selector, by=By.CSS_SELECTOR):437 if page_utils.is_xpath_selector(selector):438 by = By.XPATH439 return page_actions.find_visible_elements(self.driver, selector, by)440 def execute_script(self, script):441 return self.driver.execute_script(script)442 def execute_async_script(self, script, timeout=settings.EXTREME_TIMEOUT):443 if self.timeout_multiplier and timeout == settings.EXTREME_TIMEOUT:444 timeout = self._get_new_timeout(timeout)445 self.driver.set_script_timeout(timeout)446 return self.driver.execute_async_script(script)447 def set_window_size(self, width, height):448 self.driver.set_window_size(width, height)449 self._demo_mode_pause_if_active()450 def maximize_window(self):451 self.driver.maximize_window()452 self._demo_mode_pause_if_active()453 def activate_jquery(self):454 """ If "jQuery is not defined", use this method to activate it for use.455 This happens because jQuery is not always defined on web sites. """456 try:457 # Let's first find out if jQuery is already defined.458 self.execute_script("jQuery('html')")459 # Since that command worked, jQuery is defined. Let's return.460 return461 except Exception:462 # jQuery is not currently defined. Let's proceed by defining it.463 pass464 self.execute_script(465 '''var script = document.createElement("script"); '''466 '''script.src = "http://code.jquery.com/jquery-3.1.0.min.js"; '''467 '''document.getElementsByTagName("head")[0]'''468 '''.appendChild(script);''')469 for x in xrange(30):470 # jQuery needs a small amount of time to activate. (At most 3s)471 try:472 self.execute_script("jQuery('html')")473 return474 except Exception:475 time.sleep(0.1)476 # Since jQuery still isn't activating, give up and raise an exception477 raise Exception("Exception: WebDriver could not activate jQuery!")478 def highlight(self, selector, by=By.CSS_SELECTOR,479 loops=settings.HIGHLIGHTS, scroll=True):480 """ This method uses fancy javascript to highlight an element.481 Used during demo_mode.482 @Params483 selector - the selector of the element to find484 by - the type of selector to search by (Default: CSS)485 loops - # of times to repeat the highlight animation486 (Default: 4. Each loop lasts for about 0.18s)487 scroll - the option to scroll to the element first (Default: True)488 """489 element = self.find_element(490 selector, by=by, timeout=settings.SMALL_TIMEOUT)491 if scroll:492 self._slow_scroll_to_element(element)493 try:494 selector = self.convert_to_css_selector(selector, by=by)495 except Exception:496 # Don't highlight if can't convert to CSS_SELECTOR for jQuery497 return498 # Only get the first match499 last_syllable = selector.split(' ')[-1]500 if ':' not in last_syllable:501 selector += ':first'502 o_bs = '' # original_box_shadow503 style = element.get_attribute('style')504 if style:505 if 'box-shadow: ' in style:506 box_start = style.find('box-shadow: ')507 box_end = style.find(';', box_start) + 1508 original_box_shadow = style[box_start:box_end]509 o_bs = original_box_shadow510 script = """jQuery('%s').css('box-shadow',511 '0px 0px 6px 6px rgba(128, 128, 128, 0.5)');""" % selector512 try:513 self.execute_script(script)514 except Exception:515 self.activate_jquery()516 self.execute_script(script)517 if self.highlights:518 loops = self.highlights519 loops = int(loops)520 for n in xrange(loops):521 script = """jQuery('%s').css('box-shadow',522 '0px 0px 6px 6px rgba(255, 0, 0, 1)');""" % selector523 self.execute_script(script)524 time.sleep(0.02)525 script = """jQuery('%s').css('box-shadow',526 '0px 0px 6px 6px rgba(128, 0, 128, 1)');""" % selector527 self.execute_script(script)528 time.sleep(0.02)529 script = """jQuery('%s').css('box-shadow',530 '0px 0px 6px 6px rgba(0, 0, 255, 1)');""" % selector531 self.execute_script(script)532 time.sleep(0.02)533 script = """jQuery('%s').css('box-shadow',534 '0px 0px 6px 6px rgba(0, 255, 0, 1)');""" % selector535 self.execute_script(script)536 time.sleep(0.02)537 script = """jQuery('%s').css('box-shadow',538 '0px 0px 6px 6px rgba(128, 128, 0, 1)');""" % selector539 self.execute_script(script)540 time.sleep(0.02)541 script = """jQuery('%s').css('box-shadow',542 '0px 0px 6px 6px rgba(128, 0, 128, 1)');""" % selector543 self.execute_script(script)544 time.sleep(0.02)545 script = """jQuery('%s').css('box-shadow', '%s');""" % (selector, o_bs)546 self.execute_script(script)547 time.sleep(0.065)548 def scroll_to(self, selector, by=By.CSS_SELECTOR,549 timeout=settings.SMALL_TIMEOUT):550 ''' Fast scroll to destination '''551 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:552 timeout = self._get_new_timeout(timeout)553 element = self.wait_for_element_visible(554 selector, by=by, timeout=timeout)555 try:556 self._scroll_to_element(element)557 except StaleElementReferenceException:558 self.wait_for_ready_state_complete()559 time.sleep(0.05)560 element = self.wait_for_element_visible(561 selector, by=by, timeout=timeout)562 self._scroll_to_element(element)563 def slow_scroll_to(self, selector, by=By.CSS_SELECTOR,564 timeout=settings.SMALL_TIMEOUT):565 ''' Slow motion scroll to destination '''566 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:567 timeout = self._get_new_timeout(timeout)568 element = self.wait_for_element_visible(569 selector, by=by, timeout=timeout)570 self._slow_scroll_to_element(element)571 def scroll_click(self, selector, by=By.CSS_SELECTOR):572 self.scroll_to(selector, by=by)573 self.click(selector, by=by)574 def click_xpath(self, xpath):575 self.click(xpath, by=By.XPATH)576 def jquery_click(self, selector, by=By.CSS_SELECTOR):577 if page_utils.is_xpath_selector(selector):578 by = By.XPATH579 selector = self.convert_to_css_selector(selector, by=by)580 self.wait_for_element_present(581 selector, by=by, timeout=settings.SMALL_TIMEOUT)582 if self.is_element_visible(selector, by=by):583 self._demo_mode_highlight_if_active(selector, by)584 # Only get the first match585 last_syllable = selector.split(' ')[-1]586 if ':' not in last_syllable:587 selector += ':first'588 click_script = """jQuery('%s')[0].click()""" % selector589 try:590 self.execute_script(click_script)591 except Exception:592 # The likely reason this fails is because: "jQuery is not defined"593 self.activate_jquery() # It's a good thing we can define it here594 self.execute_script(click_script)595 self._demo_mode_pause_if_active()596 def jq_format(self, code):597 return page_utils.jq_format(code)598 def get_domain_url(self, url):599 return page_utils.get_domain_url(url)600 def download_file(self, file_url, destination_folder=None):601 """ Downloads the file from the url to the destination folder.602 If no destination folder is specified, the default one is used. """603 if not destination_folder:604 destination_folder = constants.Files.DOWNLOADS_FOLDER605 page_utils._download_file_to(file_url, destination_folder)606 def save_file_as(self, file_url, new_file_name, destination_folder=None):607 """ Similar to self.download_file(), except that you get to rename the608 file being downloaded to whatever you want. """609 if not destination_folder:610 destination_folder = constants.Files.DOWNLOADS_FOLDER611 page_utils._download_file_to(612 file_url, destination_folder, new_file_name)613 def convert_xpath_to_css(self, xpath):614 return xpath_to_css.convert_xpath_to_css(xpath)615 def convert_to_css_selector(self, selector, by):616 """ This method converts a selector to a CSS_SELECTOR.617 jQuery commands require a CSS_SELECTOR for finding elements.618 This method should only be used for jQuery actions. """619 if by == By.CSS_SELECTOR:620 return selector621 elif by == By.ID:622 return '#%s' % selector623 elif by == By.CLASS_NAME:624 return '.%s' % selector625 elif by == By.NAME:626 return '[name="%s"]' % selector627 elif by == By.TAG_NAME:628 return selector629 elif by == By.XPATH:630 return self.convert_xpath_to_css(selector)631 elif by == By.LINK_TEXT:632 return 'a:contains("%s")' % selector633 elif by == By.PARTIAL_LINK_TEXT:634 return 'a:contains("%s")' % selector635 else:636 raise Exception(637 "Exception: Could not convert [%s](by=%s) to CSS_SELECTOR!" % (638 selector, by))639 def set_value(self, selector, new_value, by=By.CSS_SELECTOR,640 timeout=settings.SMALL_TIMEOUT):641 """ This method uses jQuery to update a text field. """642 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:643 timeout = self._get_new_timeout(timeout)644 if page_utils.is_xpath_selector(selector):645 by = By.XPATH646 selector = self.convert_to_css_selector(selector, by=by)647 self._demo_mode_highlight_if_active(selector, by)648 self.scroll_to(selector, by=by, timeout=timeout)649 value = json.dumps(new_value)650 # Only get the first match651 last_syllable = selector.split(' ')[-1]652 if ':' not in last_syllable:653 selector += ':first'654 set_value_script = """jQuery('%s').val(%s)""" % (selector, value)655 try:656 self.execute_script(set_value_script)657 except Exception:658 # The likely reason this fails is because: "jQuery is not defined"659 self.activate_jquery() # It's a good thing we can define it here660 self.execute_script(set_value_script)661 self._demo_mode_pause_if_active()662 def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,663 timeout=settings.SMALL_TIMEOUT):664 """ This method uses jQuery to update a text field.665 If the new_value string ends with the newline character,666 WebDriver will finish the call, which simulates pressing667 {Enter/Return} after the text is entered. """668 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:669 timeout = self._get_new_timeout(timeout)670 if page_utils.is_xpath_selector(selector):671 by = By.XPATH672 element = self.wait_for_element_visible(673 selector, by=by, timeout=timeout)674 self._demo_mode_highlight_if_active(selector, by)675 self.scroll_to(selector, by=by)676 selector = self.convert_to_css_selector(selector, by=by)677 # Only get the first match678 last_syllable = selector.split(' ')[-1]679 if ':' not in last_syllable:680 selector += ':first'681 update_text_script = """jQuery('%s').val('%s')""" % (682 selector, self.jq_format(new_value))683 try:684 self.execute_script(update_text_script)685 except Exception:686 # The likely reason this fails is because: "jQuery is not defined"687 self.activate_jquery() # It's a good thing we can define it here688 self.execute_script(update_text_script)689 if new_value.endswith('\n'):690 element.send_keys('\n')691 self._demo_mode_pause_if_active()692 def jquery_update_text(self, selector, new_value, by=By.CSS_SELECTOR,693 timeout=settings.SMALL_TIMEOUT):694 """ The shorter version of jquery_update_text_value()695 (The longer version remains for backwards compatibility.) """696 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:697 timeout = self._get_new_timeout(timeout)698 self.jquery_update_text_value(699 selector, new_value, by=by, timeout=timeout)700 def hover_on_element(self, selector, by=By.CSS_SELECTOR):701 self.wait_for_element_visible(702 selector, by=by, timeout=settings.SMALL_TIMEOUT)703 self._demo_mode_highlight_if_active(selector, by)704 self.scroll_to(selector, by=by)705 time.sleep(0.05) # Settle down from scrolling before hovering706 return page_actions.hover_on_element(self.driver, selector)707 def hover_and_click(self, hover_selector, click_selector,708 hover_by=By.CSS_SELECTOR, click_by=By.CSS_SELECTOR,709 timeout=settings.SMALL_TIMEOUT):710 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:711 timeout = self._get_new_timeout(timeout)712 if page_utils.is_xpath_selector(hover_selector):713 hover_by = By.XPATH714 if page_utils.is_xpath_selector(click_selector):715 click_by = By.XPATH716 self.wait_for_element_visible(717 hover_selector, by=hover_by, timeout=timeout)718 self._demo_mode_highlight_if_active(hover_selector, hover_by)719 self.scroll_to(hover_selector, by=hover_by)720 pre_action_url = self.driver.current_url721 element = page_actions.hover_and_click(722 self.driver, hover_selector, click_selector,723 hover_by, click_by, timeout)724 if self.demo_mode:725 if self.driver.current_url != pre_action_url:726 self._demo_mode_pause_if_active()727 else:728 self._demo_mode_pause_if_active(tiny=True)729 return element730 def pick_select_option_by_text(self, dropdown_selector, option,731 dropdown_by=By.CSS_SELECTOR,732 timeout=settings.SMALL_TIMEOUT):733 """ Picks an HTML <select> option by option text. """734 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:735 timeout = self._get_new_timeout(timeout)736 self._pick_select_option(dropdown_selector, option,737 dropdown_by=dropdown_by, option_by="text",738 timeout=timeout)739 def pick_select_option_by_index(self, dropdown_selector, option,740 dropdown_by=By.CSS_SELECTOR,741 timeout=settings.SMALL_TIMEOUT):742 """ Picks an HTML <select> option by option index. """743 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:744 timeout = self._get_new_timeout(timeout)745 self._pick_select_option(dropdown_selector, option,746 dropdown_by=dropdown_by, option_by="index",747 timeout=timeout)748 def pick_select_option_by_value(self, dropdown_selector, option,749 dropdown_by=By.CSS_SELECTOR,750 timeout=settings.SMALL_TIMEOUT):751 """ Picks an HTML <select> option by option value. """752 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:753 timeout = self._get_new_timeout(timeout)754 self._pick_select_option(dropdown_selector, option,755 dropdown_by=dropdown_by, option_by="value",756 timeout=timeout)757 ############758 def wait_for_element_present(self, selector, by=By.CSS_SELECTOR,759 timeout=settings.LARGE_TIMEOUT):760 """ Waits for an element to appear in the HTML of a page.761 The element does not need be visible (it may be hidden). """762 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:763 timeout = self._get_new_timeout(timeout)764 if page_utils.is_xpath_selector(selector):765 by = By.XPATH766 return page_actions.wait_for_element_present(767 self.driver, selector, by, timeout)768 def assert_element_present(self, selector, by=By.CSS_SELECTOR,769 timeout=settings.SMALL_TIMEOUT):770 """ Similar to wait_for_element_present(), but returns nothing.771 Waits for an element to appear in the HTML of a page.772 The element does not need be visible (it may be hidden).773 Returns True if successful. Default timeout = SMALL_TIMEOUT. """774 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:775 timeout = self._get_new_timeout(timeout)776 self.wait_for_element_present(selector, by=by, timeout=timeout)777 return True778 # For backwards compatibility, earlier method names of the next779 # four methods have remained even though they do the same thing,780 # with the exception of assert_*, which won't return the element,781 # but like the others, will raise an exception if the call fails.782 def wait_for_element_visible(self, selector, by=By.CSS_SELECTOR,783 timeout=settings.LARGE_TIMEOUT):784 """ Waits for an element to appear in the HTML of a page.785 The element must be visible (it cannot be hidden). """786 if page_utils.is_xpath_selector(selector):787 by = By.XPATH788 return page_actions.wait_for_element_visible(789 self.driver, selector, by, timeout)790 def wait_for_element(self, selector, by=By.CSS_SELECTOR,791 timeout=settings.LARGE_TIMEOUT):792 """ The shorter version of wait_for_element_visible() """793 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:794 timeout = self._get_new_timeout(timeout)795 return self.wait_for_element_visible(selector, by=by, timeout=timeout)796 def find_element(self, selector, by=By.CSS_SELECTOR,797 timeout=settings.LARGE_TIMEOUT):798 """ Same as wait_for_element_visible() - returns the element """799 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:800 timeout = self._get_new_timeout(timeout)801 return self.wait_for_element_visible(selector, by=by, timeout=timeout)802 def assert_element(self, selector, by=By.CSS_SELECTOR,803 timeout=settings.SMALL_TIMEOUT):804 """ Similar to wait_for_element_visible(), but returns nothing.805 As above, will raise an exception if nothing can be found.806 Returns True if successful. Default timeout = SMALL_TIMEOUT. """807 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:808 timeout = self._get_new_timeout(timeout)809 self.wait_for_element_visible(selector, by=by, timeout=timeout)810 return True811 # For backwards compatibility, earlier method names of the next812 # four methods have remained even though they do the same thing,813 # with the exception of assert_*, which won't return the element,814 # but like the others, will raise an exception if the call fails.815 def wait_for_text_visible(self, text, selector, by=By.CSS_SELECTOR,816 timeout=settings.LARGE_TIMEOUT):817 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:818 timeout = self._get_new_timeout(timeout)819 if page_utils.is_xpath_selector(selector):820 by = By.XPATH821 return page_actions.wait_for_text_visible(822 self.driver, text, selector, by, timeout)823 def wait_for_text(self, text, selector, by=By.CSS_SELECTOR,824 timeout=settings.LARGE_TIMEOUT):825 """ The shorter version of wait_for_text_visible() """826 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:827 timeout = self._get_new_timeout(timeout)828 return self.wait_for_text_visible(829 text, selector, by=by, timeout=timeout)830 def find_text(self, text, selector, by=By.CSS_SELECTOR,831 timeout=settings.LARGE_TIMEOUT):832 """ Same as wait_for_text_visible() - returns the element """833 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:834 timeout = self._get_new_timeout(timeout)835 return self.wait_for_text_visible(836 text, selector, by=by, timeout=timeout)837 def assert_text(self, text, selector, by=By.CSS_SELECTOR,838 timeout=settings.SMALL_TIMEOUT):839 """ Similar to wait_for_text_visible(), but returns nothing.840 As above, will raise an exception if nothing can be found.841 Returns True if successful. Default timeout = SMALL_TIMEOUT. """842 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:843 timeout = self._get_new_timeout(timeout)844 self.wait_for_text_visible(text, selector, by=by, timeout=timeout)845 return True846 # For backwards compatibility, earlier method names of the next847 # four methods have remained even though they do the same thing,848 # with the exception of assert_*, which won't return the element,849 # but like the others, will raise an exception if the call fails.850 def wait_for_link_text_visible(self, link_text,851 timeout=settings.LARGE_TIMEOUT):852 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:853 timeout = self._get_new_timeout(timeout)854 return self.wait_for_element_visible(855 link_text, by=By.LINK_TEXT, timeout=timeout)856 def wait_for_link_text(self, link_text, timeout=settings.LARGE_TIMEOUT):857 """ The shorter version of wait_for_link_text_visible() """858 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:859 timeout = self._get_new_timeout(timeout)860 return self.wait_for_link_text_visible(link_text, timeout=timeout)861 def find_link_text(self, link_text, timeout=settings.LARGE_TIMEOUT):862 """ Same as wait_for_link_text_visible() - returns the element """863 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:864 timeout = self._get_new_timeout(timeout)865 return self.wait_for_link_text_visible(link_text, timeout=timeout)866 def assert_link_text(self, link_text, timeout=settings.SMALL_TIMEOUT):867 """ Similar to wait_for_link_text_visible(), but returns nothing.868 As above, will raise an exception if nothing can be found.869 Returns True if successful. Default timeout = SMALL_TIMEOUT. """870 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:871 timeout = self._get_new_timeout(timeout)872 self.wait_for_link_text_visible(link_text, timeout=timeout)873 return True874 # For backwards compatibility, earlier method names of the next875 # four methods have remained even though they do the same thing,876 # with the exception of assert_*, which won't return the element,877 # but like the others, will raise an exception if the call fails.878 def wait_for_partial_link_text(self, partial_link_text,879 timeout=settings.LARGE_TIMEOUT):880 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:881 timeout = self._get_new_timeout(timeout)882 return self.wait_for_element_visible(883 partial_link_text, by=By.PARTIAL_LINK_TEXT, timeout=timeout)884 def find_partial_link_text(self, partial_link_text,885 timeout=settings.LARGE_TIMEOUT):886 """ Same as wait_for_partial_link_text() - returns the element """887 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:888 timeout = self._get_new_timeout(timeout)889 return self.wait_for_partial_link_text(890 partial_link_text, timeout=timeout)891 def assert_partial_link_text(self, partial_link_text,892 timeout=settings.SMALL_TIMEOUT):893 """ Similar to wait_for_partial_link_text(), but returns nothing.894 As above, will raise an exception if nothing can be found.895 Returns True if successful. Default timeout = SMALL_TIMEOUT. """896 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:897 timeout = self._get_new_timeout(timeout)898 self.wait_for_partial_link_text(partial_link_text, timeout=timeout)899 return True900 ############901 def wait_for_element_absent(self, selector, by=By.CSS_SELECTOR,902 timeout=settings.LARGE_TIMEOUT):903 """ Waits for an element to no longer appear in the HTML of a page.904 A hidden element still counts as appearing in the page HTML.905 If an element with "hidden" status is acceptable,906 use wait_for_element_not_visible() instead. """907 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:908 timeout = self._get_new_timeout(timeout)909 if page_utils.is_xpath_selector(selector):910 by = By.XPATH911 return page_actions.wait_for_element_absent(912 self.driver, selector, by, timeout)...

Full Screen

Full Screen

BaseCase.py

Source:BaseCase.py Github

copy

Full Screen

...139 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:140 timeout = self._get_new_timeout(timeout)141 if self.browser == 'phantomjs':142 if self.is_partial_link_text_visible(partial_link_text):143 element = self.wait_for_partial_link_text(partial_link_text)144 element.click()145 return146 source = self.driver.page_source147 soup = BeautifulSoup(source)148 html_links = soup.fetch('a')149 for html_link in html_links:150 if partial_link_text in html_link.text:151 for html_attribute in html_link.attrs:152 if html_attribute[0] == 'href':153 href = html_attribute[1]154 if href.startswith('//'):155 link = "http:" + href156 elif href.startswith('/'):157 url = self.driver.current_url158 domain_url = self.get_domain_url(url)159 link = domain_url + href160 else:161 link = href162 self.open(link)163 return164 raise Exception(165 'Could not parse link from partial link_text '166 '[%s]' % partial_link_text)167 raise Exception(168 "Partial link text [%s] was not found!" % partial_link_text)169 # Not using phantomjs170 element = self.wait_for_partial_link_text(171 partial_link_text, timeout=timeout)172 pre_action_url = self.driver.current_url173 try:174 element.click()175 except StaleElementReferenceException:176 self.wait_for_ready_state_complete()177 time.sleep(0.05)178 element = self.wait_for_partial_link_text(179 partial_link_text, timeout=timeout)180 element.click()181 if Settings.WAIT_FOR_RSC_ON_CLICKS:182 self.wait_for_ready_state_complete()183 def get_text(self, selector, by=By.CSS_SELECTOR,184 timeout=Settings.SMALL_TIMEOUT):185 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:186 timeout = self._get_new_timeout(timeout)187 self.wait_for_ready_state_complete()188 time.sleep(0.01)189 if PageUtils.is_xpath_selector(selector):190 by = By.XPATH191 element = PageActions.wait_for_element_visible(192 self.driver, selector, by, timeout)193 try:194 element_text = element.text195 except StaleElementReferenceException:196 self.wait_for_ready_state_complete()197 time.sleep(0.06)198 element = PageActions.wait_for_element_visible(199 self.driver, selector, by, timeout)200 element_text = element.text201 return element_text202 def get_attribute(self, selector, attribute, by=By.CSS_SELECTOR,203 timeout=Settings.SMALL_TIMEOUT):204 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:205 timeout = self._get_new_timeout(timeout)206 self.wait_for_ready_state_complete()207 time.sleep(0.01)208 if PageUtils.is_xpath_selector(selector):209 by = By.XPATH210 element = PageActions.wait_for_element_present(211 self.driver, selector, by, timeout)212 try:213 attribute_value = element.get_attribute(attribute)214 except StaleElementReferenceException:215 self.wait_for_ready_state_complete()216 time.sleep(0.06)217 element = PageActions.wait_for_element_present(218 self.driver, selector, by, timeout)219 attribute_value = element.get_attribute(attribute)220 if attribute_value is not None:221 return attribute_value222 else:223 raise Exception("Element [%s] has no attribute [%s]!" % (224 selector, attribute))225 def refresh_page(self):226 self.driver.refresh()227 def get_current_url(self):228 return self.driver.current_url229 def get_page_source(self):230 return self.driver.page_source231 def get_page_title(self):232 return self.driver.title233 def go_back(self):234 self.driver.back()235 def go_forward(self):236 self.driver.forward()237 def close(self):238 self.driver.close()239 def get_image_url(self, selector, by=By.CSS_SELECTOR,240 timeout=Settings.SMALL_TIMEOUT):241 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:242 timeout = self._get_new_timeout(timeout)243 return self.get_attribute(selector,244 attribute='src', by=by, timeout=timeout)245 def add_text(self, selector, new_value, by=By.CSS_SELECTOR,246 timeout=Settings.SMALL_TIMEOUT):247 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:248 timeout = self._get_new_timeout(timeout)249 element = self.wait_for_element_visible(250 selector, by=by, timeout=timeout)251 pre_action_url = self.driver.current_url252 try:253 if not new_value.endswith('\n'):254 element.send_keys(new_value)255 else:256 new_value = new_value[:-1]257 element.send_keys(new_value)258 element.send_keys(Keys.RETURN)259 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:260 self.wait_for_ready_state_complete()261 except StaleElementReferenceException:262 self.wait_for_ready_state_complete()263 time.sleep(0.06)264 element = self.wait_for_element_visible(265 selector, by=by, timeout=timeout)266 if not new_value.endswith('\n'):267 element.send_keys(new_value)268 else:269 new_value = new_value[:-1]270 element.send_keys(new_value)271 element.send_keys(Keys.RETURN)272 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:273 self.wait_for_ready_state_complete()274 def send_keys(self, selector, new_value, by=By.CSS_SELECTOR,275 timeout=Settings.SMALL_TIMEOUT):276 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:277 timeout = self._get_new_timeout(timeout)278 self.add_text(selector, new_value, by=by, timeout=timeout)279 def update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,280 timeout=Settings.SMALL_TIMEOUT, retry=False):281 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:282 timeout = self._get_new_timeout(timeout)283 element = self.wait_for_element_visible(284 selector, by=by, timeout=timeout)285 try:286 element.clear()287 except StaleElementReferenceException:288 self.wait_for_ready_state_complete()289 time.sleep(0.06)290 element = self.wait_for_element_visible(291 selector, by=by, timeout=timeout)292 element.clear()293 pre_action_url = self.driver.current_url294 try:295 if not new_value.endswith('\n'):296 element.send_keys(Keys.CONTROL + "a")297 element.send_keys(new_value)298 else:299 new_value = new_value[:-1]300 element.send_keys(Keys.CONTROL + "a")301 element.send_keys(new_value)302 element.send_keys(Keys.RETURN)303 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:304 self.wait_for_ready_state_complete()305 except StaleElementReferenceException:306 self.wait_for_ready_state_complete()307 time.sleep(0.06)308 element = self.wait_for_element_visible(309 selector, by=by, timeout=timeout)310 element.clear()311 if not new_value.endswith('\n'):312 element.send_keys(new_value)313 else:314 new_value = new_value[:-1]315 element.send_keys(new_value)316 element.send_keys(Keys.RETURN)317 if Settings.WAIT_FOR_RSC_ON_PAGE_LOADS:318 self.wait_for_ready_state_complete()319 if (retry and element.get_attribute('value') != new_value and (320 not new_value.endswith('\n'))):321 logging.debug('update_text_value is falling back to jQuery!')322 selector = self.jq_format(selector)323 self.set_value(selector, new_value, by=by)324 def update_text(self, selector, new_value, by=By.CSS_SELECTOR,325 timeout=Settings.SMALL_TIMEOUT, retry=False):326 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:327 timeout = self._get_new_timeout(timeout)328 self.update_text_value(selector, new_value, by=by,329 timeout=timeout, retry=retry)330 def js_update_date(self,selector, new_value):331 if selector.startswith('#'):332 selector=selector[1:]333 js = "document.getElementById('%s').value='%s'" % (selector, new_value)334 elif "name" in selector:335 selector = selector.split("'")[1]336 js = "document.getElementsByName('%s')[0].value='%s'" % (selector, new_value)337 self.execute_script(js)338 def is_element_present(self, selector, by=By.CSS_SELECTOR):339 if PageUtils.is_xpath_selector(selector):340 by = By.XPATH341 return PageActions.is_element_present(self.driver, selector, by)342 def is_element_visible(self, selector, by=By.CSS_SELECTOR):343 if PageUtils.is_xpath_selector(selector):344 by = By.XPATH345 return PageActions.is_element_visible(self.driver, selector, by)346 def is_element_selected(self, selector, by=By.CSS_SELECTOR):347 if PageUtils.is_xpath_selector(selector):348 by = By.XPATH349 return PageActions.is_element_selected(self.driver, selector, by)350 def is_link_text_visible(self, link_text):351 self.wait_for_ready_state_complete()352 time.sleep(0.01)353 return PageActions.is_element_visible(self.driver, link_text,354 by=By.LINK_TEXT)355 def is_partial_link_text_visible(self, partial_link_text):356 self.wait_for_ready_state_complete()357 time.sleep(0.01)358 return PageActions.is_element_visible(self.driver, partial_link_text,359 by=By.PARTIAL_LINK_TEXT)360 def is_text_visible(self, text, selector, by=By.CSS_SELECTOR):361 self.wait_for_ready_state_complete()362 time.sleep(0.01)363 if PageUtils.is_xpath_selector(selector):364 by = By.XPATH365 return PageActions.is_text_visible(self.driver, text, selector, by)366 def find_visible_elements(self, selector, by=By.CSS_SELECTOR):367 if PageUtils.is_xpath_selector(selector):368 by = By.XPATH369 return PageActions.find_visible_elements(self.driver, selector, by)370 def find_btn_and_click(self,selector,text):371 resultTag = False372 try:373 btnlist = self.find_visible_elements(selector)374 for btn in btnlist:375 if btn.text == text:376 btn.click()377 resultTag = True378 break379 finally:380 return resultTag381 def execute_script(self, script):382 return self.driver.execute_script(script)383 def execute_async_script(self, script, timeout=Settings.EXTREME_TIMEOUT):384 if self.timeout_multiplier and timeout == Settings.EXTREME_TIMEOUT:385 timeout = self._get_new_timeout(timeout)386 self.driver.set_script_timeout(timeout)387 return self.driver.execute_async_script(script)388 def set_window_size(self, width, height):389 self.driver.set_window_size(width, height)390 def maximize_window(self):391 pass392 self.driver.maximize_window()393 def activate_jquery(self):394 try:395 self.execute_script("jQuery('html')")396 return397 except Exception:398 pass399 self.execute_script(400 '''var script = document.createElement("script"); '''401 '''script.src = "http://code.jquery.com/jquery-3.1.0.min.js"; '''402 '''document.getElementsByTagName("head")[0]'''403 '''.appendChild(script);''')404 for x in range(30):405 try:406 self.execute_script("jQuery('html')")407 return408 except Exception:409 time.sleep(0.1)410 raise Exception("Exception: WebDriver could not activate jQuery!")411 def highlight(self, selector, by=By.CSS_SELECTOR,412 loops=Settings.HIGHLIGHTS, scroll=True):413 element = self.find_element(414 selector, by=by, timeout=Settings.SMALL_TIMEOUT)415 if scroll:416 self._slow_scroll_to_element(element)417 try:418 selector = self.convert_to_css_selector(selector, by=by)419 except Exception:420 return421 last_syllable = selector.split(' ')[-1]422 if ':' not in last_syllable:423 selector += ':first'424 o_bs = ''425 style = element.get_attribute('style')426 if style:427 if 'box-shadow: ' in style:428 box_start = style.find('box-shadow: ')429 box_end = style.find(';', box_start) + 1430 original_box_shadow = style[box_start:box_end]431 o_bs = original_box_shadow432 script = """jQuery('%s').css('box-shadow',433 '0px 0px 6px 6px rgba(128, 128, 128, 0.5)');""" % selector434 try:435 self.execute_script(script)436 except Exception:437 self.activate_jquery()438 self.execute_script(script)439 if self.highlights:440 loops = self.highlights441 loops = int(loops)442 for n in range(loops):443 script = """jQuery('%s').css('box-shadow',444 '0px 0px 6px 6px rgba(255, 0, 0, 1)');""" % selector445 self.execute_script(script)446 time.sleep(0.02)447 script = """jQuery('%s').css('box-shadow',448 '0px 0px 6px 6px rgba(128, 0, 128, 1)');""" % selector449 self.execute_script(script)450 time.sleep(0.02)451 script = """jQuery('%s').css('box-shadow',452 '0px 0px 6px 6px rgba(0, 0, 255, 1)');""" % selector453 self.execute_script(script)454 time.sleep(0.02)455 script = """jQuery('%s').css('box-shadow',456 '0px 0px 6px 6px rgba(0, 255, 0, 1)');""" % selector457 self.execute_script(script)458 time.sleep(0.02)459 script = """jQuery('%s').css('box-shadow',460 '0px 0px 6px 6px rgba(128, 128, 0, 1)');""" % selector461 self.execute_script(script)462 time.sleep(0.02)463 script = """jQuery('%s').css('box-shadow',464 '0px 0px 6px 6px rgba(128, 0, 128, 1)');""" % selector465 self.execute_script(script)466 time.sleep(0.02)467 script = """jQuery('%s').css('box-shadow', '%s');""" % (selector, o_bs)468 self.execute_script(script)469 time.sleep(0.065)470 def scroll_to(self, selector, by=By.CSS_SELECTOR,471 timeout=Settings.SMALL_TIMEOUT):472 ''' Fast scroll to destination '''473 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:474 timeout = self._get_new_timeout(timeout)475 element = self.wait_for_element_visible(476 selector, by=by, timeout=timeout)477 try:478 self._scroll_to_element(element)479 except StaleElementReferenceException:480 self.wait_for_ready_state_complete()481 time.sleep(0.05)482 element = self.wait_for_element_visible(483 selector, by=by, timeout=timeout)484 self._scroll_to_element(element)485 def slow_scroll_to(self, selector, by=By.CSS_SELECTOR,486 timeout=Settings.SMALL_TIMEOUT):487 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:488 timeout = self._get_new_timeout(timeout)489 element = self.wait_for_element_visible(490 selector, by=by, timeout=timeout)491 self._slow_scroll_to_element(element)492 def scroll_click(self, selector, by=By.CSS_SELECTOR):493 self.scroll_to(selector, by=by)494 self.click(selector, by=by)495 def click_xpath(self, xpath):496 self.click(xpath, by=By.XPATH)497 def jquery_click(self, selector, by=By.CSS_SELECTOR):498 if PageUtils.is_xpath_selector(selector):499 by = By.XPATH500 selector = self.convert_to_css_selector(selector, by=by)501 self.wait_for_element_present(502 selector, by=by, timeout=Settings.SMALL_TIMEOUT)503 # Only get the first match504 last_syllable = selector.split(' ')[-1]505 if ':' not in last_syllable:506 selector += ':first'507 click_script = """jQuery('%s')[0].click()""" % selector508 try:509 self.execute_script(click_script)510 except Exception:511 self.activate_jquery()512 self.execute_script(click_script)513 def jq_format(self, code):514 return PageUtils.jq_format(code)515 def get_domain_url(self, url):516 return PageUtils.get_domain_url(url)517 def download_file(self, file_url, destination_folder=None):518 if not destination_folder:519 destination_folder = Constants.Files.DOWNLOADS_FOLDER520 PageUtils._download_file_to(file_url, destination_folder)521 def save_file_as(self, file_url, new_file_name, destination_folder=None):522 if not destination_folder:523 destination_folder = Constants.Files.DOWNLOADS_FOLDER524 PageUtils._download_file_to(525 file_url, destination_folder, new_file_name)526 def convert_XpathToCss(self, xpath):527 return XpathToCss.convert_XpathToCss(xpath)528 def convert_to_css_selector(self, selector, by):529 if by == By.CSS_SELECTOR:530 return selector531 elif by == By.ID:532 return '#%s' % selector533 elif by == By.CLASS_NAME:534 return '.%s' % selector535 elif by == By.NAME:536 return '[name="%s"]' % selector537 elif by == By.TAG_NAME:538 return selector539 elif by == By.XPATH:540 return self.convert_XpathToCss(selector)541 elif by == By.LINK_TEXT:542 return 'a:contains("%s")' % selector543 elif by == By.PARTIAL_LINK_TEXT:544 return 'a:contains("%s")' % selector545 else:546 raise Exception(547 "Exception: Could not convert [%s](by=%s) to CSS_SELECTOR!" % (548 selector, by))549 def set_value(self, selector, new_value, by=By.CSS_SELECTOR,550 timeout=Settings.SMALL_TIMEOUT):551 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:552 timeout = self._get_new_timeout(timeout)553 if PageUtils.is_xpath_selector(selector):554 by = By.XPATH555 selector = self.convert_to_css_selector(selector, by=by)556 self.scroll_to(selector, by=by, timeout=timeout)557 value = json.dumps(new_value)558 last_syllable = selector.split(' ')[-1]559 if ':' not in last_syllable:560 selector += ':first'561 set_value_script = """jQuery('%s').val(%s)""" % (selector, value)562 try:563 self.execute_script(set_value_script)564 except Exception:565 self.activate_jquery() # It's a good thing we can define it here566 self.execute_script(set_value_script)567 def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,568 timeout=Settings.SMALL_TIMEOUT):569 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:570 timeout = self._get_new_timeout(timeout)571 if PageUtils.is_xpath_selector(selector):572 by = By.XPATH573 element = self.wait_for_element_visible(574 selector, by=by, timeout=timeout)575 self.scroll_to(selector, by=by)576 selector = self.convert_to_css_selector(selector, by=by)577 last_syllable = selector.split(' ')[-1]578 if ':' not in last_syllable:579 selector += ':first'580 update_text_script = """jQuery('%s').val('%s')""" % (581 selector, self.jq_format(new_value))582 try:583 self.execute_script(update_text_script)584 except Exception:585 self.activate_jquery() # It's a good thing we can define it here586 self.execute_script(update_text_script)587 if new_value.endswith('\n'):588 element.send_keys('\n')589 def jquery_update_text(self, selector, new_value, by=By.CSS_SELECTOR,590 timeout=Settings.SMALL_TIMEOUT):591 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:592 timeout = self._get_new_timeout(timeout)593 self.jquery_update_text_value(594 selector, new_value, by=by, timeout=timeout)595 def hover_on_element(self, selector, by=By.CSS_SELECTOR):596 self.wait_for_element_visible(597 selector, by=by, timeout=Settings.SMALL_TIMEOUT)598 self.scroll_to(selector, by=by)599 time.sleep(0.05)600 return PageActions.hover_on_element(self.driver, selector)601 def hover_and_click(self, hover_selector, click_selector,602 hover_by=By.CSS_SELECTOR, click_by=By.CSS_SELECTOR,603 timeout=Settings.SMALL_TIMEOUT):604 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:605 timeout = self._get_new_timeout(timeout)606 if PageUtils.is_xpath_selector(hover_selector):607 hover_by = By.XPATH608 if PageUtils.is_xpath_selector(click_selector):609 click_by = By.XPATH610 self.wait_for_element_visible(611 hover_selector, by=hover_by, timeout=timeout)612 self.scroll_to(hover_selector, by=hover_by)613 pre_action_url = self.driver.current_url614 element = PageActions.hover_and_click(615 self.driver, hover_selector, click_selector,616 hover_by, click_by, timeout)617 return element618 def pick_select_option_by_text(self, dropdown_selector, option,619 dropdown_by=By.CSS_SELECTOR,620 timeout=Settings.SMALL_TIMEOUT):621 """ Picks an HTML <select> option by option text. """622 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:623 timeout = self._get_new_timeout(timeout)624 self._pick_select_option(dropdown_selector, option,625 dropdown_by=dropdown_by, option_by="text",626 timeout=timeout)627 def pick_select_option_by_index(self, dropdown_selector, option,628 dropdown_by=By.CSS_SELECTOR,629 timeout=Settings.SMALL_TIMEOUT):630 """ Picks an HTML <select> option by option index. """631 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:632 timeout = self._get_new_timeout(timeout)633 self._pick_select_option(dropdown_selector, option,634 dropdown_by=dropdown_by, option_by="index",635 timeout=timeout)636 def pick_select_option_by_value(self, dropdown_selector, option,637 dropdown_by=By.CSS_SELECTOR,638 timeout=Settings.SMALL_TIMEOUT):639 """ Picks an HTML <select> option by option value. """640 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:641 timeout = self._get_new_timeout(timeout)642 self._pick_select_option(dropdown_selector, option,643 dropdown_by=dropdown_by, option_by="value",644 timeout=timeout)645 ############646 def wait_for_element_present(self, selector, by=By.CSS_SELECTOR,647 timeout=Settings.LARGE_TIMEOUT):648 """ Waits for an element to appear in the HTML of a page.649 The element does not need be visible (it may be hidden). """650 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:651 timeout = self._get_new_timeout(timeout)652 if PageUtils.is_xpath_selector(selector):653 by = By.XPATH654 return PageActions.wait_for_element_present(655 self.driver, selector, by, timeout)656 def assert_element_present(self, selector, by=By.CSS_SELECTOR,657 timeout=Settings.SMALL_TIMEOUT):658 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:659 timeout = self._get_new_timeout(timeout)660 self.wait_for_element_present(selector, by=by, timeout=timeout)661 return True662 def wait_for_element_visible(self, selector, by=By.CSS_SELECTOR,663 timeout=Settings.LARGE_TIMEOUT):664 if PageUtils.is_xpath_selector(selector):665 by = By.XPATH666 return PageActions.wait_for_element_visible(667 self.driver, selector, by, timeout)668 def wait_for_element(self, selector, by=By.CSS_SELECTOR,669 timeout=Settings.LARGE_TIMEOUT):670 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:671 timeout = self._get_new_timeout(timeout)672 return self.wait_for_element_visible(selector, by=by, timeout=timeout)673 def find_element(self, selector, by=By.CSS_SELECTOR,674 timeout=Settings.LARGE_TIMEOUT):675 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:676 timeout = self._get_new_timeout(timeout)677 return self.wait_for_element_visible(selector, by=by, timeout=timeout)678 def assert_element(self, selector, by=By.CSS_SELECTOR,679 timeout=Settings.SMALL_TIMEOUT):680 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:681 timeout = self._get_new_timeout(timeout)682 self.wait_for_element_visible(selector, by=by, timeout=timeout)683 return True684 def wait_for_text_visible(self, text, selector, by=By.CSS_SELECTOR,685 timeout=Settings.LARGE_TIMEOUT):686 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:687 timeout = self._get_new_timeout(timeout)688 if PageUtils.is_xpath_selector(selector):689 by = By.XPATH690 return PageActions.wait_for_text_visible(691 self.driver, text, selector, by, timeout)692 def wait_for_text(self, text, selector, by=By.CSS_SELECTOR,693 timeout=Settings.LARGE_TIMEOUT):694 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:695 timeout = self._get_new_timeout(timeout)696 return self.wait_for_text_visible(697 text, selector, by=by, timeout=timeout)698 def find_text(self, text, selector, by=By.CSS_SELECTOR,699 timeout=Settings.LARGE_TIMEOUT):700 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:701 timeout = self._get_new_timeout(timeout)702 return self.wait_for_text_visible(703 text, selector, by=by, timeout=timeout)704 def assert_text(self, text, selector, by=By.CSS_SELECTOR,705 timeout=Settings.SMALL_TIMEOUT):706 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:707 timeout = self._get_new_timeout(timeout)708 self.wait_for_text_visible(text, selector, by=by, timeout=timeout)709 return True710 def wait_for_link_text_visible(self, link_text,711 timeout=Settings.LARGE_TIMEOUT):712 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:713 timeout = self._get_new_timeout(timeout)714 return self.wait_for_element_visible(715 link_text, by=By.LINK_TEXT, timeout=timeout)716 def wait_for_link_text(self, link_text, timeout=Settings.LARGE_TIMEOUT):717 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:718 timeout = self._get_new_timeout(timeout)719 return self.wait_for_link_text_visible(link_text, timeout=timeout)720 def find_link_text(self, link_text, timeout=Settings.LARGE_TIMEOUT):721 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:722 timeout = self._get_new_timeout(timeout)723 return self.wait_for_link_text_visible(link_text, timeout=timeout)724 def assert_link_text(self, link_text, timeout=Settings.SMALL_TIMEOUT):725 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:726 timeout = self._get_new_timeout(timeout)727 self.wait_for_link_text_visible(link_text, timeout=timeout)728 return True729 def wait_for_partial_link_text(self, partial_link_text,730 timeout=Settings.LARGE_TIMEOUT):731 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:732 timeout = self._get_new_timeout(timeout)733 return self.wait_for_element_visible(734 partial_link_text, by=By.PARTIAL_LINK_TEXT, timeout=timeout)735 def find_partial_link_text(self, partial_link_text,736 timeout=Settings.LARGE_TIMEOUT):737 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:738 timeout = self._get_new_timeout(timeout)739 return self.wait_for_partial_link_text(740 partial_link_text, timeout=timeout)741 def assert_partial_link_text(self, partial_link_text,742 timeout=Settings.SMALL_TIMEOUT):743 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:744 timeout = self._get_new_timeout(timeout)745 self.wait_for_partial_link_text(partial_link_text, timeout=timeout)746 return True747 def wait_for_element_absent(self, selector, by=By.CSS_SELECTOR,748 timeout=Settings.LARGE_TIMEOUT):749 if self.timeout_multiplier and timeout == Settings.LARGE_TIMEOUT:750 timeout = self._get_new_timeout(timeout)751 if PageUtils.is_xpath_selector(selector):752 by = By.XPATH753 return PageActions.wait_for_element_absent(754 self.driver, selector, by, timeout)755 def assert_element_absent(self, selector, by=By.CSS_SELECTOR,756 timeout=Settings.SMALL_TIMEOUT):757 if self.timeout_multiplier and timeout == Settings.SMALL_TIMEOUT:758 timeout = self._get_new_timeout(timeout)759 self.wait_for_element_absent(selector, by=by, timeout=timeout)...

Full Screen

Full Screen

test_nbextensions_configurator.py

Source:test_nbextensions_configurator.py Github

copy

Full Screen

...51 nt.assert_greater(52 len(enable_links), 0, 'some nbextensions should have enable links')53 def test_04_readme_rendering(self):54 # load an nbextension UI whose readme contains an image to render55 self.wait_for_partial_link_text('dashboard').click()56 self.wait_for_selector('.nbext-readme > .panel-body img',57 'there should be an image in the readme')58 def test_05_click_page_readme_link(self):59 self.driver.find_element_by_css_selector('.nbext-page-title a').click()60 self.wait_for_selector('#render-container img',61 'there should be an image in the readme')62 def test_06_enable_tree_tab(self):63 self.driver.get(self.nbext_configurator_url)64 self.wait_for_selector(65 '.nbext-ext-row', 'an nbextension ui should load')66 # now enable the appropriate nbextension67 self.wait_for_partial_link_text(68 'dashboard'69 ).find_element_by_css_selector('.nbext-enable-toggle').click()70 self.check_extension_enabled(71 'tree', 'nbextensions_configurator/tree_tab/main',72 expected_status=True)73 def test_07_open_tree_tab(self):74 self.driver.get(self.base_url())75 self.wait_for_selector(76 '#tabs a[href$=nbextensions_configurator]').click()77 self.wait_for_selector(78 '.nbext-ext-row', 'an nbextension ui should load')79 def test_08_disable_tree_tab(self):80 # now disable the appropriate nbextension & wait for update to config81 self.wait_for_partial_link_text(82 'dashboard'83 ).find_element_by_css_selector('.nbext-enable-toggle').click()84 self.check_extension_enabled(85 'tree', 'nbextensions_configurator/tree_tab/main',86 expected_status=False)87 self.driver.get(self.base_url())88 with nt.assert_raises(AssertionError):89 self.wait_for_selector('#tabs a[href$=nbextensions_configurator]')90 def test_09_no_unconfigurable_yet(self):91 self.driver.get(self.nbext_configurator_url)92 self.wait_for_selector(93 '.nbext-ext-row', 'an nbextension ui should load')94 selector = self.driver.find_element_by_css_selector('.nbext-selector')95 nt.assert_not_in(96 'daemon', selector.text,97 'There should be no daemons in the selector yet')98 def test_10_refresh_list(self):99 # 'enable' a fake nbextension100 section, require = 'notebook', 'balrog/daemon'101 self.set_extension_enabled(section, require, True)102 # refresh the list to check that it appears103 self.wait_for_selector('.nbext-button-refreshlist').click()104 self.wait_for_partial_link_text(require)105 selector = self.driver.find_element_by_css_selector('.nbext-selector')106 nt.assert_in(107 'daemon', selector.text,108 'There should now be a daemon in the selector')109 def test_11_allow_configuring_incompatibles(self):110 require = 'balrog/daemon'111 # allow configuring incompatibles112 self.wait_for_selector(113 '#nbext_hide_incompat',114 'potentially incompatible nbextensions should show checkbox'115 ).click()116 # select it, now it's configurable117 self.driver.find_element_by_partial_link_text(require).click()118 def test_12_unconfigurable(self):119 section, require = 'notebook', 'balrog/daemon'120 sel_disable = '.nbext-enable-btns .btn:nth-child(2)'121 sel_forget = '.nbext-enable-btns .btn:nth-child(3)'122 # wait for ui to load123 self.wait_for_xpath('//h3[contains(text(), "daemon")]')124 # wait a second for the other nbextension ui to hide125 time.sleep(1)126 # there should be no forget button visible yet127 with nt.assert_raises(NoSuchElementException):128 self.driver.find_element_by_css_selector(sel_forget)129 # disable balrog130 self.wait_for_selector(sel_disable)131 visible_disablers = [132 el for el in self.driver.find_elements_by_css_selector(sel_disable)133 if el.is_displayed()]134 nt.assert_equal(1, len(visible_disablers),135 'Only one disable button should be visible')136 visible_disablers[0].click()137 # now forget it138 self.wait_for_selector(sel_forget, 'A forget button should display')139 self.driver.find_element_by_css_selector(sel_forget).click()140 # confirm dialog141 self.wait_for_selector(142 '.modal-dialog .modal-footer .btn-danger',143 'a confirmation dialog should show'144 ).click()145 # it should no longer be enabled in config146 time.sleep(1)147 conf = self.get_config_manager().get(section)148 stat = conf.get('load_extensions', {}).get(require)149 nt.assert_is_none(150 stat, '{} should not have a load_extensions entry'.format(require))151 # and should no longer show in the list152 self.wait_for_selector(153 '.nbext-selector nav ul li', 'some nbextensions should show')154 nbext_sel = self.driver.find_element_by_css_selector('.nbext-selector')155 nt.assert_not_in(156 'daemon', nbext_sel.text,157 'There should no longer be a daemon in the selector')158 def test_13_duplicate_paths(self):159 if getattr(self, 'notebook', None) is None:160 raise SkipTest('cannot edit notebook server nbextensions path')161 # duplicate the dodgy/test entry on path162 saved = list(self.notebook.web_app.settings['nbextensions_path'])163 nt.assert_in(self.jupyter_dirs['dodgy']['nbexts'], saved)164 self.notebook.web_app.settings['nbextensions_path'].append(165 self.jupyter_dirs['dodgy']['nbexts'])166 try:167 self.driver.get(self.nbext_configurator_url)168 self.wait_for_selector('.nbext-selector')169 self.wait_for_partial_link_text('dummy').click()170 dummy = self.wait_for_xpath('''171//h3[contains(text(), "dummy")]172//ancestor::div[173 contains(concat(" ", normalize-space(@class), " "), " nbext-ext-row ")174]''')175 with nt.assert_raises(NoSuchElementException):176 dummy.find_element_by_css_selector('.alert-warning')177 with nt.assert_raises(NoSuchElementException):178 dummy.find_element_by_xpath(179 './*[contains(text(),"different yaml files")]')180 finally:181 self.notebook.web_app.settings['nbextensions_path'] = saved182 @classmethod183 def get_config_manager(cls):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful