navigation.py 5.28 KB
Newer Older
1
# pylint: disable=missing-docstring
2
# pylint: disable=redefined-outer-name
3
# pylint: disable=unused-argument
4 5

from lettuce import world, step
6
from common import course_location
7
from problems_setup import PROBLEM_DICT
8
from nose.tools import assert_in
9 10 11 12


@step(u'I am viewing a course with multiple sections')
def view_course_multiple_sections(step):
13
    create_course()
14

15 16 17 18 19 20 21 22 23
    section1 = world.ItemFactory.create(
        parent_location=course_location(world.scenario_dict['COURSE'].number),
        display_name="Test Section 1"
    )

    section2 = world.ItemFactory.create(
        parent_location=course_location(world.scenario_dict['COURSE'].number),
        display_name="Test Section 2"
    )
24

25 26 27 28 29
    place1 = world.ItemFactory.create(
        parent_location=section1.location,
        category='sequential',
        display_name="Test Subsection 1"
    )
30

31 32 33 34 35
    place2 = world.ItemFactory.create(
        parent_location=section2.location,
        category='sequential',
        display_name="Test Subsection 2"
    )
36

37 38
    add_problem_to_course_section(place1.location, "Problem 1")
    add_problem_to_course_section(place2.location, "Problem 2")
39

40
    create_user_and_visit_course()
41 42 43 44


@step(u'I am viewing a section with multiple subsections')
def view_course_multiple_subsections(step):
45
    create_course()
46

47 48 49 50
    section1 = world.ItemFactory.create(
        parent_location=course_location(world.scenario_dict['COURSE'].number),
        display_name="Test Section 1"
    )
51

52 53 54 55 56
    place1 = world.ItemFactory.create(
        parent_location=section1.location,
        category='sequential',
        display_name="Test Subsection 1"
    )
57

58 59 60 61
    place2 = world.ItemFactory.create(
        parent_location=section1.location,
        display_name="Test Subsection 2"
    )
62

63 64
    add_problem_to_course_section(place1.location, "Problem 3")
    add_problem_to_course_section(place2.location, "Problem 4")
65

66
    create_user_and_visit_course()
67 68 69 70


@step(u'I am viewing a section with multiple sequences')
def view_course_multiple_sequences(step):
71
    create_course()
72

73 74 75 76
    section1 = world.ItemFactory.create(
        parent_location=course_location(world.scenario_dict['COURSE'].number),
        display_name="Test Section 1"
    )
77

78 79 80 81 82 83 84 85
    place1 = world.ItemFactory.create(
        parent_location=section1.location,
        category='sequential',
        display_name="Test Subsection 1"
    )

    add_problem_to_course_section(place1.location, "Problem 5")
    add_problem_to_course_section(place1.location, "Problem 6")
86

87
    create_user_and_visit_course()
88 89


90 91
@step(u'I navigate to a section')
def when_i_navigate_to_a_section(step):
92 93 94
    # Prevent jQuery menu animations from interferring with the clicks
    world.disable_jquery_animations()

95
    # Open the 2nd section
96
    world.css_click(css_selector='.chapter', index=1)
97
    subsection_css = 'a[href*="Test_Subsection_2/"]'
98

99
    # Click on the subsection to see the content
100
    world.css_click(subsection_css)
101 102


103 104
@step(u'I navigate to a subsection')
def when_i_navigate_to_a_subsection(step):
105 106 107
    # Click on the 2nd subsection to see the content
    subsection_css = 'a[href*="Test_Subsection_2/"]'
    world.css_click(subsection_css)
108

109

110 111 112
@step(u'I navigate to an item in a sequence')
def when_i_navigate_to_an_item_in_a_sequence(step):
    sequence_css = 'a[data-element="2"]'
113
    world.css_click(sequence_css)
114 115


116 117
@step(u'I see the content of the section')
def then_i_see_the_content_of_the_section(step):
118
    wait_for_problem('Problem 2')
119 120


121 122
@step(u'I see the content of the subsection')
def then_i_see_the_content_of_the_subsection(step):
123
    wait_for_problem('Problem 4')
124 125


126 127
@step(u'I see the content of the sequence item')
def then_i_see_the_content_of_the_sequence_item(step):
128
    wait_for_problem('Problem 6')
129 130


131 132
@step(u'I return to the course')
def and_i_return_to_the_course(step):
133 134
    world.visit('/')
    world.click_link("View Course")
135 136
    course = 'a[href*="/courseware"]'
    world.css_click(course)
137 138


139 140
def create_course():
    world.clear_courses()
141 142 143
    world.scenario_dict['COURSE'] = world.CourseFactory.create(
        org='edx', number='999', display_name='Test Course'
    )
144 145 146


def create_user_and_visit_course():
147
    world.register_by_course_key(world.scenario_dict['COURSE'].id)
148
    world.log_in()
149
    world.visit(u'/courses/{}/courseware/'.format(world.scenario_dict['COURSE'].id))
150 151


152 153 154
def add_problem_to_course_section(parent_location, display_name):
    """
    Add a problem to the course at `parent_location` (a `Location` instance)
155

156 157 158
    `display_name` is the name of the problem to display, which
    is useful to identify which problem we're looking at.
    """
159 160

    # Generate the problem XML using capa.tests.response_xml_factory
161 162
    # Since this is just a placeholder, we always use multiple choice.
    factory_dict = PROBLEM_DICT['multiple choice']
163
    problem_xml = factory_dict['factory'].build_xml(**factory_dict['kwargs'])
164 165 166 167 168 169 170 171 172 173 174 175 176 177

    # Add the problem
    world.ItemFactory.create(
        parent_location=parent_location,
        category='problem',
        display_name=display_name,
        data=problem_xml
    )


def wait_for_problem(display_name):
    """
    Wait for the problem with `display_name` to appear on the page.
    """
178 179 180
    # Wait for the problem to reload
    world.wait_for_ajax_complete()

181
    wait_func = lambda _: world.css_has_text(
182
        '.problem-header', display_name, strip=True
183 184
    )
    world.wait_for(wait_func)