1 # Copyright 2014 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 from telemetry import benchmark
6 from telemetry.core import exceptions
7 from telemetry.core import util
8 from telemetry.core.platform import tracing_category_filter
9 from telemetry.core.platform import tracing_options
10 from telemetry.page.actions import action_runner as action_runner_module
11 from telemetry.page.actions import page_action
12 from telemetry.timeline import model
13 from telemetry.unittest import tab_test_case
14 from telemetry.web_perf import timeline_interaction_record as tir_module
17 class ActionRunnerInteractionTest(tab_test_case.TabTestCase):
19 def GetInteractionRecords(self, trace_data):
20 timeline_model = model.TimelineModel(trace_data)
21 renderer_thread = timeline_model.GetRendererThreadFromTabId(self._tab.id)
23 tir_module.TimelineInteractionRecord.FromAsyncEvent(e)
24 for e in renderer_thread.async_slices
25 if tir_module.IsTimelineInteractionRecord(e.name)
28 def VerifyIssuingInteractionRecords(self, **interaction_kwargs):
29 action_runner = action_runner_module.ActionRunner(self._tab,
31 self.Navigate('interaction_enabled_page.html')
33 options = tracing_options.TracingOptions()
34 options.enable_chrome_trace = True
35 self._browser.platform.tracing_controller.Start(
36 options, tracing_category_filter.CreateNoOverheadFilter())
37 interaction = action_runner.BeginInteraction('InteractionName',
40 trace_data = self._browser.platform.tracing_controller.Stop()
42 records = self.GetInteractionRecords(trace_data)
45 'Failed to issue the interaction record on the tracing timeline.'
46 ' Trace data:\n%s' % repr(trace_data.EventData()))
47 self.assertEqual('InteractionName', records[0].label)
48 for attribute_name in interaction_kwargs:
49 self.assertTrue(getattr(records[0], attribute_name))
51 @benchmark.Disabled('chromeos')
52 def testIssuingMultipleMeasurementInteractionRecords(self):
53 self.VerifyIssuingInteractionRecords(is_fast=True)
54 self.VerifyIssuingInteractionRecords(is_responsive=True)
55 self.VerifyIssuingInteractionRecords(is_smooth=True)
56 self.VerifyIssuingInteractionRecords(is_fast=True, is_smooth=True)
59 class ActionRunnerTest(tab_test_case.TabTestCase):
60 def testExecuteJavaScript(self):
61 action_runner = action_runner_module.ActionRunner(self._tab,
63 self.Navigate('blank.html')
64 action_runner.ExecuteJavaScript('var testing = 42;')
65 self.assertEqual(42, self._tab.EvaluateJavaScript('testing'))
67 def testWaitForNavigate(self):
68 self.Navigate('page_with_link.html')
69 action_runner = action_runner_module.ActionRunner(self._tab,
71 action_runner.ClickElement('#clickme')
72 action_runner.WaitForNavigate()
74 self.assertTrue(self._tab.EvaluateJavaScript(
75 'document.readyState == "interactive" || '
76 'document.readyState == "complete"'))
78 self._tab.EvaluateJavaScript('document.location.pathname;'),
82 action_runner = action_runner_module.ActionRunner(self._tab)
83 self.Navigate('blank.html')
85 action_runner.ExecuteJavaScript(
86 'window.setTimeout(function() { window.testing = 101; }, 50);')
87 action_runner.Wait(0.1)
88 self.assertEqual(101, self._tab.EvaluateJavaScript('window.testing'))
90 action_runner.ExecuteJavaScript(
91 'window.setTimeout(function() { window.testing = 102; }, 100);')
92 action_runner.Wait(0.2)
93 self.assertEqual(102, self._tab.EvaluateJavaScript('window.testing'))
95 def testWaitForJavaScriptCondition(self):
96 action_runner = action_runner_module.ActionRunner(self._tab,
98 self.Navigate('blank.html')
100 action_runner.ExecuteJavaScript('window.testing = 219;')
101 action_runner.WaitForJavaScriptCondition(
102 'window.testing == 219', timeout_in_seconds=0.1)
103 action_runner.ExecuteJavaScript(
104 'window.setTimeout(function() { window.testing = 220; }, 50);')
105 action_runner.WaitForJavaScriptCondition(
106 'window.testing == 220', timeout_in_seconds=0.1)
107 self.assertEqual(220, self._tab.EvaluateJavaScript('window.testing'))
109 def testWaitForElement(self):
110 action_runner = action_runner_module.ActionRunner(self._tab,
112 self.Navigate('blank.html')
114 action_runner.ExecuteJavaScript(
116 ' var el = document.createElement("div");'
118 ' el.textContent = "foo";'
119 ' document.body.appendChild(el);'
121 action_runner.WaitForElement('#test1', timeout_in_seconds=0.1)
122 action_runner.WaitForElement(text='foo', timeout_in_seconds=0.1)
123 action_runner.WaitForElement(
124 element_function='document.getElementById("test1")')
125 action_runner.ExecuteJavaScript(
126 'window.setTimeout(function() {'
127 ' var el = document.createElement("div");'
129 ' document.body.appendChild(el);'
131 action_runner.WaitForElement('#test2', timeout_in_seconds=0.1)
132 action_runner.ExecuteJavaScript(
133 'window.setTimeout(function() {'
134 ' document.getElementById("test2").textContent = "bar";'
136 action_runner.WaitForElement(text='bar', timeout_in_seconds=0.1)
137 action_runner.ExecuteJavaScript(
138 'window.setTimeout(function() {'
139 ' var el = document.createElement("div");'
141 ' document.body.appendChild(el);'
143 action_runner.WaitForElement(
144 element_function='document.getElementById("test3")')
146 def testWaitForElementWithWrongText(self):
147 action_runner = action_runner_module.ActionRunner(self._tab,
149 self.Navigate('blank.html')
151 action_runner.ExecuteJavaScript(
153 ' var el = document.createElement("div");'
155 ' el.textContent = "foo";'
156 ' document.body.appendChild(el);'
158 action_runner.WaitForElement('#test1', timeout_in_seconds=0.2)
159 def WaitForElement():
160 action_runner.WaitForElement(text='oo', timeout_in_seconds=0.2)
161 self.assertRaises(util.TimeoutException, WaitForElement)
163 def testClickElement(self):
164 self.Navigate('page_with_clickables.html')
165 action_runner = action_runner_module.ActionRunner(self._tab,
168 action_runner.ExecuteJavaScript('valueSettableByTest = 1;')
169 action_runner.ClickElement('#test')
170 self.assertEqual(1, action_runner.EvaluateJavaScript('valueToTest'))
172 action_runner.ExecuteJavaScript('valueSettableByTest = 2;')
173 action_runner.ClickElement(text='Click/tap me')
174 self.assertEqual(2, action_runner.EvaluateJavaScript('valueToTest'))
176 action_runner.ExecuteJavaScript('valueSettableByTest = 3;')
177 action_runner.ClickElement(
178 element_function='document.body.firstElementChild;')
179 self.assertEqual(3, action_runner.EvaluateJavaScript('valueToTest'))
182 action_runner.ClickElement('#notfound')
183 self.assertRaises(exceptions.EvaluateException, WillFail)
185 @benchmark.Disabled('debug')
186 def testTapElement(self):
187 self.Navigate('page_with_clickables.html')
188 action_runner = action_runner_module.ActionRunner(self._tab,
191 action_runner.ExecuteJavaScript('valueSettableByTest = 1;')
192 action_runner.TapElement('#test')
193 self.assertEqual(1, action_runner.EvaluateJavaScript('valueToTest'))
195 action_runner.ExecuteJavaScript('valueSettableByTest = 2;')
196 action_runner.TapElement(text='Click/tap me')
197 self.assertEqual(2, action_runner.EvaluateJavaScript('valueToTest'))
199 action_runner.ExecuteJavaScript('valueSettableByTest = 3;')
200 action_runner.TapElement(
201 element_function='document.body.firstElementChild')
202 self.assertEqual(3, action_runner.EvaluateJavaScript('valueToTest'))
205 action_runner.TapElement('#notfound')
206 self.assertRaises(exceptions.EvaluateException, WillFail)
208 def testScroll(self):
209 if not page_action.IsGestureSourceTypeSupported(
213 self.Navigate('page_with_swipeables.html')
214 action_runner = action_runner_module.ActionRunner(self._tab,
217 action_runner.ScrollElement(
218 selector='#left-right', direction='right', left_start_ratio=0.9)
219 self.assertTrue(action_runner.EvaluateJavaScript(
220 'document.querySelector("#left-right").scrollLeft') > 75)
221 action_runner.ScrollElement(
222 selector='#top-bottom', direction='down', top_start_ratio=0.9)
223 self.assertTrue(action_runner.EvaluateJavaScript(
224 'document.querySelector("#top-bottom").scrollTop') > 75)
226 action_runner.ScrollPage(direction='right', left_start_ratio=0.9,
228 self.assertTrue(action_runner.EvaluateJavaScript(
229 'document.body.scrollLeft') > 75)
232 if not page_action.IsGestureSourceTypeSupported(
236 self.Navigate('page_with_swipeables.html')
237 action_runner = action_runner_module.ActionRunner(self._tab,
240 action_runner.SwipeElement(
241 selector='#left-right', direction='left', left_start_ratio=0.9)
242 self.assertTrue(action_runner.EvaluateJavaScript(
243 'document.querySelector("#left-right").scrollLeft') > 75)
244 action_runner.SwipeElement(
245 selector='#top-bottom', direction='up', top_start_ratio=0.9)
246 self.assertTrue(action_runner.EvaluateJavaScript(
247 'document.querySelector("#top-bottom").scrollTop') > 75)
249 action_runner.SwipePage(direction='left', left_start_ratio=0.9)
250 self.assertTrue(action_runner.EvaluateJavaScript(
251 'document.body.scrollLeft') > 75)