From ebc9fa9e2ab2abc8cfa957b85abe50985bbe6f30 Mon Sep 17 00:00:00 2001 From: JonahStanley Date: Thu, 11 Jul 2013 11:17:28 -0400 Subject: [PATCH] Made a retry_on_exception world function that will retry the given function --- .../contentstore/features/common.py | 17 ++++----- .../contentstore/features/signup.py | 20 +++++------ common/djangoapps/terrain/ui_helpers.py | 13 ++++++- lms/djangoapps/courseware/features/login.py | 29 ++++++--------- lms/djangoapps/courseware/features/signup.py | 36 +++++++------------ 5 files changed, 48 insertions(+), 67 deletions(-) diff --git a/cms/djangoapps/contentstore/features/common.py b/cms/djangoapps/contentstore/features/common.py index 4d4e099c8b..875bc3ced9 100644 --- a/cms/djangoapps/contentstore/features/common.py +++ b/cms/djangoapps/contentstore/features/common.py @@ -144,17 +144,12 @@ def log_into_studio( world.is_css_present(signin_css) world.css_click(signin_css) - attempt = 0 - while attempt < 5: - try: - login_form = world.browser.find_by_css('form#login_form') - login_form.find_by_name('email').fill(email) - login_form.find_by_name('password').fill(password) - login_form.find_by_name('submit').click() - break - except: - attempt += 1 - + def fill_login_form(): + login_form = world.browser.find_by_css('form#login_form') + login_form.find_by_name('email').fill(email) + login_form.find_by_name('password').fill(password) + login_form.find_by_name('submit').click() + world.retry_on_exception(fill_login_form) assert_true(world.is_css_present('.new-course-button')) diff --git a/cms/djangoapps/contentstore/features/signup.py b/cms/djangoapps/contentstore/features/signup.py index 68414389e4..49a305f70b 100644 --- a/cms/djangoapps/contentstore/features/signup.py +++ b/cms/djangoapps/contentstore/features/signup.py @@ -7,18 +7,14 @@ from common import * @step('I fill in the registration form$') def i_fill_in_the_registration_form(step): - attempt = 0 - while attempt < 5: - try: - register_form = world.css_find('form#register_form') - register_form.find_by_name('email').fill('robot+studio@edx.org') - register_form.find_by_name('password').fill('test') - register_form.find_by_name('username').fill('robot-studio') - register_form.find_by_name('name').fill('Robot Studio') - register_form.find_by_name('terms_of_service').check() - break - except: - attempt += 1 + def fill_in_reg_form(): + register_form = world.css_find('form#register_form') + register_form.find_by_name('email').fill('robot+studio@edx.org') + register_form.find_by_name('password').fill('test') + register_form.find_by_name('username').fill('robot-studio') + register_form.find_by_name('name').fill('Robot Studio') + register_form.find_by_name('terms_of_service').check() + world.retry_on_exception(fill_in_reg_form) @step('I press the Create My Account button on the registration form$') diff --git a/common/djangoapps/terrain/ui_helpers.py b/common/djangoapps/terrain/ui_helpers.py index 076384d30c..46fe97548a 100644 --- a/common/djangoapps/terrain/ui_helpers.py +++ b/common/djangoapps/terrain/ui_helpers.py @@ -249,7 +249,7 @@ def css_visible(css_selector, index=0, max_attempts=5): attempt = 0 while attempt < max_attempts: try: - return world.browser.find_by_css(css_selector)[index].visible + return except: attempt += 1 assert_true(attempt < max_attempts, 'Ran out of attempts to access {}'.format(css_selector)) @@ -299,3 +299,14 @@ def click_tools(): @world.absorb def is_mac(): return platform.mac_ver()[0] is not '' + + +@world.absorb +def retry_on_exception(func, max_attempts=5): + attempts = 0 + while attempts < max_attempts: + try: + return func() + break + except: + attempts += 1 diff --git a/lms/djangoapps/courseware/features/login.py b/lms/djangoapps/courseware/features/login.py index 25260c3256..6a9a7c9e28 100644 --- a/lms/djangoapps/courseware/features/login.py +++ b/lms/djangoapps/courseware/features/login.py @@ -20,15 +20,11 @@ def i_am_an_activated_user(step): def i_submit_my_credentials_on_the_login_form(step): fill_in_the_login_form('email', 'robot@edx.org') fill_in_the_login_form('password', 'test') - attempt = 0 - while attempt < 5: - try: - login_form = world.browser.find_by_css('form#login-form') - login_form.find_by_name('submit').click() - break - except: - attempt += 1 - assert_true(attempt < 5, 'Login form could not be clicked') + + def submit_login_form(): + login_form = world.browser.find_by_css('form#login-form') + login_form.find_by_name('submit').click() + world.retry_on_excetion(submit_login_form) @step(u'I should see the login error message "([^"]*)"$') @@ -57,13 +53,8 @@ def user_is_an_activated_user(uname): def fill_in_the_login_form(field, value): - attempt = 0 - while attempt < 5: - try: - login_form = world.browser.find_by_css('form#login-form') - form_field = login_form.find_by_name(field) - form_field.fill(value) - break - except: - attempt += 1 - assert_true(attempt < 5, 'Login form could not be filled') + def fill_login_form(): + login_form = world.browser.find_by_css('form#login-form') + form_field = login_form.find_by_name(field) + form_field.fill(value) + world.retry_on_excetion(fill_login_form) diff --git a/lms/djangoapps/courseware/features/signup.py b/lms/djangoapps/courseware/features/signup.py index 0dfdae02b1..ef864aa855 100644 --- a/lms/djangoapps/courseware/features/signup.py +++ b/lms/djangoapps/courseware/features/signup.py @@ -6,38 +6,26 @@ from lettuce import world, step @step('I fill in "([^"]*)" on the registration form with "([^"]*)"$') def when_i_fill_in_field_on_the_registration_form_with_value(step, field, value): - attempt = 0 - while attempt < 5: - try: - register_form = world.browser.find_by_css('form#register-form') - form_field = register_form.find_by_name(field) - form_field.fill(value) - break - except: - attempt += 1 + def fill_in_registration(): + register_form = world.browser.find_by_css('form#register-form') + form_field = register_form.find_by_name(field) + form_field.fill(value) + world.retry_on_exception(fill_in_registration) @step('I submit the registration form$') def i_press_the_button_on_the_registration_form(step): - attempt = 0 - while attempt < 5: - try: - register_form = world.browser.find_by_css('form#register-form') - register_form.find_by_name('submit').click() - break - except: - attempt += 1 + def submit_registration(): + register_form = world.browser.find_by_css('form#register-form') + register_form.find_by_name('submit').click() + world.retry_on_exception(submit_registration) @step('I check the checkbox named "([^"]*)"$') def i_check_checkbox(step, checkbox): - attempt = 0 - while attempt < 5: - try: - world.browser.find_by_name(checkbox).check() - break - except: - attempt += 1 + def check_box(): + world.browser.find_by_name(checkbox).check() + world.retry_on_exception(check_box) @step('I should see "([^"]*)" in the dashboard banner$')