[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / perf / page_sets / login_helpers / login_utils.py
bloba58a64caea2b55396ffb7653e1abf4e7b70f4e35
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
3 # found in the LICENSE file.
5 import json
6 import os
9 DEFAULT_CREDENTIAL_PATH = os.path.join(
10 os.path.dirname(__file__), os.path.pardir, 'data', 'credentials.json')
13 def GetAccountNameAndPassword(credential,
14 credentials_path=DEFAULT_CREDENTIAL_PATH):
15 """Returns username and password for |credential| in credentials_path file.
17 Args:
18 credential: The credential to retrieve from the file (type string).
19 credentials_path: The string that specifies the path to credential file.
21 Returns:
22 A tuple (username, password) in which both are username and password
23 strings.
24 """
25 with open(credentials_path, 'r') as f:
26 credentials = json.load(f)
27 c = credentials.get(credential)
28 return c['username'], c['password']
31 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.
34 Waits until the input element exists on the page. Then executes JS to populate
35 the value property of the element with |input_text|.
37 Args:
38 action_runner: ActionRunner instance to execute JS to populate form fields.
39 input_text: Text string to populate the input field with.
40 input_id: Id of the input field to populate. (type string).
41 form_id: Optional form id string to identify |input_id| in querySelector.
43 Raises:
44 exceptions.TimeoutException: If waiting to find the element times out.
45 exceptions.Error: See ExecuteJavaScript() for a detailed list of
46 possible exceptions.
47 """
48 if form_id and input_id:
49 element_selector = '#%s #%s' % (form_id, input_id)
50 elif input_id:
51 element_selector = '#%s' % (input_id)
52 else:
53 raise ValueError("Input ID can not be None or empty.")
54 action_runner.WaitForElement(selector=element_selector)
55 action_runner.ExecuteJavaScript(
56 'document.querySelector("%s").value = "%s";' %
57 (element_selector, input_text))