| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 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 os |
| 6 import time |
| 7 |
| 8 from telemetry import page |
| 9 from telemetry import story |
| 10 from telemetry.core import exceptions |
| 11 |
| 12 |
| 13 class CastPage(page.Page): |
| 14 """Abstract Cast page for Media Router Telemetry tests.""" |
| 15 |
| 16 def ChooseSink(self, tab, sink_name): |
| 17 """Chooses a specific sink in the list.""" |
| 18 |
| 19 tab.ExecuteJavaScript( |
| 20 'var sinks = window.document.getElementById("media-router-container").' |
| 21 ' shadowRoot.getElementById("sink-list").getElementsByTagName("span");' |
| 22 'for (var i=0; i<sinks.length; i++) {' |
| 23 ' if(sinks[i].textContent.trim() == "%s") {' |
| 24 ' sinks[i].click();' |
| 25 ' break;' |
| 26 '}}' % sink_name); |
| 27 |
| 28 def CloseDialog(self, tab): |
| 29 """Closes media router dialog.""" |
| 30 |
| 31 try: |
| 32 tab.ExecuteJavaScript( |
| 33 'window.document.getElementById("media-router-container").' + |
| 34 'shadowRoot.getElementById("container-header").shadowRoot.' + |
| 35 'getElementById("close-button").click();') |
| 36 except exceptions.DevtoolsTargetCrashException: |
| 37 # Ignore the crash exception, this exception is caused by the js |
| 38 # code which closes the dialog, it is expected. |
| 39 pass |
| 40 |
| 41 def ExecuteAsyncJavaScript(self, action_runner, script, verify_func, |
| 42 error_message, timeout=5): |
| 43 """Executes async javascript function and waits until it finishes.""" |
| 44 |
| 45 action_runner.ExecuteJavaScript(script) |
| 46 self._WaitForResult(action_runner, verify_func, error_message, |
| 47 timeout=timeout) |
| 48 |
| 49 def _WaitForResult(self, action_runner, verify_func, error_message, |
| 50 timeout=5): |
| 51 """Waits until the function finishes or timeout.""" |
| 52 |
| 53 start_time = time.time() |
| 54 while (not verify_func() and |
| 55 time.time() - start_time < timeout): |
| 56 action_runner.Wait(1) |
| 57 if not verify_func(): |
| 58 raise page.page_test.Failure(error_message) |
| 59 |
| 60 def _GetDeviceName(self): |
| 61 """Gets device name from environment variable RECEIVER_NAME.""" |
| 62 if 'RECEIVER_NAME' not in os.environ or not os.environ.get('RECEIVER_NAME'): |
| 63 raise page.page_test.Failure( |
| 64 'Your test machine is not set up correctly, ' |
| 65 'RECEIVER_NAME enviroment variable is missing.') |
| 66 return os.environ.get('RECEIVER_NAME') |
| OLD | NEW |