video.py 8.49 KB
Newer Older
1
# pylint: disable=C0111
2 3

from lettuce import world, step
4
from nose.tools import assert_less
5 6
from xmodule.modulestore import Location
from contentstore.utils import get_modulestore
7
from selenium.webdriver.common.keys import Keys
8

9
VIDEO_BUTTONS = {
polesye committed
10 11
    'CC': '.hide-subtitles',
    'volume': '.volume',
polesye committed
12 13
    'play': '.video_control.play',
    'pause': '.video_control.pause',
polesye committed
14
    'handout': '.video-handout.video-download-button a',
polesye committed
15 16
}

17 18 19 20 21
SELECTORS = {
    'spinner': '.video-wrapper .spinner',
    'controls': 'section.video-controls',
}

22 23 24
# We should wait 300 ms for event handler invocation + 200ms for safety.
DELAY = 0.5

25

26 27 28 29 30 31 32 33 34 35 36
@step('youtube stub server (.*) YouTube API')
def configure_youtube_api(_step, action):
    action=action.strip()
    if action == 'proxies':
        world.youtube.config['youtube_api_blocked'] = False
    elif action == 'blocks':
        world.youtube.config['youtube_api_blocked'] = True
    else:
        raise ValueError('Parameter `action` should be one of "proxies" or "blocks".')


37
@step('I have created a Video component$')
38
def i_created_a_video_component(_step):
39

40
    world.create_course_with_unit()
41
    world.create_component_instance(
42
        step=_step,
43
        category='video',
44 45
    )

46 47 48 49 50
    world.wait_for_xmodule()
    world.disable_jquery_animations()

    world.wait_for_present('.is-initialized')
    world.wait(DELAY)
51
    world.wait_for_invisible(SELECTORS['spinner'])
52 53
    if not world.youtube.config.get('youtube_api_blocked'):
        world.wait_for_visible(SELECTORS['controls'])
54

polesye committed
55 56 57 58
@step('I have created a Video component with subtitles$')
def i_created_a_video_with_subs(_step):
    _step.given('I have created a Video component with subtitles "OEoXaMPEzfM"')

polesye committed
59

polesye committed
60 61
@step('I have created a Video component with subtitles "([^"]*)"$')
def i_created_a_video_with_subs_with_name(_step, sub_id):
polesye committed
62
    _step.given('I have created a Video component')
63 64 65 66 67

    # Store the current URL so we can return here
    video_url = world.browser.url

    # Upload subtitles for the video using the upload interface
polesye committed
68
    _step.given('I have uploaded subtitles "{}"'.format(sub_id))
69 70 71

    # Return to the video
    world.visit(video_url)
72

73
    world.wait_for_xmodule()
74 75 76 77 78 79 80

    # update .sub filed with proper subs name (which mimics real Studio/XML behavior)
    # this is needed only for that videos which are created in acceptance tests.
    _step.given('I edit the component')
    world.wait_for_ajax_complete()
    _step.given('I save changes')

81 82 83
    world.disable_jquery_animations()

    world.wait_for_present('.is-initialized')
84
    world.wait_for_invisible(SELECTORS['spinner'])
85 86


polesye committed
87
@step('I have uploaded subtitles "([^"]*)"$')
polesye committed
88 89
def i_have_uploaded_subtitles(_step, sub_id):
    _step.given('I go to the files and uploads page')
90
    _step.given('I upload the test file "subs_{}.srt.sjson"'.format(sub_id.strip()))
91 92


Anton Stupak committed
93
@step('when I view the (.*) it does not have autoplay enabled$')
94
def does_not_autoplay(_step, video_type):
95 96
    world.wait(DELAY)
    world.wait_for_ajax_complete()
polesye committed
97 98 99
    actual = world.css_find('.%s' % video_type)[0]['data-autoplay']
    expected = [u'False', u'false', False]
    assert actual in expected
100
    assert world.css_has_class('.video_control', 'play')
101 102


Anton Stupak committed
103
@step('creating a video takes a single click$')
Don Mitchell committed
104
def video_takes_a_single_click(_step):
105 106 107
    component_css = '.xmodule_VideoModule'
    assert world.is_css_not_present(component_css)

108
    world.css_click("a[data-category='video']")
109
    assert world.is_css_present(component_css)
110 111


Anton Stupak committed
112
@step('I edit the component$')
113 114 115 116
def i_edit_the_component(_step):
    world.edit_component()


Anton Stupak committed
117
@step('I have (hidden|toggled) captions$')
118
def hide_or_show_captions(step, shown):
119
    button_css = 'a.hide-subtitles'
120
    if shown == 'hidden':
121 122 123 124 125 126 127
        world.css_click(button_css)
    if shown == 'toggled':
        world.css_click(button_css)
        # When we click the first time, a tooltip shows up. We want to
        # click the button rather than the tooltip, so move the mouse
        # away to make it disappear.
        button = world.css_find(button_css)
128
        # mouse_out is not implemented on firefox with selenium
129
        if not world.is_firefox:
130
            button.mouse_out()
131
        world.css_click(button_css)
Peter Fogg committed
132

133

Anton Stupak committed
134
@step('I have created a video with only XML data$')
135 136 137 138 139 140 141 142
def xml_only_video(step):
    # Create a new video *without* metadata. This requires a certain
    # amount of rummaging to make sure all the correct data is present
    step.given('I have clicked the new unit button')

    # Wait for the new unit to be created and to load the page
    world.wait(1)

143 144
    course = world.scenario_dict['COURSE']
    store = get_modulestore(course.location)
145

146
    parent_location = store.get_items(course.id, category='vertical', revision='draft')[0].location
147 148 149 150 151 152 153

    youtube_id = 'ABCDEFG'
    world.scenario_dict['YOUTUBE_ID'] = youtube_id

    # Create a new Video component, but ensure that it doesn't have
    # metadata. This allows us to test that we are correctly parsing
    # out XML
154
    world.ItemFactory.create(
155 156
        parent_location=parent_location,
        category='video',
157 158
        data='<video youtube="1.00:%s"></video>' % youtube_id,
        modulestore=store,
159 160 161
    )


Anton Stupak committed
162
@step('The correct Youtube video is shown$')
163 164
def the_youtube_video_is_shown(_step):
    ele = world.css_find('.video').first
165
    assert ele['data-streams'].split(':')[1] == world.scenario_dict['YOUTUBE_ID']
Valera Rozuvan committed
166 167 168


@step('Make sure captions are (.+)$')
169
def set_captions_visibility_state(_step, captions_state):
170
    SELECTOR = '.closed .subtitles'
171
    world.wait_for_visible('.hide-subtitles')
Valera Rozuvan committed
172
    if captions_state == 'closed':
173
        if not world.is_css_present(SELECTOR):
174
            world.css_find('.hide-subtitles').click()
Valera Rozuvan committed
175
    else:
176
        if world.is_css_present(SELECTOR):
177
            world.css_find('.hide-subtitles').click()
Valera Rozuvan committed
178 179


polesye committed
180
@step('I hover over button "([^"]*)"$')
181
def hover_over_button(_step, button):
182
    world.css_find(VIDEO_BUTTONS[button.strip()]).mouse_over()
polesye committed
183

Valera Rozuvan committed
184

polesye committed
185
@step('Captions (?:are|become) "([^"]*)"$')
186
def check_captions_visibility_state(_step, visibility_state):
Valera Rozuvan committed
187 188 189 190
    if visibility_state == 'visible':
        assert world.css_visible('.subtitles')
    else:
        assert not world.css_visible('.subtitles')
191 192 193 194 195 196 197


def find_caption_line_by_data_index(index):
    SELECTOR = ".subtitles > li[data-index='{index}']".format(index=index)
    return world.css_find(SELECTOR).first


198
@step('I focus on caption line with data-index "([^"]*)"$')
199
def focus_on_caption_line(_step, index):
200 201
    world.wait_for_present('.video.is-captions-rendered')
    world.wait_for(lambda _: world.css_text('.subtitles'), timeout=30)
202 203 204
    find_caption_line_by_data_index(int(index.strip()))._element.send_keys(Keys.TAB)


205 206
@step('I press "enter" button on caption line with data-index "([^"]*)"$')
def click_on_the_caption(_step, index):
207 208
    world.wait_for_present('.video.is-captions-rendered')
    world.wait_for(lambda _: world.css_text('.subtitles'), timeout=30)
209 210 211
    find_caption_line_by_data_index(int(index.strip()))._element.send_keys(Keys.ENTER)


212
@step('I see caption line with data-index "([^"]*)" has class "([^"]*)"$')
213 214
def caption_line_has_class(_step, index, className):
    SELECTOR = ".subtitles > li[data-index='{index}']".format(index=int(index.strip()))
215
    assert world.css_has_class(SELECTOR, className.strip())
216

217

polesye committed
218 219 220 221
@step('I see a range on slider$')
def see_a_range_slider_with_proper_range(_step):
    world.wait_for_visible(VIDEO_BUTTONS['pause'])
    assert world.css_visible(".slider-range")
222

223

224 225 226 227 228 229 230 231 232 233 234 235 236 237
@step('I (.*) see video button "([^"]*)"$')
def do_not_see_or_not_button_video(_step, action, button_type):
    world.wait(DELAY)
    world.wait_for_ajax_complete()
    action=action.strip()
    button = button_type.strip()
    if action == 'do not':
        assert not world.is_css_present(VIDEO_BUTTONS[button])
    elif action == 'can':
        assert world.css_visible(VIDEO_BUTTONS[button])
    else:
        raise ValueError('Parameter `action` should be one of "do not" or "can".')


238 239 240 241 242 243 244
@step('I click video button "([^"]*)"$')
def click_button_video(_step, button_type):
    world.wait(DELAY)
    world.wait_for_ajax_complete()
    button = button_type.strip()
    world.css_click(VIDEO_BUTTONS[button])

245 246 247 248 249 250 251 252 253 254 255 256 257 258

@step('I seek video to "([^"]*)" seconds$')
def seek_video_to_n_seconds(_step, seconds):
    time = float(seconds.strip())
    jsCode = "$('.video').data('video-player-state').videoPlayer.onSlideSeek({{time: {0:f}}})".format(time)
    world.browser.execute_script(jsCode)


@step('I see video starts playing from "([^"]*)" position$')
def start_playing_video_from_n_seconds(_step, position):
    world.wait_for(
        func=lambda _: world.css_html('.vidtime')[:4] == position.strip(),
        timeout=5
    )