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.page.actions import action_runner as action_runner_module
9 from telemetry.page.actions import page_action
10 from telemetry.timeline import model
11 from telemetry.unittest import tab_test_case
12 from telemetry.web_perf import timeline_interaction_record as tir_module
15 class ActionRunnerInteractionTest(tab_test_case.TabTestCase):
17 def GetInteractionRecords(self, trace_data):
18 timeline_model = model.TimelineModel(trace_data)
19 renderer_thread = timeline_model.GetRendererThreadFromTabId(self._tab.id)
21 tir_module.TimelineInteractionRecord.FromAsyncEvent(e)
22 for e in renderer_thread.async_slices
23 if tir_module.IsTimelineInteractionRecord(e.name)
26 def VerifyIssuingInteractionRecords(self, **interaction_kwargs):
27 action_runner = action_runner_module.ActionRunner(self._tab)
28 self.Navigate('interaction_enabled_page.html')
30 self._browser.StartTracing()
31 interaction = action_runner.BeginInteraction('InteractionName',
34 trace_data = self._browser.StopTracing()
36 records = self.GetInteractionRecords(trace_data)
39 'Failed to issue the interaction record on the tracing timeline.'
40 ' Trace data:\n%s' % repr(trace_data.EventData()))
41 self.assertEqual('InteractionName', records[0].label)
42 for attribute_name in interaction_kwargs:
43 self.assertTrue(getattr(records[0], attribute_name))
45 def testIssuingMultipleMeasurementInteractionRecords(self):
46 self.VerifyIssuingInteractionRecords(is_fast=True)
47 self.VerifyIssuingInteractionRecords(is_responsive=True)
48 self.VerifyIssuingInteractionRecords(is_smooth=True)
49 self.VerifyIssuingInteractionRecords(is_fast=True, is_smooth=True)
52 class ActionRunnerTest(tab_test_case.TabTestCase):
53 def testExecuteJavaScript(self):
54 action_runner = action_runner_module.ActionRunner(self._tab)
55 self.Navigate('blank.html')
56 action_runner.ExecuteJavaScript('var testing = 42;')
57 self.assertEqual(42, self._tab.EvaluateJavaScript('testing'))
59 def testWaitForNavigate(self):
60 self.Navigate('page_with_link.html')
61 action_runner = action_runner_module.ActionRunner(self._tab)
62 action_runner.ClickElement('#clickme')
63 action_runner.WaitForNavigate()
65 self.assertTrue(self._tab.EvaluateJavaScript(
66 'document.readyState == "interactive" || '
67 'document.readyState == "complete"'))
69 self._tab.EvaluateJavaScript('document.location.pathname;'),
73 action_runner = action_runner_module.ActionRunner(self._tab)
74 self.Navigate('blank.html')
76 action_runner.ExecuteJavaScript(
77 'window.setTimeout(function() { window.testing = 101; }, 50);')
78 action_runner.Wait(0.1)
79 self.assertEqual(101, self._tab.EvaluateJavaScript('window.testing'))
81 action_runner.ExecuteJavaScript(
82 'window.setTimeout(function() { window.testing = 102; }, 100);')
83 action_runner.Wait(0.2)
84 self.assertEqual(102, self._tab.EvaluateJavaScript('window.testing'))
86 def testWaitForJavaScriptCondition(self):
87 action_runner = action_runner_module.ActionRunner(self._tab)
88 self.Navigate('blank.html')
90 action_runner.ExecuteJavaScript('window.testing = 219;')
91 action_runner.WaitForJavaScriptCondition(
92 'window.testing == 219', timeout_in_seconds=0.1)
93 action_runner.ExecuteJavaScript(
94 'window.setTimeout(function() { window.testing = 220; }, 50);')
95 action_runner.WaitForJavaScriptCondition(
96 'window.testing == 220', timeout_in_seconds=0.1)
97 self.assertEqual(220, self._tab.EvaluateJavaScript('window.testing'))
99 def testWaitForElement(self):
100 action_runner = action_runner_module.ActionRunner(self._tab)
101 self.Navigate('blank.html')
103 action_runner.ExecuteJavaScript(
105 ' var el = document.createElement("div");'
107 ' el.textContent = "foo";'
108 ' document.body.appendChild(el);'
110 action_runner.WaitForElement('#test1', timeout_in_seconds=0.1)
111 action_runner.WaitForElement(text='foo', timeout_in_seconds=0.1)
112 action_runner.WaitForElement(
113 element_function='document.getElementById("test1")')
114 action_runner.ExecuteJavaScript(
115 'window.setTimeout(function() {'
116 ' var el = document.createElement("div");'
118 ' document.body.appendChild(el);'
120 action_runner.WaitForElement('#test2', timeout_in_seconds=0.1)
121 action_runner.ExecuteJavaScript(
122 'window.setTimeout(function() {'
123 ' document.getElementById("test2").textContent = "bar";'
125 action_runner.WaitForElement(text='bar', timeout_in_seconds=0.1)
126 action_runner.ExecuteJavaScript(
127 'window.setTimeout(function() {'
128 ' var el = document.createElement("div");'
130 ' document.body.appendChild(el);'
132 action_runner.WaitForElement(
133 element_function='document.getElementById("test3")')
135 def testWaitForElementWithWrongText(self):
136 action_runner = action_runner_module.ActionRunner(self._tab)
137 self.Navigate('blank.html')
139 action_runner.ExecuteJavaScript(
141 ' var el = document.createElement("div");'
143 ' el.textContent = "foo";'
144 ' document.body.appendChild(el);'
146 action_runner.WaitForElement('#test1', timeout_in_seconds=0.2)
147 def WaitForElement():
148 action_runner.WaitForElement(text='oo', timeout_in_seconds=0.2)
149 self.assertRaises(util.TimeoutException, WaitForElement)
151 def testClickElement(self):
152 self.Navigate('page_with_clickables.html')
153 action_runner = action_runner_module.ActionRunner(self._tab)
155 action_runner.ExecuteJavaScript('valueSettableByTest = 1;')
156 action_runner.ClickElement('#test')
157 self.assertEqual(1, action_runner.EvaluateJavaScript('valueToTest'))
159 action_runner.ExecuteJavaScript('valueSettableByTest = 2;')
160 action_runner.ClickElement(text='Click/tap me')
161 self.assertEqual(2, action_runner.EvaluateJavaScript('valueToTest'))
163 action_runner.ExecuteJavaScript('valueSettableByTest = 3;')
164 action_runner.ClickElement(
165 element_function='document.body.firstElementChild;')
166 self.assertEqual(3, action_runner.EvaluateJavaScript('valueToTest'))
169 action_runner.ClickElement('#notfound')
170 self.assertRaises(exceptions.EvaluateException, WillFail)
172 @benchmark.Disabled('debug')
173 def testTapElement(self):
174 self.Navigate('page_with_clickables.html')
175 action_runner = action_runner_module.ActionRunner(self._tab)
177 action_runner.ExecuteJavaScript('valueSettableByTest = 1;')
178 action_runner.TapElement('#test')
179 self.assertEqual(1, action_runner.EvaluateJavaScript('valueToTest'))
181 action_runner.ExecuteJavaScript('valueSettableByTest = 2;')
182 action_runner.TapElement(text='Click/tap me')
183 self.assertEqual(2, action_runner.EvaluateJavaScript('valueToTest'))
185 action_runner.ExecuteJavaScript('valueSettableByTest = 3;')
186 action_runner.TapElement(
187 element_function='document.body.firstElementChild')
188 self.assertEqual(3, action_runner.EvaluateJavaScript('valueToTest'))
191 action_runner.TapElement('#notfound')
192 self.assertRaises(exceptions.EvaluateException, WillFail)
194 def testScroll(self):
195 if not page_action.IsGestureSourceTypeSupported(
199 self.Navigate('page_with_swipeables.html')
200 action_runner = action_runner_module.ActionRunner(self._tab)
202 action_runner.ScrollElement(
203 selector='#left-right', direction='right', left_start_ratio=0.9)
204 self.assertTrue(action_runner.EvaluateJavaScript(
205 'document.querySelector("#left-right").scrollLeft') > 75)
206 action_runner.ScrollElement(
207 selector='#top-bottom', direction='down', top_start_ratio=0.9)
208 self.assertTrue(action_runner.EvaluateJavaScript(
209 'document.querySelector("#top-bottom").scrollTop') > 75)
211 action_runner.ScrollPage(direction='right', left_start_ratio=0.9,
213 self.assertTrue(action_runner.EvaluateJavaScript(
214 'document.body.scrollLeft') > 75)
217 if not page_action.IsGestureSourceTypeSupported(
221 self.Navigate('page_with_swipeables.html')
222 action_runner = action_runner_module.ActionRunner(self._tab)
224 action_runner.SwipeElement(
225 selector='#left-right', direction='left', left_start_ratio=0.9)
226 self.assertTrue(action_runner.EvaluateJavaScript(
227 'document.querySelector("#left-right").scrollLeft') > 75)
228 action_runner.SwipeElement(
229 selector='#top-bottom', direction='up', top_start_ratio=0.9)
230 self.assertTrue(action_runner.EvaluateJavaScript(
231 'document.querySelector("#top-bottom").scrollTop') > 75)
233 action_runner.SwipePage(direction='left', left_start_ratio=0.9)
234 self.assertTrue(action_runner.EvaluateJavaScript(
235 'document.body.scrollLeft') > 75)