Commit c660229b by Will Daly

Added checking for problem answer state after a problem is checked

parent 30843610
...@@ -8,6 +8,7 @@ Feature: Answer problems ...@@ -8,6 +8,7 @@ Feature: Answer problems
And I am viewing a "<ProblemType>" problem And I am viewing a "<ProblemType>" problem
When I answer a "<ProblemType>" problem "correctly" When I answer a "<ProblemType>" problem "correctly"
Then My "<ProblemType>" answer is marked "correct" Then My "<ProblemType>" answer is marked "correct"
And The "<ProblemType>" problem displays a "correct" answer
Examples: Examples:
| ProblemType | | ProblemType |
...@@ -25,6 +26,7 @@ Feature: Answer problems ...@@ -25,6 +26,7 @@ Feature: Answer problems
And I am viewing a "<ProblemType>" problem And I am viewing a "<ProblemType>" problem
When I answer a "<ProblemType>" problem "incorrectly" When I answer a "<ProblemType>" problem "incorrectly"
Then My "<ProblemType>" answer is marked "incorrect" Then My "<ProblemType>" answer is marked "incorrect"
And The "<ProblemType>" problem displays a "incorrect" answer
Examples: Examples:
| ProblemType | | ProblemType |
...@@ -41,6 +43,7 @@ Feature: Answer problems ...@@ -41,6 +43,7 @@ Feature: Answer problems
Given I am viewing a "<ProblemType>" problem Given I am viewing a "<ProblemType>" problem
When I check a problem When I check a problem
Then My "<ProblemType>" answer is marked "incorrect" Then My "<ProblemType>" answer is marked "incorrect"
And The "<ProblemType>" problem displays a "blank" answer
Examples: Examples:
| ProblemType | | ProblemType |
...@@ -58,6 +61,7 @@ Feature: Answer problems ...@@ -58,6 +61,7 @@ Feature: Answer problems
And I answer a "<ProblemType>" problem "<Correctness>ly" And I answer a "<ProblemType>" problem "<Correctness>ly"
When I reset the problem When I reset the problem
Then My "<ProblemType>" answer is marked "unanswered" Then My "<ProblemType>" answer is marked "unanswered"
And The "<ProblemType>" problem displays a "blank" answer
Examples: Examples:
| ProblemType | Correctness | | ProblemType | Correctness |
......
...@@ -2,8 +2,8 @@ from lettuce import world, step ...@@ -2,8 +2,8 @@ from lettuce import world, step
from lettuce.django import django_url from lettuce.django import django_url
import random import random
import textwrap import textwrap
import time from common import i_am_registered_for_the_course, \
from common import i_am_registered_for_the_course, TEST_SECTION_NAME, section_location TEST_SECTION_NAME, section_location
from capa.tests.response_xml_factory import OptionResponseXMLFactory, \ from capa.tests.response_xml_factory import OptionResponseXMLFactory, \
ChoiceResponseXMLFactory, MultipleChoiceResponseXMLFactory, \ ChoiceResponseXMLFactory, MultipleChoiceResponseXMLFactory, \
StringResponseXMLFactory, NumericalResponseXMLFactory, \ StringResponseXMLFactory, NumericalResponseXMLFactory, \
...@@ -26,7 +26,7 @@ PROBLEM_FACTORY_DICT = { ...@@ -26,7 +26,7 @@ PROBLEM_FACTORY_DICT = {
'kwargs': { 'kwargs': {
'question_text': 'The correct answer is Choice 3', 'question_text': 'The correct answer is Choice 3',
'choices': [False, False, True, False], 'choices': [False, False, True, False],
'choice_names': ['choice_1', 'choice_2', 'choice_3', 'choice_4']}}, 'choice_names': ['choice_0', 'choice_1', 'choice_2', 'choice_3']}},
'checkbox': { 'checkbox': {
'factory': ChoiceResponseXMLFactory(), 'factory': ChoiceResponseXMLFactory(),
...@@ -152,9 +152,9 @@ def answer_problem(step, problem_type, correctness): ...@@ -152,9 +152,9 @@ def answer_problem(step, problem_type, correctness):
elif problem_type == "multiple choice": elif problem_type == "multiple choice":
if correctness == 'correct': if correctness == 'correct':
inputfield('multiple choice', choice='choice_3').check()
else:
inputfield('multiple choice', choice='choice_2').check() inputfield('multiple choice', choice='choice_2').check()
else:
inputfield('multiple choice', choice='choice_1').check()
elif problem_type == "checkbox": elif problem_type == "checkbox":
if correctness == 'correct': if correctness == 'correct':
...@@ -202,6 +202,65 @@ def answer_problem(step, problem_type, correctness): ...@@ -202,6 +202,65 @@ def answer_problem(step, problem_type, correctness):
# Submit the problem # Submit the problem
check_problem(step) check_problem(step)
@step(u'The "([^"]*)" problem displays a "([^"]*)" answer')
def assert_problem_has_answer(step, problem_type, answer_class):
'''
Assert that the problem is displaying a particular answer.
These correspond to the same correct/incorrect
answers we set in answer_problem()
We can also check that a problem has been left blank
by setting answer_class='blank'
'''
assert answer_class in ['correct', 'incorrect', 'blank']
if problem_type == "drop down":
if answer_class == 'blank':
assert world.browser.is_element_not_present_by_css('option[selected="true"]')
else:
actual = world.browser.find_by_css('option[selected="true"]').value
expected = 'Option 2' if answer_class == 'correct' else 'Option 3'
assert actual == expected
elif problem_type == "multiple choice":
if answer_class == 'correct':
assert_checked('multiple choice', ['choice_2'])
elif answer_class == 'incorrect':
assert_checked('multiple choice', ['choice_1'])
else:
assert_checked('multiple choice', [])
elif problem_type == "checkbox":
if answer_class == 'correct':
assert_checked('checkbox', ['choice_0', 'choice_2'])
elif answer_class == 'incorrect':
assert_checked('checkbox', ['choice_3'])
else:
assert_checked('checkbox', [])
elif problem_type == 'string':
if answer_class == 'blank':
expected = ''
else:
expected = 'correct string' if answer_class == 'correct' else 'incorrect'
assert_textfield('string', expected)
elif problem_type == 'formula':
if answer_class == 'blank':
expected = ''
else:
expected = "x^2+2*x+y" if answer_class == 'correct' else 'x^2'
assert_textfield('formula', expected)
else:
# The other response types use random data,
# which would be difficult to check
# We trade input value coverage in the other tests for
# input type coverage in this test.
pass
@step(u'I check a problem') @step(u'I check a problem')
def check_problem(step): def check_problem(step):
...@@ -274,6 +333,7 @@ def assert_answer_mark(step, problem_type, correctness): ...@@ -274,6 +333,7 @@ def assert_answer_mark(step, problem_type, correctness):
# Expect that we found the expected selector # Expect that we found the expected selector
assert(has_expected) assert(has_expected)
def inputfield(problem_type, choice=None, input_num=1): def inputfield(problem_type, choice=None, input_num=1):
""" Return the <input> element for *problem_type*. """ Return the <input> element for *problem_type*.
For example, if problem_type is 'string', return For example, if problem_type is 'string', return
...@@ -289,8 +349,30 @@ def inputfield(problem_type, choice=None, input_num=1): ...@@ -289,8 +349,30 @@ def inputfield(problem_type, choice=None, input_num=1):
base = "_choice_" if problem_type == "multiple choice" else "_" base = "_choice_" if problem_type == "multiple choice" else "_"
sel = sel + base + str(choice) sel = sel + base + str(choice)
# If the input element doesn't exist, fail immediately # If the input element doesn't exist, fail immediately
assert(world.browser.is_element_present_by_css(sel, wait_time=4)) assert(world.browser.is_element_present_by_css(sel, wait_time=4))
# Retrieve the input element # Retrieve the input element
return world.browser.find_by_css(sel) return world.browser.find_by_css(sel)
def assert_checked(problem_type, choices):
'''
Assert that choice names given in *choices* are the only
ones checked.
Works for both radio and checkbox problems
'''
all_choices = ['choice_0', 'choice_1', 'choice_2', 'choice_3']
for ch in all_choices:
el = inputfield(problem_type, choice=ch)
if ch in choices:
assert el.checked
else:
assert not el.checked
def assert_textfield(problem_type, expected_text, input_num=1):
el = inputfield(problem_type, input_num=input_num)
assert el.value == expected_text
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment