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.
4
5from telemetry.internal.actions import page_action
6from telemetry.internal.actions import utils
7
8
9class MouseClickAction(page_action.PageAction):
10  def __init__(self, selector=None):
11    super(MouseClickAction, self).__init__()
12    self._selector = selector
13
14  def WillRunAction(self, tab):
15    """Load the mouse click JS code prior to running the action."""
16    super(MouseClickAction, self).WillRunAction(tab)
17    utils.InjectJavaScript(tab, 'mouse_click.js')
18    tab.ExecuteJavaScript("""
19        window.__mouseClickActionDone = false;
20        window.__mouseClickAction = new __MouseClickAction(function() {
21          window.__mouseClickActionDone = true;
22        });""")
23
24  def RunAction(self, tab):
25    code = '''
26        function(element, info) {
27          if (!element) {
28            throw Error('Cannot find element: ' + info);
29          }
30          window.__mouseClickAction.start({
31            element: element
32          });
33        }'''
34    page_action.EvaluateCallbackWithElement(
35        tab, code, selector=self._selector)
36    tab.WaitForJavaScriptCondition('window.__mouseClickActionDone', timeout=60)
37