| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 logging | 5 import logging |
| 6 import os | 6 import os |
| 7 import time | 7 import time |
| 8 import utils | 8 import utils |
| 9 | 9 |
| 10 from telemetry import page | 10 from telemetry import page |
| 11 from telemetry import story | 11 from telemetry import story |
| 12 from telemetry.core import exceptions | 12 from telemetry.core import exceptions |
| 13 | 13 |
| 14 | 14 |
| 15 class CastPage(page.Page): | 15 class CastPage(page.Page): |
| 16 """Abstract Cast page for Media Router Telemetry tests.""" | 16 """Abstract Cast page for Media Router Telemetry tests.""" |
| 17 | 17 |
| 18 def ChooseSink(self, tab, sink_name): | 18 def ChooseSink(self, tab, sink_name): |
| 19 """Chooses a specific sink in the list.""" | 19 """Chooses a specific sink in the list.""" |
| 20 | 20 |
| 21 tab.ExecuteJavaScript( | 21 tab.ExecuteJavaScript2(""" |
| 22 'var sinks = window.document.getElementById("media-router-container").' | 22 var sinks = window.document.getElementById("media-router-container"). |
| 23 ' shadowRoot.getElementById("sink-list").getElementsByTagName("span");' | 23 shadowRoot.getElementById("sink-list").getElementsByTagName("span"); |
| 24 'for (var i=0; i<sinks.length; i++) {' | 24 for (var i=0; i<sinks.length; i++) { |
| 25 ' if(sinks[i].textContent.trim() == "%s") {' | 25 if(sinks[i].textContent.trim() == {{ sink_name }}) { |
| 26 ' sinks[i].click();' | 26 sinks[i].click(); |
| 27 ' break;' | 27 break; |
| 28 '}}' % sink_name); | 28 }} |
| 29 """, |
| 30 sink_name=sink_name) |
| 29 | 31 |
| 30 def CloseDialog(self, tab): | 32 def CloseDialog(self, tab): |
| 31 """Closes media router dialog.""" | 33 """Closes media router dialog.""" |
| 32 | 34 |
| 33 try: | 35 try: |
| 34 tab.ExecuteJavaScript( | 36 tab.ExecuteJavaScript2( |
| 35 'window.document.getElementById("media-router-container").' + | 37 'window.document.getElementById("media-router-container").' + |
| 36 'shadowRoot.getElementById("container-header").shadowRoot.' + | 38 'shadowRoot.getElementById("container-header").shadowRoot.' + |
| 37 'getElementById("close-button").click();') | 39 'getElementById("close-button").click();') |
| 38 except exceptions.DevtoolsTargetCrashException: | 40 except exceptions.DevtoolsTargetCrashException: |
| 39 # Ignore the crash exception, this exception is caused by the js | 41 # Ignore the crash exception, this exception is caused by the js |
| 40 # code which closes the dialog, it is expected. | 42 # code which closes the dialog, it is expected. |
| 41 pass | 43 pass |
| 42 | 44 |
| 43 def CloseExistingRoute(self, action_runner, sink_name): | 45 def CloseExistingRoute(self, action_runner, sink_name): |
| 44 """Closes the existing route if it exists, otherwise does nothing.""" | 46 """Closes the existing route if it exists, otherwise does nothing.""" |
| 45 | 47 |
| 46 action_runner.TapElement(selector='#start_session_button') | 48 action_runner.TapElement(selector='#start_session_button') |
| 47 action_runner.Wait(5) | 49 action_runner.Wait(5) |
| 48 for tab in action_runner.tab.browser.tabs: | 50 for tab in action_runner.tab.browser.tabs: |
| 49 if tab.url == 'chrome://media-router/': | 51 if tab.url == 'chrome://media-router/': |
| 50 if self.CheckIfExistingRoute(tab, sink_name): | 52 if self.CheckIfExistingRoute(tab, sink_name): |
| 51 self.ChooseSink(tab, sink_name) | 53 self.ChooseSink(tab, sink_name) |
| 52 tab.ExecuteJavaScript( | 54 tab.ExecuteJavaScript2( |
| 53 "window.document.getElementById('media-router-container')." | 55 "window.document.getElementById('media-router-container')." |
| 54 "shadowRoot.getElementById('route-details').shadowRoot." | 56 "shadowRoot.getElementById('route-details').shadowRoot." |
| 55 "getElementById('close-route-button').click();") | 57 "getElementById('close-route-button').click();") |
| 56 self.CloseDialog(tab) | 58 self.CloseDialog(tab) |
| 57 # Wait for 5s to make sure the route is closed. | 59 # Wait for 5s to make sure the route is closed. |
| 58 action_runner.Wait(5) | 60 action_runner.Wait(5) |
| 59 | 61 |
| 60 def CheckIfExistingRoute(self, tab, sink_name): | 62 def CheckIfExistingRoute(self, tab, sink_name): |
| 61 """"Checks if there is existing route for the specific sink.""" | 63 """"Checks if there is existing route for the specific sink.""" |
| 62 | 64 |
| 63 tab.ExecuteJavaScript( | 65 tab.ExecuteJavaScript2(""" |
| 64 "var sinks = window.document.getElementById('media-router-container')." | 66 var sinks = window.document.getElementById('media-router-container'). |
| 65 " allSinks;" | 67 allSinks; |
| 66 "var sink_id = null;" | 68 var sink_id = null; |
| 67 "for (var i=0; i<sinks.length; i++) {" | 69 for (var i=0; i<sinks.length; i++) { |
| 68 " if (sinks[i].name == '%s') {" | 70 if (sinks[i].name == {{ sink_name }}) { |
| 69 " console.info('sink id: ' + sinks[i].id); " | 71 console.info('sink id: ' + sinks[i].id); |
| 70 " sink_id = sinks[i].id;" | 72 sink_id = sinks[i].id; |
| 71 " break;" | 73 break; |
| 72 " }" | 74 } |
| 73 "}" | 75 } |
| 74 "var routes = window.document.getElementById('media-router-container')." | 76 var routes = window.document.getElementById('media-router-container'). |
| 75 " routeList;" | 77 routeList; |
| 76 "for (var i=0; i<routes.length; i++) {" | 78 for (var i=0; i<routes.length; i++) { |
| 77 " if (!!sink_id && routes[i].sinkId == sink_id) {" | 79 if (!!sink_id && routes[i].sinkId == sink_id) { |
| 78 " window.__telemetry_route_id = routes[i].id;" | 80 window.__telemetry_route_id = routes[i].id; |
| 79 " break;" | 81 break; |
| 80 " }" | 82 } |
| 81 "}" % sink_name) | 83 }""", |
| 82 route = tab.EvaluateJavaScript('!!window.__telemetry_route_id') | 84 sink_name=sink_name) |
| 85 route = tab.EvaluateJavaScript2('!!window.__telemetry_route_id') |
| 83 logging.info('Is there existing route? ' + str(route)) | 86 logging.info('Is there existing route? ' + str(route)) |
| 84 return route | 87 return route |
| 85 | 88 |
| 86 def ExecuteAsyncJavaScript(self, action_runner, script, verify_func, | 89 def ExecuteAsyncJavaScript(self, action_runner, script, verify_func, |
| 87 error_message, timeout=5): | 90 error_message, timeout=5): |
| 88 """Executes async javascript function and waits until it finishes.""" | 91 """Executes async javascript function and waits until it finishes.""" |
| 89 | 92 |
| 90 action_runner.ExecuteJavaScript(script) | 93 action_runner.ExecuteJavaScript2(script) |
| 91 self._WaitForResult(action_runner, verify_func, error_message, | 94 self._WaitForResult(action_runner, verify_func, error_message, |
| 92 timeout=timeout) | 95 timeout=timeout) |
| 93 | 96 |
| 94 def WaitUntilDialogLoaded(self, action_runner, tab): | 97 def WaitUntilDialogLoaded(self, action_runner, tab): |
| 95 """Waits until dialog is fully loaded.""" | 98 """Waits until dialog is fully loaded.""" |
| 96 | 99 |
| 97 self._WaitForResult( | 100 self._WaitForResult( |
| 98 action_runner, | 101 action_runner, |
| 99 lambda: tab.EvaluateJavaScript( | 102 lambda: tab.EvaluateJavaScript2( |
| 100 '!!window.document.getElementById(' | 103 '!!window.document.getElementById(' |
| 101 '"media-router-container") &&' | 104 '"media-router-container") &&' |
| 102 'window.document.getElementById(' | 105 'window.document.getElementById(' |
| 103 '"media-router-container").sinksToShow_ &&' | 106 '"media-router-container").sinksToShow_ &&' |
| 104 'window.document.getElementById(' | 107 'window.document.getElementById(' |
| 105 '"media-router-container").sinksToShow_.length'), | 108 '"media-router-container").sinksToShow_.length'), |
| 106 'The dialog is not fully loaded within 15s.', | 109 'The dialog is not fully loaded within 15s.', |
| 107 timeout=15) | 110 timeout=15) |
| 108 | 111 |
| 109 def _WaitForResult(self, action_runner, verify_func, error_message, | 112 def _WaitForResult(self, action_runner, verify_func, error_message, |
| 110 timeout=5): | 113 timeout=5): |
| 111 """Waits until the function finishes or timeout.""" | 114 """Waits until the function finishes or timeout.""" |
| 112 | 115 |
| 113 start_time = time.time() | 116 start_time = time.time() |
| 114 while (not verify_func() and | 117 while (not verify_func() and |
| 115 time.time() - start_time < timeout): | 118 time.time() - start_time < timeout): |
| 116 action_runner.Wait(1) | 119 action_runner.Wait(1) |
| 117 if not verify_func(): | 120 if not verify_func(): |
| 118 raise page.page_test.Failure(error_message) | 121 raise page.page_test.Failure(error_message) |
| 119 | 122 |
| 120 def _GetDeviceName(self): | 123 def _GetDeviceName(self): |
| 121 """Gets device name from environment variable RECEIVER_NAME.""" | 124 """Gets device name from environment variable RECEIVER_NAME.""" |
| 122 | 125 |
| 123 if 'RECEIVER_IP' not in os.environ or not os.environ.get('RECEIVER_IP'): | 126 if 'RECEIVER_IP' not in os.environ or not os.environ.get('RECEIVER_IP'): |
| 124 raise page.page_test.Failure( | 127 raise page.page_test.Failure( |
| 125 'Your test machine is not set up correctly, ' | 128 'Your test machine is not set up correctly, ' |
| 126 'RECEIVER_IP enviroment variable is missing.') | 129 'RECEIVER_IP enviroment variable is missing.') |
| 127 return utils.GetDeviceName(os.environ.get('RECEIVER_IP')) | 130 return utils.GetDeviceName(os.environ.get('RECEIVER_IP')) |
| OLD | NEW |