This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
html5_player.py
80 lines (65 loc) · 2.74 KB
/
html5_player.py
1
2
3
4
5
6
7
8
9
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import time
from gaiatest.apps.base import PageRegion
class HTML5Player(PageRegion):
"""Represents HTML5 Player.
Reference:
http://www.w3.org/TR/2012/WD-html5-20121025/media-elements.html#media-element
"""
def wait_for_video_loaded(self):
self.wait_for_condition(
lambda m:
int(self.root_element.get_attribute('readyState')) == 4)
@property
def is_paused(self):
return self.root_element.get_attribute('paused') == 'true'
@property
def is_ended(self):
return self.root_element.get_attribute('ended') == 'true'
@property
def has_controls(self):
return self.root_element.get_attribute('controls') == 'true'
def _disable_controls(self):
if self.has_controls:
self.marionette.execute_script(
'arguments[0].removeAttribute("controls")',
script_args=[self.root_element])
def invoke_controls(self):
if not self.has_controls:
self.marionette.execute_script(
'arguments[0].setAttribute("controls", "controls")',
script_args=[self.root_element])
if not (self.is_paused or self.is_ended):
self.root_element.tap()
else:
self.root_element.tap()
time.sleep(.25)
def play(self):
self.invoke_controls()
# We cannot actually tap on the elements because they are not HTML elements
# but we can tap where we anticipate them to be.
y_tap_location = int(self.root_element.size['height']/3)
self.root_element.tap(y=y_tap_location)
self.wait_for_condition(lambda m: not self.is_paused)
self._disable_controls()
def pause(self):
self.invoke_controls()
# We cannot actually tap on the elements because they are not HTML elements
# but we can tap where we anticipate them to be.
y_tap_location = int(self.root_element.size['height']/3)
self.root_element.tap(y=y_tap_location)
self.wait_for_condition(lambda m: self.is_paused)
self._disable_controls()
def is_video_playing(self):
# get 4 timestamps during approx. 1 sec
# ensure that newer timestamp has greater value than previous one
timestamps = []
for i in range(4):
timestamps.append(self.current_timestamp)
time.sleep(.25)
return all([timestamps[i - 1] < timestamps[i] for i in range(1, 3)])
@property
def current_timestamp(self):
return float(self.root_element.get_attribute('currentTime'))