def wait_for_many_elements(selector, timeout=5, interval=0.5, for_at_least=1): if selector.startswith("//"): find_function = lambda x: world.dom.xpath(x) selector_type = 'xpath' else: find_function = lambda x: world.dom.cssselect(x) selector_type = 'css' def element_is_there(): raw = world.browser.html.strip() if raw.startswith('<?xml'): raw = unicode(re.sub(r'[<][?]xml[^>]+[>]', '', raw)) try: world.dom = html.fromstring(raw) except WebDriverException: return False return find_function(selector) element_is_there.__doc__ = \ "element %s to be in the DOM (not necessarily visible)" % selector world.wait_for_condition(element_is_there) found = find_function(selector) assert len(found) >= for_at_least, \ 'could not find {2} occurrence(s) of the {3} selector "{0}" not found within {1} seconds'.format(selector, timeout, for_at_least, selector_type) if selector_type == 'xpath': return world.browser.find_by_xpath(selector) return world.browser.find_by_css(selector)
def wait_until_hidden(selector, timeout=5, interval=0.5): found = world.wait_for_many_elements(selector, timeout, interval) def element_become_hidden(): sel = 'jQuery("{0}:visible").length'.format(selector) return int(world.browser.evaluate_script(sel)) == 0 world.wait_for_condition(element_become_hidden)
def __exit__(self, type, value, traceback): def ajax_ready(): jscript = 'jQuery("#{0}.ready").length'.format(self.sid) evaluated = world.browser.evaluate_script(jscript) return int(evaluated) > 0 world.wait_for_condition(ajax_ready, 10)
def check_existence_of_object_with_the_next_info(step): form_data = step.hashes kwargs = {} for data in form_data: name = data['name'] value = data['value'] kwargs[name] = value condition = lambda: Phase.objects.filter(**kwargs).exists() world.wait_for_condition(condition)
def do_not_see_some_tag(step, tag_name): def tag_was_deleted(): selector = '//span[@class="tag-name" and contains(., "%s")]' % tag_name try: tag = world.wait_for_element(selector, 5, 0.5) except AssertionError: tag = None return tag is None world.wait_for_condition(tag_was_deleted)
def click_on_a_story(step): story = world.wait_for_element("div.story-header a", 5, 0.5) assert story, 'There are no stories.' world.story = world.wait_for_element("div.story-header a", 5, 0.5).value def ensure_story_opens(): loaded = None try: link = world.browser.find_link_by_text(world.story).first.click() except: loaded = world.wait_for_element('#story-main') return loaded is not None world.wait_for_condition(ensure_story_opens, 10, 0.1)
def click_on_a_button_of_a_phase(step, button_name, number): number = int(number) if button_name.lower() == u'edit': nth = 1 elif button_name.lower() == u'delete': nth = 2 else: assert False, 'Invalid button for phase' def ensure_dialog_loads(): world.browser.execute_script('$(".phase-controls").css' '("visibility","visible")') button = world.wait_for_many_elements('div.phase button')[number * 2 - 3 + nth] assert button, 'There is no %s button for phase %s' % (button_name, number) button.click() form = world.wait_for_element('#%s-phase-dialog' % button_name.lower()) try: assert form.visible except AssertionError: pass return form.visible world.wait_for_condition(ensure_dialog_loads)