diff --git a/lms/djangoapps/courseware/features/problems.py b/lms/djangoapps/courseware/features/problems.py index c1b634783d..1985847bd3 100644 --- a/lms/djangoapps/courseware/features/problems.py +++ b/lms/djangoapps/courseware/features/problems.py @@ -4,17 +4,10 @@ from selenium.webdriver.support.ui import Select import random from common import i_am_registered_for_the_course -problem_urls = { 'drop down': '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/Drop_Down_Problems', - 'multiple choice': '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/Multiple_Choice_Problems', - 'checkbox': '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/Checkbox_Problems', - 'string': '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/String_Problems', - 'numerical': '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/Numerical_Problems', - 'formula': '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/Formula_Problems', } - @step(u'I am viewing a "([^"]*)" problem') def view_problem(step, problem_type): i_am_registered_for_the_course(step, 'edX/model_course/2013_Spring') - url = django_url(problem_urls[problem_type]) + url = django_url(problem_url(problem_type)) world.browser.visit(url) @step(u'I answer a "([^"]*)" problem "([^"]*)ly"') @@ -28,31 +21,28 @@ def answer_problem(step, problem_type, correctness): elif problem_type == "multiple choice": if correctness == 'correct': - world.browser.find_by_css("input#input_i4x-edX-model_course-problem-Multiple_Choice_Problem_2_1_choice_choice_3").check() + inputfield('multiple choice', choice='choice_3').check() else: - world.browser.find_by_css("input#input_i4x-edX-model_course-problem-Multiple_Choice_Problem_2_1_choice_choice_2").check() + inputfield('multiple choice', choice='choice_2').check() elif problem_type == "checkbox": if correctness == 'correct': - world.browser.find_by_css('input#input_i4x-edX-model_course-problem-Checkbox_Problem_2_1_choice_0').check() - world.browser.find_by_css('input#input_i4x-edX-model_course-problem-Checkbox_Problem_2_1_choice_2').check() + inputfield('checkbox', choice='choice_0').check() + inputfield('checkbox', choice='choice_2').check() else: - world.browser.find_by_css('input#input_i4x-edX-model_course-problem-Checkbox_Problem_2_1_choice_3').check() + inputfield('checkbox', choice='choice_3').check() elif problem_type == 'string': - textfield = world.browser.find_by_css("input#input_i4x-edX-model_course-problem-String_Problem_2_1") textvalue = 'correct string' if correctness == 'correct' else 'incorrect' - textfield.fill(textvalue) + inputfield('string').fill(textvalue) elif problem_type == 'numerical': - textfield = world.browser.find_by_css("input#input_i4x-edX-model_course-problem-Numerical_Problem_2_1") textvalue = "pi + 1" if correctness == 'correct' else str(random.randint(-2,2)) - textfield.fill(textvalue) + inputfield('numerical').fill(textvalue) elif problem_type == 'formula': - textfield = world.browser.find_by_css("input#input_i4x-edX-model_course-problem-Formula_Problem_2_1") textvalue = "x^2+2*x+y" if correctness == 'correct' else 'x^2' - textfield.fill(textvalue) + inputfield('formula').fill(textvalue) check_problem(step) @@ -104,3 +94,35 @@ def assert_answer_mark(step, problem_type, correctness): else: mark_class = 'span.correct' if correctness == 'correct' else 'span.incorrect' assert(world.browser.is_element_present_by_css(mark_class, wait_time=4)) + +def problem_url(problem_type): + base = '/courses/edX/model_course/2013_Spring/courseware/Problem_Components/' + url_extensions = { 'drop down': 'Drop_Down_Problems', + 'multiple choice': 'Multiple_Choice_Problems', + 'checkbox': 'Checkbox_Problems', + 'string': 'String_Problems', + 'numerical': 'Numerical_Problems', + 'formula': 'Formula_Problems', } + + assert(problem_type in url_extensions) + return base + url_extensions[problem_type] + + + +def inputfield(problem_type, choice=None): + field_extensions = { 'drop down': 'Drop_Down_Problem', + 'multiple choice': 'Multiple_Choice_Problem', + 'checkbox': 'Checkbox_Problem', + 'string': 'String_Problem', + 'numerical': 'Numerical_Problem', + 'formula': 'Formula_Problem', } + + assert(problem_type in field_extensions) + extension = field_extensions[problem_type] + sel = "input#input_i4x-edX-model_course-problem-%s_2_1" % extension + + if choice is not None: + base = "_choice_" if problem_type == "multiple choice" else "_" + sel = sel + base + str(choice) + + return world.browser.find_by_css(sel)