尽管测试重试并返回true,断言仍会失败

时间:2016-03-29 15:38:51

标签: python python-2.7 selenium nose

似乎每当一个元素无法找到,并且它进入重试循环,即使一次,断言也会失败,尽管在测试失败3次之前没有“返回False”。

def find_element(context, element_id, retry=0):
    # Generate string list of IDs for printing
    if isinstance(element_id, list):
        element_name = '#' + ' or #'.join(element_id)
    else:
        element_name = '#' + element_id

    try:
        # Print list of IDs with "or" separator, if id is list
        info('Locate element: ' + element_name)

        # Wait 10 seconds for element to be visible
        element = element_visible(context, element_id)
        success('Element found: #' + element[0])

        success('Location: {}, Size: {}'.format(element[1].location, element[1].size))
        return True

    except TimeoutException:
        warn('Unable to find {}. Retry = {}'.format(element_name, retry))
        if retry < 2:
            # If element is not found, refresh browser loop
            refresh_browser(context)
            find_element(context, element_id, retry + 1)
        else:
            alert('Element "{}" not found'.format(element_name))
            return False

    except:
        alert('Unexpected error: {}'.format(sys.exc_info()[0]))
        raise


def assert_element(context, element_id):
    result = find_element(context, element_id)
    if isinstance(element_id, list):
        element_id = ' or '.join(element_id)
    assert_true(result, 'Element with id "{}" should appear on page'.format(element_id))

有什么可以触发失败的断言的见解?

1 个答案:

答案 0 :(得分:2)

您需要return下一次递归通话的结果:

# If element is not found, refresh browser loop
refresh_browser(context)
return find_element(context, element_id, retry + 1)  # < HERE