1 # Copyright (c) 2012 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.
5 import pyauto_functional
# must be imported before pyauto
8 class PyAutoEventsTest(pyauto
.PyUITest
):
9 """Tests using the event queue."""
11 def testBasicEvents(self
):
12 """Basic test for the event queue."""
13 url
= self
.GetHttpURLForDataPath('apptest', 'basic.html')
14 driver
= self
.NewWebDriver()
15 event_id
= self
.AddDomEventObserver(automation_id
=4444, recurring
=True)
16 success_id
= self
.AddDomEventObserver('test success', automation_id
=4444)
17 self
.NavigateToURL(url
)
18 self
._ExpectEvent
(event_id
, 'init')
19 self
._ExpectEvent
(event_id
, 'login ready')
20 driver
.find_element_by_id('login').click()
21 self
._ExpectEvent
(event_id
, 'login start')
22 self
._ExpectEvent
(event_id
, 'login done')
23 self
.GetNextEvent(success_id
)
25 def testDomMutationEvents(self
):
26 """Basic tests for WaitForDomNode."""
27 url
= self
.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
28 self
.NavigateToURL(url
)
29 self
.WaitForDomNode('id("login")', expected_value
='Log In')
30 self
.NewWebDriver().find_element_by_id('login').click()
31 self
.WaitForDomNode('id("console")', expected_value
='.*succeeded.*')
33 def testDomMutationGenericXPath(self
):
34 """Test mutation observers with a generic xpath and regexp."""
35 url
= self
.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
36 self
.NavigateToURL(url
)
37 self
.WaitForDomNode('//a', expected_value
='Log In')
38 self
.NewWebDriver().find_element_by_id('login').click()
39 self
.WaitForDomNode('//div', expected_value
='.*succeeded.*')
41 def testDomMutationObservers(self
):
42 """Tests for the various types of Dom Mutation observers."""
43 url
= self
.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
44 self
.NavigateToURL(url
)
45 self
.GetNextEvent(self
.AddDomMutationObserver('add', 'id("login")',
46 expected_value
='Log In'))
47 success_id
= self
.AddDomMutationObserver('change', 'id("console")',
48 expected_value
='.*succeeded.*')
49 self
.NewWebDriver().find_element_by_id('login').click()
50 self
.GetNextEvent(self
.AddDomMutationObserver('remove', 'id("fail")/a'))
51 self
.GetNextEvent(success_id
)
53 def testWaitUntilNavigationCompletes(self
):
54 """Basic test for WaitUntilNavigationCompletes."""
55 url
= self
.GetHttpURLForDataPath('apptest', 'dom_mutations.html')
56 js
= """window.onunload =
58 window.domAutomationController.send("done");
60 window.location.href = "%s";""" % url
61 self
.ExecuteJavascript(js
)
62 self
.WaitUntilNavigationCompletes()
63 self
.WaitUntilNavigationCompletes()
64 self
.WaitForDomNode('id("login")')
66 def _ExpectEvent(self
, event_id
, expected_event_name
):
67 """Checks that the next event is expected."""
68 e
= self
.GetNextEvent(event_id
)
69 self
.assertEqual(e
.get('name'), expected_event_name
,
70 msg
="unexpected event: %s" % e
)
73 if __name__
== '__main__':
74 pyauto_functional
.Main()