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.
4 from telemetry
.page
import page
as page_module
5 from telemetry
.page
import page_set
as page_set_module
7 class PolymerPage(page_module
.Page
):
9 def __init__(self
, url
, page_set
, run_no_page_interactions
):
10 """ Base class for all polymer pages.
13 run_no_page_interactions: whether the page will run any interactions after
16 super(PolymerPage
, self
).__init
__(
19 self
.script_to_evaluate_on_commit
= '''
20 document.addEventListener("polymer-ready", function() {
21 window.__polymer_ready = true;
24 self
._run
_no
_page
_interactions
= run_no_page_interactions
26 def RunPageInteractions(self
, action_runner
):
27 # If a polymer page wants to customize its actions, it should
28 # override the PerformPageInteractions method instead of this method.
29 if self
._run
_no
_page
_interactions
:
31 self
.PerformPageInteractions(action_runner
)
33 def PerformPageInteractions(self
, action_runner
):
34 """ Override this to perform actions after the page has navigated. """
37 def RunNavigateSteps(self
, action_runner
):
38 super(PolymerPage
, self
).RunNavigateSteps(action_runner
)
39 action_runner
.WaitForJavaScriptCondition(
40 'window.__polymer_ready')
43 class PolymerCalculatorPage(PolymerPage
):
45 def __init__(self
, page_set
, run_no_page_interactions
):
46 super(PolymerCalculatorPage
, self
).__init
__(
47 url
=('http://www.polymer-project.org/components/paper-calculator/'
49 page_set
=page_set
, run_no_page_interactions
=run_no_page_interactions
)
51 def PerformPageInteractions(self
, action_runner
):
52 self
.TapButton(action_runner
)
53 self
.SlidePanel(action_runner
)
55 def TapButton(self
, action_runner
):
56 interaction
= action_runner
.BeginInteraction(
58 action_runner
.TapElement(element_function
='''
59 document.querySelector(
60 'body /deep/ #outerPanels'
63 ).shadowRoot.querySelector(
64 'paper-calculator-key[label="5"]'
69 def SlidePanel(self
, action_runner
):
70 # only bother with this interaction if the drawer is hidden
71 opened
= action_runner
.EvaluateJavaScript('''
73 var outer = document.querySelector("body /deep/ #outerPanels");
74 return outer.opened || outer.wideMode;
77 interaction
= action_runner
.BeginInteraction(
79 action_runner
.SwipeElement(
80 left_start_ratio
=0.1, top_start_ratio
=0.2,
81 direction
='left', distance
=300, speed_in_pixels_per_second
=5000,
83 document.querySelector(
84 'body /deep/ #outerPanels'
87 ).shadowRoot.querySelector(
90 action_runner
.WaitForJavaScriptCondition('''
91 var outer = document.querySelector("body /deep/ #outerPanels");
92 outer.opened || outer.wideMode;''')
96 class PolymerShadowPage(PolymerPage
):
98 def __init__(self
, page_set
, run_no_page_interactions
):
99 super(PolymerShadowPage
, self
).__init
__(
100 url
='http://www.polymer-project.org/components/paper-shadow/demo.html',
101 page_set
=page_set
, run_no_page_interactions
=run_no_page_interactions
)
103 def PerformPageInteractions(self
, action_runner
):
104 action_runner
.ExecuteJavaScript(
105 "document.getElementById('fab').scrollIntoView()")
106 action_runner
.Wait(5)
107 self
.AnimateShadow(action_runner
, 'card')
108 #FIXME(wiltzius) disabling until this issue is fixed:
109 # https://github.com/Polymer/paper-shadow/issues/12
110 #self.AnimateShadow(action_runner, 'fab')
112 def AnimateShadow(self
, action_runner
, eid
):
113 for i
in range(1, 6):
114 action_runner
.ExecuteJavaScript(
115 'document.getElementById("{0}").z = {1}'.format(eid
, i
))
116 action_runner
.Wait(1)
119 class PolymerSampler(PolymerPage
):
121 def __init__(self
, page_set
, anchor
, run_no_page_interactions
,
122 scrolling_page
=False):
123 """Page exercising interactions with a single Paper Sampler subpage.
126 page_set: Page set to inforporate this page into.
127 anchor: string indicating which subpage to load (matches the element
128 type that page is displaying)
129 scrolling_page: Whether scrolling the content pane is relevant to this
132 super(PolymerSampler
, self
).__init
__(
133 url
=('http://www.polymer-project.org/components/%s/demo.html' % anchor
),
134 page_set
=page_set
, run_no_page_interactions
=run_no_page_interactions
)
135 self
.scrolling_page
= scrolling_page
136 self
.iframe_js
= 'document'
138 def RunNavigateSteps(self
, action_runner
):
139 super(PolymerSampler
, self
).RunNavigateSteps(action_runner
)
141 window.Polymer.whenPolymerReady(function() {
142 %s.contentWindow.Polymer.whenPolymerReady(function() {
143 window.__polymer_ready = true;
147 action_runner
.ExecuteJavaScript(waitForLoadJS
)
148 action_runner
.WaitForJavaScriptCondition(
149 'window.__polymer_ready')
151 def PerformPageInteractions(self
, action_runner
):
152 #TODO(wiltzius) Add interactions for input elements and shadow pages
153 if self
.scrolling_page
:
154 # Only bother scrolling the page if its been marked as worthwhile
155 self
.ScrollContentPane(action_runner
)
156 self
.TouchEverything(action_runner
)
158 def ScrollContentPane(self
, action_runner
):
159 element_function
= (self
.iframe_js
+ '.querySelector('
160 '"core-scroll-header-panel").$.mainContainer')
161 interaction
= action_runner
.BeginInteraction('Scroll_Page')
162 action_runner
.ScrollElement(use_touch
=True,
165 element_function
=element_function
)
167 interaction
= action_runner
.BeginInteraction('Scroll_Page')
168 action_runner
.ScrollElement(use_touch
=True,
171 element_function
=element_function
)
174 def TouchEverything(self
, action_runner
):
181 # 'paper-radio-button',
183 'paper-toggle-button',
186 for tappable_type
in tappable_types
:
187 self
.DoActionOnWidgetType(action_runner
, tappable_type
, self
.TapWidget
)
188 swipeable_types
= ['paper-slider']
189 for swipeable_type
in swipeable_types
:
190 self
.DoActionOnWidgetType(action_runner
, swipeable_type
, self
.SwipeWidget
)
192 def DoActionOnWidgetType(self
, action_runner
, widget_type
, action_function
):
193 # Find all widgets of this type, but skip any that are disabled or are
194 # currently active as they typically don't produce animation frames.
195 element_list_query
= (self
.iframe_js
+
196 ('.querySelectorAll("body %s:not([disabled]):'
197 'not([active])")' % widget_type
))
198 roles_count_query
= element_list_query
+ '.length'
199 for i
in range(action_runner
.EvaluateJavaScript(roles_count_query
)):
200 element_query
= element_list_query
+ ("[%d]" % i
)
201 if action_runner
.EvaluateJavaScript(
202 element_query
+ '.offsetParent != null'):
203 # Only try to tap on visible elements (offsetParent != null)
204 action_runner
.ExecuteJavaScript(element_query
+ '.scrollIntoView()')
205 action_runner
.Wait(1) # wait for page to settle after scrolling
206 action_function(action_runner
, element_query
)
208 def TapWidget(self
, action_runner
, element_function
):
209 interaction
= action_runner
.BeginInteraction(
211 action_runner
.TapElement(element_function
=element_function
)
212 action_runner
.Wait(1) # wait for e.g. animations on the widget
215 def SwipeWidget(self
, action_runner
, element_function
):
216 interaction
= action_runner
.BeginInteraction(
218 action_runner
.SwipeElement(element_function
=element_function
,
219 left_start_ratio
=0.75,
220 speed_in_pixels_per_second
=300)
224 class PolymerPageSet(page_set_module
.PageSet
):
226 def __init__(self
, run_no_page_interactions
=False):
227 super(PolymerPageSet
, self
).__init
__(
228 user_agent_type
='mobile',
229 archive_data_file
='data/polymer.json',
230 bucket
=page_set_module
.PUBLIC_BUCKET
)
232 self
.AddUserStory(PolymerCalculatorPage(self
, run_no_page_interactions
))
233 self
.AddUserStory(PolymerShadowPage(self
, run_no_page_interactions
))
235 # Polymer Sampler subpages that are interesting to tap / swipe elements on
242 # 'paper-radio-button',
243 #FIXME(wiltzius) Disabling x-shadow until this issue is fixed:
244 # https://github.com/Polymer/paper-shadow/issues/12
247 'paper-toggle-button',
249 for p
in TAPPABLE_PAGES
:
250 self
.AddUserStory(PolymerSampler(
251 self
, p
, run_no_page_interactions
=run_no_page_interactions
))
253 # Polymer Sampler subpages that are interesting to scroll
255 'core-scroll-header-panel',
257 for p
in SCROLLABLE_PAGES
:
258 self
.AddUserStory(PolymerSampler(
259 self
, p
, run_no_page_interactions
=run_no_page_interactions
,
260 scrolling_page
=True))
263 assert (page
.__class
__.RunPageInteractions
==
264 PolymerPage
.RunPageInteractions
), (
265 'Pages in this page set must not override PolymerPage\' '
266 'RunPageInteractions method.')