0
0
Selenium Pythontesting~10 mins

Retry mechanism for flaky tests in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to retry a flaky test up to 3 times.

Selenium Python
for attempt in range([1]):
    try:
        run_flaky_test()
        break
    except Exception:
        if attempt == 2:
            raise
Drag options to blanks, or click blank then click option'
A0
B1
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 means no retries, only one attempt.
Using 0 means the loop never runs.
2fill in blank
medium

Complete the code to catch the exception type for flaky test failures.

Selenium Python
try:
    flaky_test()
except [1]:
    print('Test failed, retrying...')
Drag options to blanks, or click blank then click option'
ATimeoutException
BKeyError
CValueError
DIndexError
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValueError or KeyError which are unrelated to Selenium waits.
3fill in blank
hard

Fix the error in the retry decorator to retry flaky tests.

Selenium Python
def retry_test(func):
    def wrapper(*args, **kwargs):
        for _ in range(3):
            try:
                return func(*args, **kwargs)
            except [1]:
                pass
        raise Exception('Test failed after retries')
    return wrapper
Drag options to blanks, or click blank then click option'
ATimeoutException
BException
CAssertionError
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching only TimeoutException causes some failures to not retry.
4fill in blank
hard

Fill both blanks to implement a retry loop with delay between attempts.

Selenium Python
import time

for attempt in range([1]):
    try:
        flaky_test()
        break
    except Exception:
        if attempt < [2]:
            time.sleep(2)
        else:
            raise
Drag options to blanks, or click blank then click option'
A3
B2
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Sleeping after last attempt wastes time.
Using 0 or 1 for retry count is too low.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that retries tests and stores pass status.

Selenium Python
results = { [1]: (run_test(test) == True) for test in tests if test [2] '5' and test [3] '10' }
Drag options to blanks, or click blank then click option'
Atest
B>
C<
Dtest.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using test as key without transformation.
Using wrong comparison operators.