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