Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 means no retries, only one attempt.
Using 0 means the loop never runs.
✗ Incorrect
The loop should run 3 times to retry the flaky test up to 3 attempts.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValueError or KeyError which are unrelated to Selenium waits.
✗ Incorrect
TimeoutException is common for flaky Selenium tests due to delays.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Catching only TimeoutException causes some failures to not retry.
✗ Incorrect
Using Exception catches all errors to retry any failure.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Sleeping after last attempt wastes time.
Using 0 or 1 for retry count is too low.
✗ Incorrect
Retry 3 times and sleep between first 2 attempts only.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using test as key without transformation.
Using wrong comparison operators.
✗ Incorrect
Use test.upper() as key, filter tests greater than 5 and less than 10.