summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/apptest.py
blob: 8a6ef839cea182e1ca8fa55eef52824fdd6998d8 (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
73
74
# Copyright (c) 2012 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 pyauto_functional  # must be imported before pyauto
import pyauto

class PyAutoEventsTest(pyauto.PyUITest):
  """Tests using the event queue."""

  def testBasicEvents(self):
    """Basic test for the event queue."""
    url = self.GetHttpURLForDataPath('apptest', 'basic.html')
    driver = self.NewWebDriver()
    event_id = self.AddDomEventObserver(automation_id=4444, recurring=True)
    success_id = self.AddDomEventObserver('test success', automation_id=4444)
    self.NavigateToURL(url)
    self._ExpectEvent(event_id, 'init')
    self._ExpectEvent(event_id, 'login ready')
    driver.find_element_by_id('login').click()
    self._ExpectEvent(event_id, 'login start')
    self._ExpectEvent(event_id, 'login done')
    self.GetNextEvent(success_id)

  def testDomMutationEvents(self):
    """Basic tests for WaitForDomNode."""
    url = self.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
    self.NavigateToURL(url)
    self.WaitForDomNode('id("login")', expected_value='Log In')
    self.NewWebDriver().find_element_by_id('login').click()
    self.WaitForDomNode('id("console")', expected_value='.*succeeded.*')

  def testDomMutationGenericXPath(self):
    """Test mutation observers with a generic xpath and regexp."""
    url = self.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
    self.NavigateToURL(url)
    self.WaitForDomNode('//a', expected_value='Log In')
    self.NewWebDriver().find_element_by_id('login').click()
    self.WaitForDomNode('//div', expected_value='.*succeeded.*')

  def testDomMutationObservers(self):
    """Tests for the various types of Dom Mutation observers."""
    url = self.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
    self.NavigateToURL(url)
    self.GetNextEvent(self.AddDomMutationObserver('add', 'id("login")',
                                                  expected_value='Log In'))
    success_id = self.AddDomMutationObserver('change', 'id("console")',
                                             expected_value='.*succeeded.*')
    self.NewWebDriver().find_element_by_id('login').click()
    self.GetNextEvent(self.AddDomMutationObserver('remove', 'id("fail")/a'))
    self.GetNextEvent(success_id)

  def testWaitUntilNavigationCompletes(self):
    """Basic test for WaitUntilNavigationCompletes."""
    url = self.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
    js = """window.onunload =
                function() {
                  window.domAutomationController.send("done");
                };
            window.location.href = "%s";""" % url
    self.ExecuteJavascript(js)
    self.WaitUntilNavigationCompletes()
    self.WaitUntilNavigationCompletes()
    self.WaitForDomNode('id("login")')

  def _ExpectEvent(self, event_id, expected_event_name):
    """Checks that the next event is expected."""
    e = self.GetNextEvent(event_id)
    self.assertEqual(e.get('name'), expected_event_name,
                     msg="unexpected event: %s" % e)


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