Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: telemetry/telemetry/internal/actions/scroll_to_element.py

Issue 2559503002: [Telemetry] Fix JavaScript interpolation in telemetry actions (Closed)
Patch Set: inline js literals Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 from telemetry.internal.actions import page_action 5 from telemetry.internal.actions import page_action
6 from telemetry.internal.actions.scroll import ScrollAction 6 from telemetry.internal.actions.scroll import ScrollAction
7 from telemetry.util import js_template
7 8
8 9
9 class ScrollToElementAction(page_action.PageAction): 10 class ScrollToElementAction(page_action.PageAction):
10 11
11 12
12 def __init__(self, selector=None, element_function=None, 13 def __init__(self, selector=None, element_function=None,
13 speed_in_pixels_per_second=800): 14 speed_in_pixels_per_second=800):
14 """ 15 """
15 Args: 16 Args:
16 selector: Css selector to find element with. 17 selector: Css selector to find element with.
17 element_function: js string that evaluates to an element. 18 element_function: js string that evaluates to an element.
18 speed_in_pixels_per_second: Speed in pixels per second to scroll. 19 speed_in_pixels_per_second: Speed in pixels per second to scroll.
19 """ 20 """
20 super(ScrollToElementAction, self).__init__() 21 super(ScrollToElementAction, self).__init__()
21 self._selector = selector 22 self._selector = selector
22 self._element_function = element_function 23 self._element_function = element_function
23 self._speed = speed_in_pixels_per_second 24 self._speed = speed_in_pixels_per_second
24 self._distance = None 25 self._distance = None
25 self._direction = None 26 self._direction = None
26 self._scroller = None 27 self._scroller = None
27 assert (self._selector or self._element_function), ( 28 assert (self._selector or self._element_function), (
28 'Must have either selector or element function') 29 'Must have either selector or element function')
29 30
30 def WillRunAction(self, tab): 31 def WillRunAction(self, tab):
31 if self._selector: 32 if self._selector:
32 # TODO(catapult:#3028): Fix interpolation of JavaScript values. 33 element = js_template.Render(
33 element = 'document.querySelector("%s")' % self._selector 34 'document.querySelector({{ selector }})', selector=self._selector)
34 else: 35 else:
35 element = self._element_function 36 element = self._element_function
36 37
37 # TODO(catapult:#3028): Fix interpolation of JavaScript values. 38 # TODO(catapult:#3028): Render in JavaScript method when supported by API.
38 get_distance_js = ''' 39 get_distance_js = js_template.Render('''
39 (function(elem){ 40 (function(elem){
40 var rect = elem.getBoundingClientRect(); 41 var rect = elem.getBoundingClientRect();
41 if (rect.bottom < 0) { 42 if (rect.bottom < 0) {
42 // The bottom of the element is above the viewport. 43 // The bottom of the element is above the viewport.
43 // Scroll up until the top of the element is on screen. 44 // Scroll up until the top of the element is on screen.
44 return rect.top - (window.innerHeight / 2); 45 return rect.top - (window.innerHeight / 2);
45 } 46 }
46 if (rect.top - window.innerHeight >= 0) { 47 if (rect.top - window.innerHeight >= 0) {
47 // rect.top provides the pixel offset of the element from the 48 // rect.top provides the pixel offset of the element from the
48 // top of the page. Because that exceeds the viewport's height, 49 // top of the page. Because that exceeds the viewport's height,
49 // we know that the element is below the viewport. 50 // we know that the element is below the viewport.
50 return rect.top - (window.innerHeight / 2); 51 return rect.top - (window.innerHeight / 2);
51 } 52 }
52 return 0; 53 return 0;
53 })(%s); 54 })({{ @element }});
54 ''' % element 55 ''', element=element)
55 56
56 self._distance = tab.EvaluateJavaScript(get_distance_js) 57 self._distance = tab.EvaluateJavaScript(get_distance_js)
57 self._direction = 'down' if self._distance > 0 else 'up' 58 self._direction = 'down' if self._distance > 0 else 'up'
58 self._distance = abs(self._distance) 59 self._distance = abs(self._distance)
59 self._scroller = ScrollAction(direction=self._direction, 60 self._scroller = ScrollAction(direction=self._direction,
60 distance=self._distance, 61 distance=self._distance,
61 speed_in_pixels_per_second=self._speed) 62 speed_in_pixels_per_second=self._speed)
62 63
63 def RunAction(self, tab): 64 def RunAction(self, tab):
64 if self._distance == 0: # Element is already in view. 65 if self._distance == 0: # Element is already in view.
65 return 66 return
66 self._scroller.WillRunAction(tab) 67 self._scroller.WillRunAction(tab)
67 self._scroller.RunAction(tab) 68 self._scroller.RunAction(tab)
OLDNEW
« no previous file with comments | « telemetry/telemetry/internal/actions/scroll_bounce.py ('k') | telemetry/telemetry/internal/actions/seek.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698