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

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


@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 95 96
    # Open the 2nd section
    world.css_click(css_selector='div.chapter', index=1)
    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 111
@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"]'
112
    world.css_click(sequence_css)
113 114


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


120 121 122
@step(u'I see the content of the subsection')
def then_i_see_the_content_of_the_subsection(step):
    wait_for_problem('PROBLEM 4')
123 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):
    wait_for_problem('PROBLEM 6')
128 129


130 131 132 133 134
@step(u'I return to the courseware')
def and_i_return_to_the_courseware(step):
    world.visit('/')
    world.click_link("View Course")
    world.click_link("Courseware")
135 136


137 138 139 140
@step(u'I see that I was most recently in the subsection')
def then_i_see_that_i_was_most_recently_in_the_subsection(step):
    message = world.css_text('section.course-content > p')
    assert_in("You were most recently in Test Subsection 2", message)
141 142


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


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


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

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

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

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

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