problems.py 5.92 KB
Newer Older
Will Daly committed
1 2 3 4
'''
Steps for problem.feature lettuce tests
'''

5 6
# pylint: disable=C0111
# pylint: disable=W0621
Will Daly committed
7

8 9
from lettuce import world, step
from lettuce.django import django_url
10
from common import i_am_registered_for_the_course, visit_scenario_item
11
from problems_setup import PROBLEM_DICT, answer_problem, problem_has_answer, add_problem_to_course
12
from nose.tools import assert_equal
13

14

15
def _view_problem(step, problem_type, problem_settings=None):
16 17 18
    i_am_registered_for_the_course(step, 'model_course')

    # Ensure that the course has this problem type
19
    add_problem_to_course(world.scenario_dict['COURSE'].number, problem_type, problem_settings)
20 21 22

    # Go to the one section in the factory-created course
    # which should be loaded with the correct problem
23
    visit_scenario_item('SECTION')
24 25


26 27 28
@step(u'I am viewing a "([^"]*)" problem with "([^"]*)" attempt')
def view_problem_with_attempts(step, problem_type, attempts):
    _view_problem(step, problem_type, {'max_attempts': attempts})
29

30

31 32 33
@step(u'I am viewing a "([^"]*)" that shows the answer "([^"]*)"')
def view_problem_with_show_answer(step, problem_type, answer):
    _view_problem(step, problem_type, {'showanswer': answer})
Will Daly committed
34

35 36 37

@step(u'I am viewing a "([^"]*)" problem')
def view_problem(step, problem_type):
38
    _view_problem(step, problem_type)
39

Will Daly committed
40

41 42 43 44
@step(u'External graders respond "([^"]*)"')
def set_external_grader_response(step, correctness):
    assert(correctness in ['correct', 'incorrect'])

45 46 47 48 49
    response_dict = {
        'correct': True if correctness == 'correct' else False,
        'score': 1 if correctness == 'correct' else 0,
        'msg': 'Your problem was graded {0}'.format(correctness)
    }
50 51 52

    # Set the fake xqueue server to always respond
    # correct/incorrect when asked to grade a problem
53
    world.xqueue.config['default'] = response_dict
54

55

56
@step(u'I answer a "([^"]*)" problem "([^"]*)ly"')
57
def answer_problem_step(step, problem_type, correctness):
58 59 60 61 62
    """ Mark a given problem type correct or incorrect, then submit it.

    *problem_type* is a string representing the type of problem (e.g. 'drop down')
    *correctness* is in ['correct', 'incorrect']
    """
63 64
    # Change the answer on the page
    input_problem_answer(step, problem_type, correctness)
65

66 67 68 69 70 71 72 73 74
    # Submit the problem
    check_problem(step)


@step(u'I input an answer on a "([^"]*)" problem "([^"]*)ly"')
def input_problem_answer(_, problem_type, correctness):
    """
    Have the browser input an answer (either correct or incorrect)
    """
75
    assert(correctness in ['correct', 'incorrect'])
76
    assert(problem_type in PROBLEM_DICT)
77
    answer_problem(world.scenario_dict['COURSE'].number, problem_type, correctness)
Will Daly committed
78

Will Daly committed
79

80 81
@step(u'I check a problem')
def check_problem(step):
82 83 84
    # first scroll down so the loading mathjax button does not
    # cover up the Check button
    world.browser.execute_script("window.scrollTo(0,1024)")
85 86
    world.css_click("input.check")

87 88 89
    # Wait for the problem to finish re-rendering
    world.wait_for_ajax_complete()

90

91 92 93 94 95 96 97 98 99 100 101
@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']
102
    assert problem_type in PROBLEM_DICT
103
    problem_has_answer(world.scenario_dict['COURSE'].number, problem_type, answer_class)
104

Will Daly committed
105

106
@step(u'I reset the problem')
Don Mitchell committed
107
def reset_problem(_step):
108
    world.css_click('input.reset')
109

110 111 112
    # Wait for the problem to finish re-rendering
    world.wait_for_ajax_complete()

Will Daly committed
113

114
@step(u'I press the button with the label "([^"]*)"$')
Don Mitchell committed
115
def press_the_button_with_label(_step, buttonname):
116 117
    button_css = 'button span.show-label'
    elem = world.css_find(button_css).first
118
    world.css_has_text(button_css, elem)
119
    world.css_click(button_css)
120 121


122
@step(u'The "([^"]*)" button does( not)? appear')
Don Mitchell committed
123
def action_button_present(_step, buttonname, doesnt_appear):
124
    button_css = 'div.action input[value*="%s"]' % buttonname
125
    if bool(doesnt_appear):
Jay Zoldak committed
126 127 128 129 130
        assert world.is_css_not_present(button_css)
    else:
        assert world.is_css_present(button_css)


131 132 133 134 135 136
@step(u'the Show/Hide button label is "([^"]*)"$')
def show_hide_label_is(_step, label_name):
    # The label text is changed by static/xmodule_js/src/capa/display.js
    # so give it some time to change on the page.
    label_css = 'button.show span.show-label'
    world.wait_for(lambda _: world.css_has_text(label_css, label_name))
137 138


139 140
@step(u'I should see a score of "([^"]*)"$')
def see_score(_step, score):
141 142 143
    # The problem progress is changed by
    # cms/static/xmodule_js/src/capa/display.js
    # so give it some time to render on the page.
144
    score_css = 'div.problem-progress'
145 146
    expected_text = '({})'.format(score)
    world.wait_for(lambda _: world.css_has_text(score_css, expected_text))
147 148


149 150
@step(u'[Mm]y "([^"]*)" answer is( NOT)? marked "([^"]*)"')
def assert_answer_mark(_step, problem_type, isnt_marked, correctness):
Will Daly committed
151 152 153
    """
    Assert that the expected answer mark is visible
    for a given problem type.
154 155 156

    *problem_type* is a string identifying the type of problem (e.g. 'drop down')
    *correctness* is in ['correct', 'incorrect', 'unanswered']
157 158
    """
    # Determine which selector(s) to look for based on correctness
159 160
    assert(correctness in ['correct', 'incorrect', 'unanswered'])
    assert(problem_type in PROBLEM_DICT)
161 162

    # At least one of the correct selectors should be present
163
    for sel in PROBLEM_DICT[problem_type][correctness]:
164
        if bool(isnt_marked):
165 166 167
            has_expected = world.is_css_not_present(sel)
        else:
            has_expected = world.is_css_present(sel)
168 169 170 171

        # As soon as we find the selector, break out of the loop
        if has_expected:
            break
172

173 174
    # Expect that we found the expected selector
    assert(has_expected)