1# Copyright 2015 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. 4import os 5 6from telemetry.internal.actions import page_action 7 8 9def read_js(): 10 with open(os.path.join(os.path.dirname(__file__), 'mouse_click.js')) as f: 11 return f.read() 12 13 14class MouseClickAction(page_action.PageAction): 15 _MOUSE_CLICK_JAVASCRIPT = read_js() 16 17 def __init__(self, selector=None): 18 super(MouseClickAction, self).__init__() 19 self._selector = selector 20 21 def WillRunAction(self, tab): 22 """Load the mouse click JS code prior to running the action.""" 23 super(MouseClickAction, self).WillRunAction(tab) 24 tab.ExecuteJavaScript(MouseClickAction._MOUSE_CLICK_JAVASCRIPT) 25 done_callback = 'function() { window.__mouseClickActionDone = true; }' 26 tab.ExecuteJavaScript(""" 27 window.__mouseClickActionDone = false; 28 window.__mouseClickAction = new __MouseClickAction(%s);""" 29 % (done_callback)) 30 31 def RunAction(self, tab): 32 code = ''' 33 function(element, info) { 34 if (!element) { 35 throw Error('Cannot find element: ' + info); 36 } 37 window.__mouseClickAction.start({ 38 element: element 39 }); 40 }''' 41 page_action.EvaluateCallbackWithElement( 42 tab, code, selector=self._selector) 43 tab.WaitForJavaScriptExpression('window.__mouseClickActionDone', 60) 44