Search code examples
pythonpython-2.7seleniumnose

Assertion fails despite test retrying and returning true


It appears that whenever an element is unable to be found, and it proceeds to the retry loop, even once, the assertion fails, despite there being no "return False" until the test fails 3 times.

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))

Any insight as to what could be triggering a failed assertion?


Solution

  • You need to return the result of your next recursive call:

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