15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#!/usr/bin/env python 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Copyright (c) 2012 The Chromium Authors. All rights reserved. 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# found in the LICENSE file. 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import time 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import unittest 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import pyauto_functional # Must be imported before pyauto 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import pyauto 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import pyauto_errors 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PyAutoTest(pyauto.PyUITest): 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """Test functionality of the PyAuto framework.""" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) _EXTRA_CHROME_FLAGS = [ 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) '--scooby-doo=123', 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) '--donald-duck=cool', 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) '--super-mario', 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) '--marvin-the-martian', 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ] 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) def ExtraChromeFlags(self): 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """Ensures Chrome is launched with some custom flags. 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Overrides the default list of extra flags passed to Chrome. See 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ExtraChromeFlags() in pyauto.py. 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """ 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return pyauto.PyUITest.ExtraChromeFlags(self) + self._EXTRA_CHROME_FLAGS 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) def testSetCustomChromeFlags(self): 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """Ensures that Chrome can be launched with custom flags.""" 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.NavigateToURL('about://version') 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for flag in self._EXTRA_CHROME_FLAGS: 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertEqual(self.FindInPage(flag)['match_count'], 1, 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) msg='Missing expected Chrome flag "%s"' % flag) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) def testCallOnInvalidWindow(self): 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """Verify that exception is raised when a browser is missing/invalid.""" 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertEqual(1, self.GetBrowserWindowCount()) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertRaises( 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pyauto_errors.JSONInterfaceError, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) lambda: self.FindInPage('some text', windex=1)) # invalid window 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) def testJSONInterfaceTimeout(self): 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """Verify that an exception is raised when the JSON interface times out.""" 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.ClearEventQueue() 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.AddDomEventObserver('foo') 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertRaises( 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pyauto_errors.AutomationCommandTimeout, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) lambda: self.GetNextEvent(timeout=2000)) # event queue is empty 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) def testActionTimeoutChanger(self): 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) """Verify that ActionTimeoutChanger works.""" 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new_timeout = 1000 # 1 sec 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) changer = pyauto.PyUITest.ActionTimeoutChanger(self, new_timeout) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertEqual(self._automation_timeout, new_timeout) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) # Verify the amount of time taken for automation timeout 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) then = time.time() 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertRaises( 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pyauto_errors.AutomationCommandTimeout, 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) lambda: self.ExecuteJavascript('invalid js should timeout')) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) elapsed = time.time() - then 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) self.assertTrue(elapsed < new_timeout / 1000.0 + 2, # margin of 2 secs 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) msg='ActionTimeoutChanger did not work. ' 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 'Automation timeout took %f secs' % elapsed) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if __name__ == '__main__': 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pyauto_functional.Main() 73