navigation.py 5.33 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 8 9 10 11
from problems_setup import PROBLEM_DICT


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

14 15 16 17 18 19 20 21 22
    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"
    )
23

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

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

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

39
    create_user_and_visit_course()
40 41 42 43


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

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

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

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

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

65
    create_user_and_visit_course()
66 67 68 69


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

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

77 78 79 80 81 82 83 84
    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")
85

86
    create_user_and_visit_course()
87 88


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

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

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


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

108

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


115 116
@step(u'I see the content of the section')
def then_i_see_the_content_of_the_section(step):
117 118 119
    """
    Uppercasing the title here since CSS does it on the front-end
    """
120
    wait_for_problem('Problem 2')
121 122


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


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


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


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


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


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

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

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

    # 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.
    """
180 181 182
    # Wait for the problem to reload
    world.wait_for_ajax_complete()

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