2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
10 import pyauto_functional
15 class NetflixTestHelper():
16 """Helper functions for Netflix tests.
18 For sample usage, look at class NetflixTest.
21 # Netflix player states.
22 IS_GUEST_MODE_ERROR = '0'
25 TITLE_HOMEPAGE = 'http://movies.netflix.com/WiHome'
26 SIGNOUT_PAGE = 'https://account.netflix.com/Logout'
28 VIDEO_URL = 'https://movies.netflix.com/WiPlayer?movieid=70136124'
29 ALT_VIDEO_URL = 'https://movies.netflix.com/WiPlayer?movieid=70133713'
32 def __init__(self, pyauto):
35 def _IsNetflixPluginEnabled(self):
36 """Determine Netflix plugin availability and its state."""
37 return [x for x in self._pyauto.GetPluginsInfo().Plugins() \
38 if x['name'] == 'Netflix' and x['enabled']]
40 def _LoginToNetflix(self):
41 """Login to Netflix."""
42 credentials = self._pyauto.GetPrivateInfo()['test_netflix_acct']
43 board_name = self._pyauto.ChromeOSBoard()
44 assert credentials.get(board_name), \
45 'No netflix credentials for %s.' % board_name
46 self._pyauto.NavigateToURL(credentials['login_url'])
48 document.getElementById('email').value='%s';
49 document.getElementById('password').value='%s';
50 window.domAutomationController.send('ok');
51 """ % (credentials[board_name], credentials['password'])
52 self._pyauto.assertEqual(self._pyauto.ExecuteJavascript(login_js), 'ok',
53 msg='Failed to set login credentials.')
54 self._pyauto.assertTrue(self._pyauto.SubmitForm('login-form'),
55 msg='Login to Netflix failed. We think this is an authetication '
56 'problem from the Netflix side. Sometimes we also see this while '
59 def _GetVideoDroppedFrames(self, tab_index=0, windex=0):
60 """Returns total Netflix video dropped frames."""
62 var frames = nrdp.video.droppedFrames;
63 window.domAutomationController.send(frames + '');
65 return int(self._pyauto.ExecuteJavascript(js, tab_index=tab_index,
68 def _GetVideoFrames(self, tab_index=0, windex=0):
69 """Returns Netflix video total frames."""
71 var frames = nrdp.video.totalFrames;
72 window.domAutomationController.send(frames + '');
74 return int(self._pyauto.ExecuteJavascript(js, tab_index=tab_index,
77 def _HandleInfobars(self, err_msg):
78 """Manage infobars that come up during the test."""
79 def _HandleNetflixInfobar():
80 tab_info = self._pyauto.GetBrowserInfo()['windows'][0]['tabs'][0]
81 infobars = tab_info['infobars']
83 for infobar in infobars:
84 if 'netflix' in infobar['text']:
85 # After storage infobar pops up, clicking the Ok button immediately
86 # returns the Storage error on faster machines like Stumpy/Lumpy so
87 # adding a delay of 1 second here.
89 self._pyauto.PerformActionOnInfobar('accept', infobar_index=index)
93 self._pyauto.assertTrue(self._pyauto.WaitUntil(_HandleNetflixInfobar),
96 def CurrentPlaybackTime(self):
97 """Returns the current playback time in seconds."""
98 time = self._pyauto.ExecuteJavascript("""
99 time = nrdp.video.currentTime;
100 window.domAutomationController.send(time + '');
102 return int(float(time))
105 """Sign out from Netflix Login."""
106 self._pyauto.NavigateToURL(self.SIGNOUT_PAGE)
108 def LoginAndStartPlaying(self):
109 """Login and start playing the video."""
110 self._pyauto.assertTrue(self._pyauto._IsNetflixPluginEnabled(),
111 msg='Netflix plugin is disabled or not available.')
112 self._pyauto._LoginToNetflix()
113 self._pyauto.assertTrue(self._pyauto.WaitUntil(
114 lambda: self._pyauto.GetActiveTabURL().spec(),
115 expect_retval=self.TITLE_HOMEPAGE),
116 msg='Login to Netflix failed.')
117 self._pyauto.NavigateToURL(self.VIDEO_URL)
118 self._pyauto._HandleInfobars(err_msg='Netflix infobar did not show up')
120 def CheckNetflixPlaying(self, expected_result, error_msg):
121 """Check if Netflix is playing the video or not.
124 expected_result: expected return value from Netflix player.
125 error_msg: If expected value isn't matching, error message to throw.
127 self._pyauto.assertTrue(self._pyauto.WaitUntil(
128 lambda: self._pyauto.ExecuteJavascript("""
129 if (typeof nrdp == 'undefined') {
130 window.domAutomationController.send('not ready');
132 player_status = nrdp.video.readyState;
133 window.domAutomationController.send(player_status + '');
134 """), expect_retval=expected_result),
138 class NetflixTest(pyauto.PyUITest, NetflixTestHelper):
139 """Test case for Netflix player."""
141 def __init__(self, methodName='runTest', **kwargs):
142 pyauto.PyUITest.__init__(self, methodName, **kwargs)
143 NetflixTestHelper.__init__(self, self)
145 def ShouldAutoLogin(self):
150 credentials = self.GetPrivateInfo()['test_google_account']
151 self.Login(credentials['username'], credentials['password'])
152 logging.info('Logged in as %s' % credentials['username'])
153 login_info = self.GetLoginInfo()
154 self.assertTrue(login_info['is_logged_in'], msg='Login failed.')
155 self.assertFalse(login_info['is_guest'],
156 msg='Should not be logged in as guest.')
159 assert os.geteuid() == 0, 'Run test as root since we might need to logout'
160 pyauto.PyUITest.setUp(self)
161 if self.GetLoginInfo()['is_logged_in']:
167 pyauto.PyUITest.tearDown(self)
169 def testPlayerLoadsAndPlays(self):
170 """Test that Netflix player loads and plays the title."""
171 self.LoginAndStartPlaying()
172 self._HandleInfobars(err_msg='Netflix plugin access infobar did not show up')
173 self.CheckNetflixPlaying(self.IS_PLAYING,
174 'Player did not start playing the title.')
176 def testMultiplePlayback(self):
177 """Test that playing two titles, Netflix returns multiple play error."""
178 self.LoginAndStartPlaying()
179 self._HandleInfobars(err_msg='Netflix plugin access infobar did not show up')
180 self.CheckNetflixPlaying(self.IS_PLAYING,
181 'Player did not start playing the title.')
182 self.AppendTab(self.ALT_VIDEO_URL)
183 self.assertTrue('Multiple Play Error' in self.GetTabContents(),
184 msg='Multiple Play Error is not found on the page.')
186 def testPlaying(self):
187 """Test that title playing progresses."""
188 self.LoginAndStartPlaying()
189 self._HandleInfobars(err_msg='Netflix plugin access infobar did not show up')
190 self.CheckNetflixPlaying(self.IS_PLAYING,
191 'Player did not start playing the title.')
192 title_length = self.ExecuteJavascript("""
193 time = nrdp.video.duration;
194 window.domAutomationController.send(time + '');
196 title_length = int(float(title_length))
200 while current_time < title_length:
201 # We want to test playing only for ten seconds.
205 current_time = self.CurrentPlaybackTime()
206 self.assertTrue(prev_time <= current_time,
207 msg='Prev playing time %s is greater than current time %s.'
208 % (prev_time, current_time))
209 prev_time = current_time
210 # play video for some time
212 # In case player doesn't start playing at all, above while loop may
213 # still pass. So re-verifying and assuming that player did play something
214 # during last 10 seconds.
215 self.assertTrue(current_time > 0,
216 msg='Netflix player did not start playing.')
219 class NetflixGuestModeTest(pyauto.PyUITest, NetflixTestHelper):
220 """Netflix in guest mode."""
222 def __init__(self, methodName='runTest', **kwargs):
223 pyauto.PyUITest.__init__(self, methodName, **kwargs)
224 NetflixTestHelper.__init__(self, self)
227 assert os.geteuid() == 0, 'Run test as root since we might need to logout'
228 pyauto.PyUITest.setUp(self)
229 if self.GetLoginInfo()['is_logged_in']:
232 login_info = self.GetLoginInfo()
233 self.assertTrue(login_info['is_logged_in'], msg='Not logged in at all.')
234 self.assertTrue(login_info['is_guest'], msg='Not logged in as guest.')
236 def ShouldAutoLogin(self):
240 self.AppendTab(self.SIGNOUT_PAGE)
242 pyauto.PyUITest.tearDown(self)
244 def testGuestMode(self):
245 """Test that Netflix doesn't play in guest mode login."""
246 self.LoginAndStartPlaying()
247 self.CheckNetflixPlaying(
248 self.IS_GUEST_MODE_ERROR,
249 'Netflix player did not return a Guest mode error.')
250 # crosbug.com/p/14009
251 self.assertTrue('Netflix Video Player Unavailable' in self.GetTabContents(),
252 msg='Guest Mode error is not found on the page.')
255 if __name__ == '__main__':
256 pyauto_functional.Main()