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.
14 class TimeoutException(Exception):
19 main_module = sys.modules['__main__']
20 if hasattr(main_module, '__file__'):
21 return os.path.dirname(os.path.abspath(main_module.__file__))
26 def GetTelemetryDir():
27 return os.path.normpath(os.path.join(
28 __file__, os.pardir, os.pardir, os.pardir))
31 def GetUnittestDataDir():
32 return os.path.join(GetTelemetryDir(), 'unittest_data')
35 def GetChromiumSrcDir():
36 return os.path.normpath(os.path.join(GetTelemetryDir(), os.pardir, os.pardir))
39 def AddDirToPythonPath(*path_parts):
40 path = os.path.abspath(os.path.join(*path_parts))
41 if os.path.isdir(path) and path not in sys.path:
45 def _GetUniqueModuleName():
47 return "page_set_module_" + str(_counter[0])
49 def GetPythonPageSetModule(file_path):
50 return imp.load_source(_GetUniqueModuleName(), file_path)
53 def WaitFor(condition, timeout):
54 """Waits for up to |timeout| secs for the function |condition| to return True.
56 Polling frequency is (elapsed_time / 10), with a min of .1s and max of 5s.
59 Result of |condition| function (if present).
61 min_poll_interval = 0.1
65 def GetConditionString():
66 if condition.__name__ == '<lambda>':
68 return inspect.getsource(condition).strip()
71 return condition.__name__
73 start_time = time.time()
74 last_output_time = start_time
80 elapsed_time = now - start_time
81 last_output_elapsed_time = now - last_output_time
82 if elapsed_time > timeout:
83 raise TimeoutException('Timed out while waiting %ds for %s.' %
84 (timeout, GetConditionString()))
85 if last_output_elapsed_time > output_interval:
86 logging.info('Continuing to wait %ds for %s. Elapsed: %ds.',
87 timeout, GetConditionString(), elapsed_time)
88 last_output_time = time.time()
89 poll_interval = min(max(elapsed_time / 10., min_poll_interval),
91 time.sleep(poll_interval)
94 def FindElementAndPerformAction(tab, text, callback_code):
95 """JavaScript snippet for finding an element with a given text on a page."""
98 var callback_function = """ + callback_code + """;
99 function _findElement(element, text) {
100 if (element.innerHTML == text) {
104 for (var i in element.childNodes) {
105 var found = _findElement(element.childNodes[i], text);
111 var _element = _findElement(document, \"""" + text + """\");
112 return callback_function(_element);
114 return tab.EvaluateJavaScript(code)
117 def GetUnreservedAvailableLocalPort():
118 """Returns an available port on the system.
120 WARNING: This method does not reserve the port it returns, so it may be used
121 by something else before you get to use it. This can lead to flake.
123 tmp = socket.socket()
125 port = tmp.getsockname()[1]
131 def CloseConnections(tab):
132 """Closes all TCP sockets held open by the browser."""
134 tab.ExecuteJavaScript("""window.chrome && chrome.benchmarking &&
135 chrome.benchmarking.closeConnections()""")
140 def GetBuildDirectories():
141 """Yields all combination of Chromium build output directories."""
142 build_dirs = ['build',
143 os.path.basename(os.environ.get('CHROMIUM_OUT_DIR', 'out')),
146 build_types = ['Debug', 'Debug_x64', 'Release', 'Release_x64']
148 for build_dir in build_dirs:
149 for build_type in build_types:
150 yield build_dir, build_type