| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | 5 import json |
| 6 import os | 6 import os |
| 7 | 7 |
| 8 | 8 |
| 9 DEFAULT_CREDENTIAL_PATH = os.path.join( | 9 DEFAULT_CREDENTIAL_PATH = os.path.join( |
| 10 os.path.dirname(__file__), os.path.pardir, 'data', 'credentials.json') | 10 os.path.dirname(__file__), os.path.pardir, 'data', 'credentials.json') |
| 11 | 11 |
| 12 | 12 |
| 13 def GetAccountNameAndPassword(credential, | 13 def GetAccountNameAndPassword(credential, |
| 14 credentials_path=DEFAULT_CREDENTIAL_PATH): | 14 credentials_path=DEFAULT_CREDENTIAL_PATH): |
| 15 """Returns username and password for |credential| in credentials_path file. | 15 """Returns username and password for |credential| in credentials_path file. |
| 16 | 16 |
| 17 Args: | 17 Args: |
| 18 credential: The credential to retrieve from the file (type string). | 18 credential: The credential to retrieve from the file (type string). |
| 19 credentials_path: The string that specifies the path to credential file. | 19 credentials_path: The string that specifies the path to credential file. |
| 20 | 20 |
| 21 Returns: | 21 Returns: |
| 22 A tuple (username, password) in which both are username and password | 22 A tuple (username, password) in which both are username and password |
| 23 strings. | 23 strings. |
| 24 """ | 24 """ |
| 25 with open(credentials_path, 'r') as f: | 25 with open(credentials_path, 'r') as f: |
| 26 credentials = json.load(f) | 26 credentials = json.load(f) |
| 27 c = credentials.get(credential) | 27 c = credentials.get(credential) |
| 28 return c['username'], c['password'] | 28 return c['username'], c['password'] |
| 29 | 29 |
| 30 def InputWithSelector(action_runner, input_text, input_selector): |
| 31 """Sets the text value of an input field in a form on the page. |
| 32 |
| 33 Waits until the input element exists on the page. Then executes JS to populate |
| 34 the value property of the element with |input_text|. |
| 35 |
| 36 Args: |
| 37 action_runner: ActionRunner instance to execute JS to populate form fields. |
| 38 input_text: Text string to populate the input field with. |
| 39 input_selector: The selector of the input element. |
| 40 |
| 41 |
| 42 Raises: |
| 43 exceptions.TimeoutException: If waiting to find the element times out. |
| 44 exceptions.Error: See ExecuteJavaScript() for a detailed list of |
| 45 possible exceptions. |
| 46 """ |
| 47 action_runner.WaitForElement(selector=input_selector) |
| 48 action_runner.ExecuteJavaScript( |
| 49 'document.querySelector("%s").value = "%s";' % |
| 50 (input_selector, input_text)) |
| 30 | 51 |
| 31 def InputForm(action_runner, input_text, input_id, form_id=None): | 52 def InputForm(action_runner, input_text, input_id, form_id=None): |
| 32 """Sets the text value of an input field in a form on the page. | 53 """Sets the text value of an input field in a form on the page. |
| 33 | 54 |
| 34 Waits until the input element exists on the page. Then executes JS to populate | 55 Waits until the input element exists on the page. Then executes JS to populate |
| 35 the value property of the element with |input_text|. | 56 the value property of the element with |input_text|. |
| 36 | 57 |
| 37 Args: | 58 Args: |
| 38 action_runner: ActionRunner instance to execute JS to populate form fields. | 59 action_runner: ActionRunner instance to execute JS to populate form fields. |
| 39 input_text: Text string to populate the input field with. | 60 input_text: Text string to populate the input field with. |
| 40 input_id: Id of the input field to populate. (type string). | 61 input_id: Id of the input field to populate. (type string). |
| 41 form_id: Optional form id string to identify |input_id| in querySelector. | 62 form_id: Optional form id string to identify |input_id| in querySelector. |
| 42 | 63 |
| 43 Raises: | 64 Raises: |
| 44 exceptions.TimeoutException: If waiting to find the element times out. | 65 exceptions.TimeoutException: If waiting to find the element times out. |
| 45 exceptions.Error: See ExecuteJavaScript() for a detailed list of | 66 exceptions.Error: See ExecuteJavaScript() for a detailed list of |
| 46 possible exceptions. | 67 possible exceptions. |
| 47 """ | 68 """ |
| 48 if form_id and input_id: | 69 if form_id and input_id: |
| 49 element_selector = '#%s #%s' % (form_id, input_id) | 70 element_selector = '#%s #%s' % (form_id, input_id) |
| 50 elif input_id: | 71 elif input_id: |
| 51 element_selector = '#%s' % (input_id) | 72 element_selector = '#%s' % (input_id) |
| 52 else: | 73 else: |
| 53 raise ValueError("Input ID can not be None or empty.") | 74 raise ValueError("Input ID can not be None or empty.") |
| 54 action_runner.WaitForElement(selector=element_selector) | 75 InputWithSelector(action_runner, input_text, element_selector) |
| 55 action_runner.ExecuteJavaScript( | 76 |
| 56 'document.querySelector("%s").value = "%s";' % | |
| 57 (element_selector, input_text)) | |
| OLD | NEW |