summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/execute_javascript.py
blob: 212ff4492b06aacb7748f06daaff3a25d9009bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import sys
import unittest

import pyauto_functional
from pyauto import PyUITest


class ExecuteJavascriptTest(PyUITest):
  def _GetExtensionInfoById(self, extensions, id):
    for x in extensions:
      if x['id'] == id:
        return x
    return None

  def testExecuteJavascript(self):
    self.NavigateToURL(self.GetFileURLForDataPath(
        'frame_dom_access', 'frame_dom_access.html'))

    v = self.ExecuteJavascript('window.domAutomationController.send(' +
                               'document.getElementById("myinput").nodeName)')
    self.assertEqual(v, 'INPUT')

  def testGetDOMValue(self):
    self.NavigateToURL(self.GetFileURLForDataPath(
        'frame_dom_access', 'frame_dom_access.html'))

    v = self.GetDOMValue('document.getElementById("myinput").nodeName')
    self.assertEqual(v, 'INPUT')

  def testExecuteJavascriptInExtension(self):
    """Test we can inject JavaScript into an extension."""
    dir_path = os.path.abspath(
        os.path.join(self.DataDir(), 'extensions', 'js_injection_background'))
    ext_id = self.InstallExtension(dir_path)

    # Verify extension is enabled.
    extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id)
    self.assertTrue(extension['is_enabled'],
                    msg='Extension was disabled by default')

    # Get the background page's view.
    background_view = self.WaitUntilExtensionViewLoaded(
        view_type='EXTENSION_BACKGROUND_PAGE')
    self.assertTrue(background_view,
                    msg='problematic background view: views = %s.' %
                    self.GetBrowserInfo()['extension_views'])

    # Get values from background page's DOM
    v = self.ExecuteJavascriptInRenderView(
        'window.domAutomationController.send('
        'document.getElementById("myinput").nodeName)', background_view)
    self.assertEqual(v, 'INPUT',
                     msg='Incorrect value returned (v = %s).' % v)
    v = self.ExecuteJavascriptInRenderView(
        'window.domAutomationController.send(bool_var)', background_view)
    self.assertEqual(v, True, msg='Incorrect value returned (v = %s).' % v)
    v = self.ExecuteJavascriptInRenderView(
        'window.domAutomationController.send(int_var)', background_view)
    self.assertEqual(v, 42, msg='Incorrect value returned (v = %s).' % v)
    v = self.ExecuteJavascriptInRenderView(
        'window.domAutomationController.send(str_var)', background_view)
    self.assertEqual(v, 'foo', msg='Incorrect value returned (v = %s).' % v)


if __name__ == '__main__':
  pyauto_functional.Main()