Index: tools/telemetry/telemetry/core/backends/form_based_credentials_backend.py |
diff --git a/tools/telemetry/telemetry/core/backends/form_based_credentials_backend.py b/tools/telemetry/telemetry/core/backends/form_based_credentials_backend.py |
index bb32f32251a3489a3053af0629642ad7fd8b00a9..759a4a243f75b34c710d516e2bc34d6764994c73 100644 |
--- a/tools/telemetry/telemetry/core/backends/form_based_credentials_backend.py |
+++ b/tools/telemetry/telemetry/core/backends/form_based_credentials_backend.py |
@@ -6,32 +6,12 @@ import logging |
from telemetry.core import util |
-def _WaitForLoginFormToLoad(backend, login_form_id, tab): |
- def IsFormLoadedOrAlreadyLoggedIn(): |
- return (tab.EvaluateJavaScript( |
- 'document.querySelector("#%s")!== null' % login_form_id) or |
- backend.IsAlreadyLoggedIn(tab)) |
- |
- # Wait until the form is submitted and the page completes loading. |
- util.WaitFor(IsFormLoadedOrAlreadyLoggedIn, 60) |
- |
-def _SubmitFormAndWait(form_id, tab): |
- tab.ExecuteJavaScript( |
- 'document.getElementById("%s").submit();' % form_id) |
- |
- def FinishedLoading(): |
- return not tab.EvaluateJavaScript( |
- 'document.querySelector("#%s") !== null' % form_id) |
- |
- # Wait until the form is submitted and the page completes loading. |
- util.WaitFor(FinishedLoading, 60) |
- |
class FormBasedCredentialsBackend(object): |
def __init__(self): |
self._logged_in = False |
def IsAlreadyLoggedIn(self, tab): |
- raise NotImplementedError() |
+ return tab.EvaluateJavaScript(self.logged_in_javascript) |
@property |
def credentials_type(self): |
@@ -46,6 +26,11 @@ class FormBasedCredentialsBackend(object): |
raise NotImplementedError() |
@property |
+ def login_button_id(self): |
+ """Some sites (like codepen) have a button to click to log in.""" |
+ return None |
+ |
+ @property |
def login_input_id(self): |
raise NotImplementedError() |
@@ -53,6 +38,11 @@ class FormBasedCredentialsBackend(object): |
def password_input_id(self): |
raise NotImplementedError() |
+ @property |
+ def logged_in_javascript(self): |
+ """Evaluates to true iff already logged in.""" |
+ raise NotImplementedError() |
+ |
def IsLoggedIn(self): |
return self._logged_in |
@@ -62,7 +52,36 @@ class FormBasedCredentialsBackend(object): |
""" |
self._logged_in = False |
- def LoginNeeded(self, tab, config): |
+ def _WaitForLoginState(self, ar): |
tonyg
2014/08/21 22:34:05
s/ar/action_runner/ throughout.
|
+ """Waits until it can detect either the login form, or already logged in.""" |
+ condition = '(document.querySelector("#%s")!== null) || (%s)' % ( |
+ self.login_form_id, self.logged_in_javascript) |
+ ar.WaitForJavaScriptCondition(condition, 60) |
+ |
+ def _SubmitLoginFormAndWait(self, ar, tab, username, password): |
+ """Submits the login form and waits for the navigation.""" |
+ tab.WaitForDocumentReadyStateToBeComplete() |
+ email_id = 'document.querySelector("#%s #%s").value = "%s"; ' % ( |
+ self.login_form_id, self.login_input_id, username) |
+ password = 'document.querySelector("#%s #%s").value = "%s"; ' % ( |
+ self.login_form_id, self.password_input_id, password) |
+ tab.ExecuteJavaScript(email_id) |
+ tab.ExecuteJavaScript(password) |
+ if self.login_button_id: |
+ # May need to wait a bit before clicking button, this is required for |
+ # Codepen. |
+ ar.Wait(1) |
+ ar.ClickElement('#%s' % self.login_button_id) |
+ else: |
+ tab.ExecuteJavaScript( |
+ 'document.getElementById("%s").submit();' % self.login_form_id) |
+ # Wait for the form element to disappear as confirmation of the navigation. |
+ ar.WaitForJavaScriptCondition( |
+ 'document.querySelector("#%s") !== null' % self.login_form_id, 60) |
+ ar.Wait(1) # TODO(sullivan): no hack!!! |
sullivan
2014/08/21 21:57:04
This is the hack I really want to get rid of, is t
tonyg
2014/08/21 22:34:05
Have you tried replacing this and the WaitForJavaS
|
+ |
+ |
+ def LoginNeeded(self, tab, ar, config): |
"""Logs in to a test account. |
Raises: |
@@ -86,23 +105,14 @@ class FormBasedCredentialsBackend(object): |
try: |
logging.info('Loading %s...', url) |
tab.Navigate(url) |
- _WaitForLoginFormToLoad(self, self.login_form_id, tab) |
+ self._WaitForLoginState(ar) |
if self.IsAlreadyLoggedIn(tab): |
self._logged_in = True |
return True |
- tab.WaitForDocumentReadyStateToBeInteractiveOrBetter() |
- logging.info('Loaded page: %s', url) |
- |
- email_id = 'document.querySelector("#%s").%s.value = "%s"; ' % ( |
- self.login_form_id, self.login_input_id, config['username']) |
- password = 'document.querySelector("#%s").%s.value = "%s"; ' % ( |
- self.login_form_id, self.password_input_id, config['password']) |
- tab.ExecuteJavaScript(email_id) |
- tab.ExecuteJavaScript(password) |
- |
- _SubmitFormAndWait(self.login_form_id, tab) |
+ self._SubmitLoginFormAndWait( |
+ ar, tab, config['username'], config['password']) |
self._logged_in = True |
return True |