conditional.py 4.61 KB
Newer Older
1
# pylint: disable=missing-docstring
2
# pylint: disable=redefined-outer-name
3 4

from lettuce import world, steps
5
from nose.tools import assert_in, assert_true  # pylint: disable=no-name-in-module
6

7
from common import i_am_registered_for_the_course, visit_scenario_item
8 9
from problems_setup import add_problem_to_course, answer_problem

10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
@steps
class ConditionalSteps(object):
    COURSE_NUM = 'test_course'

    def setup_conditional(self, step, condition_type, condition, cond_value):
        r'that a course has a Conditional conditioned on (?P<condition_type>\w+) (?P<condition>\w+)=(?P<cond_value>\w+)$'

        i_am_registered_for_the_course(step, self.COURSE_NUM)

        world.scenario_dict['VERTICAL'] = world.ItemFactory(
            parent_location=world.scenario_dict['SECTION'].location,
            category='vertical',
            display_name="Test Vertical",
        )

        world.scenario_dict['WRAPPER'] = world.ItemFactory(
            parent_location=world.scenario_dict['VERTICAL'].location,
            category='wrapper',
            display_name="Test Poll Wrapper"
        )

        if condition_type == 'problem':
            world.scenario_dict['CONDITION_SOURCE'] = add_problem_to_course(self.COURSE_NUM, 'string')
        elif condition_type == 'poll':
            world.scenario_dict['CONDITION_SOURCE'] = world.ItemFactory(
                parent_location=world.scenario_dict['WRAPPER'].location,
                category='poll_question',
                display_name='Conditional Poll',
                data={
                    'question': 'Is this a good poll?',
                    'answers': [
                        {'id': 'yes', 'text': 'Yes, of course'},
                        {'id': 'no', 'text': 'Of course not!'}
                    ],
                }
            )
        else:
            raise Exception("Unknown condition type: {!r}".format(condition_type))

        metadata = {
            'xml_attributes': {
52
                condition: cond_value
53 54 55 56 57 58 59
            }
        }

        world.scenario_dict['CONDITIONAL'] = world.ItemFactory(
            parent_location=world.scenario_dict['WRAPPER'].location,
            category='conditional',
            display_name="Test Conditional",
60 61
            metadata=metadata,
            sources_list=[world.scenario_dict['CONDITION_SOURCE'].location],
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        )

        world.ItemFactory(
            parent_location=world.scenario_dict['CONDITIONAL'].location,
            category='html',
            display_name='Conditional Contents',
            data='<html><div class="hidden-contents">Hidden Contents</p></html>'
        )

    def setup_problem_attempts(self, step, not_attempted=None):
        r'that the conditioned problem has (?P<not_attempted>not )?been attempted$'
        visit_scenario_item('CONDITION_SOURCE')

        if not_attempted is None:
            answer_problem(self.COURSE_NUM, 'string', True)
77
            world.css_click("button.check")
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    def when_i_view_the_conditional(self, step):
        r'I view the conditional$'
        visit_scenario_item('CONDITIONAL')
        world.wait_for_js_variable_truthy('$(".xblock-student_view[data-type=Conditional]").data("initialized")')

    def check_visibility(self, step, visible):
        r'the conditional contents are (?P<visible>\w+)$'
        world.wait_for_ajax_complete()

        assert_in(visible, ('visible', 'hidden'))

        if visible == 'visible':
            world.wait_for_visible('.hidden-contents')
            assert_true(world.css_visible('.hidden-contents'))
        else:
            assert_true(world.is_css_not_present('.hidden-contents'))
95 96 97 98 99 100
            assert_true(
                world.css_contains_text(
                    '.conditional-message',
                    'must be attempted before this will become visible.'
                )
            )
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    def answer_poll(self, step, answer):
        r' I answer the conditioned poll "([^"]*)"$'
        visit_scenario_item('CONDITION_SOURCE')
        world.wait_for_js_variable_truthy('$(".xblock-student_view[data-type=Poll]").data("initialized")')
        world.wait_for_ajax_complete()

        answer_text = [
            poll_answer['text']
            for poll_answer
            in world.scenario_dict['CONDITION_SOURCE'].answers
            if poll_answer['id'] == answer
        ][0]

        text_selector = '.poll_answer .text'

        poll_texts = world.retry_on_exception(
            lambda: [elem.text for elem in world.css_find(text_selector)]
        )

        for idx, poll_text in enumerate(poll_texts):
            if poll_text == answer_text:
                world.css_click(text_selector, index=idx)
                return


127
ConditionalSteps()