OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import logging | |
6 | |
7 import telemetry | |
8 from telemetry import util | |
9 | |
10 | |
11 def _WaitForLoginFormToLoad(backend, login_form_id, tab): | |
12 def IsFormLoadedOrAlreadyLoggedIn(): | |
13 return tab.EvaluateJavaScript( | |
14 'document.querySelector("#%s")!== null' % login_form_id) or \ | |
15 backend.IsAlreadyLoggedIn(tab) | |
16 | |
17 # Wait until the form is submitted and the page completes loading. | |
18 util.WaitFor(lambda: IsFormLoadedOrAlreadyLoggedIn(), # pylint: disable=W0108 | |
19 60) | |
20 | |
21 def _SubmitFormAndWait(form_id, tab): | |
22 js = 'document.getElementById("%s").submit();' % form_id | |
23 tab.ExecuteJavaScript(js) | |
24 | |
25 def IsLoginStillHappening(): | |
26 return tab.EvaluateJavaScript( | |
27 'document.querySelector("#%s")!== null' % form_id) | |
28 | |
29 # Wait until the form is submitted and the page completes loading. | |
30 util.WaitFor(lambda: not IsLoginStillHappening(), 60) | |
31 | |
32 class FormBasedCredentialsBackend(object): | |
33 def __init__(self): | |
34 self._logged_in = False | |
35 | |
36 def IsAlreadyLoggedIn(self, tab): | |
37 raise NotImplementedError() | |
38 | |
39 @property | |
40 def credentials_type(self): | |
41 raise NotImplementedError() | |
42 | |
43 @property | |
44 def url(self): | |
45 raise NotImplementedError() | |
46 | |
47 @property | |
48 def login_form_id(self): | |
49 raise NotImplementedError() | |
50 | |
51 @property | |
52 def login_input_id(self): | |
53 raise NotImplementedError() | |
54 | |
55 @property | |
56 def password_input_id(self): | |
57 raise NotImplementedError() | |
58 | |
59 def IsLoggedIn(self): | |
60 return self._logged_in | |
61 | |
62 def _ResetLoggedInState(self): | |
63 """Makes the backend think we're not logged in even though we are. | |
64 Should only be used in unit tests to simulate --dont-override-profile. | |
65 """ | |
66 self._logged_in = False | |
67 | |
68 def LoginNeeded(self, tab, config): | |
69 """Logs in to a test account. | |
70 | |
71 Raises: | |
72 RuntimeError: if could not get credential information. | |
73 """ | |
74 if self._logged_in: | |
75 return True | |
76 | |
77 if 'username' not in config or 'password' not in config: | |
78 message = ('Credentials for "%s" must include username and password.' % | |
79 self.credentials_type) | |
80 raise RuntimeError(message) | |
81 | |
82 logging.debug('Logging into %s account...' % self.credentials_type) | |
83 | |
84 try: | |
85 logging.info('Loading %s...', self.url) | |
86 tab.Navigate(self.url) | |
87 _WaitForLoginFormToLoad(self, self.login_form_id, tab) | |
88 | |
89 if self.IsAlreadyLoggedIn(tab): | |
90 self._logged_in = True | |
91 return True | |
92 | |
93 tab.WaitForDocumentReadyStateToBeInteractiveOrBetter() | |
94 logging.info('Loaded page: %s', self.url) | |
95 | |
96 email_id = 'document.querySelector("#%s").%s.value = "%s"; ' % ( | |
97 self.login_form_id, self.login_input_id, config['username']) | |
98 password = 'document.querySelector("#%s").%s.value = "%s"; ' % ( | |
99 self.login_form_id, self.password_input_id, config['password']) | |
100 tab.ExecuteJavaScript(email_id) | |
101 tab.ExecuteJavaScript(password) | |
102 | |
103 _SubmitFormAndWait(self.login_form_id, tab) | |
104 | |
105 self._logged_in = True | |
106 return True | |
107 except telemetry.TimeoutException: | |
108 logging.warning('Timed out while loading: %s', self.url) | |
109 return False | |
110 | |
111 def LoginNoLongerNeeded(self, tab): # pylint: disable=W0613 | |
112 assert self._logged_in | |
OLD | NEW |