OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """PyAuto: Python Interface to Chromium's Automation Proxy. | 6 """PyAuto: Python Interface to Chromium's Automation Proxy. |
7 | 7 |
8 PyAuto uses swig to expose Automation Proxy interfaces to Python. | 8 PyAuto uses swig to expose Automation Proxy interfaces to Python. |
9 For complete documentation on the functionality available, | 9 For complete documentation on the functionality available, |
10 run pydoc on this file. | 10 run pydoc on this file. |
11 | 11 |
12 Ref: http://dev.chromium.org/developers/testing/pyauto | 12 Ref: http://dev.chromium.org/developers/testing/pyauto |
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 """Terminate the given pid. | 753 """Terminate the given pid. |
754 | 754 |
755 If the pid refers to a renderer, use KillRendererProcess instead. | 755 If the pid refers to a renderer, use KillRendererProcess instead. |
756 """ | 756 """ |
757 if PyUITest.IsWin(): | 757 if PyUITest.IsWin(): |
758 subprocess.call(['taskkill.exe', '/T', '/F', '/PID', str(pid)]) | 758 subprocess.call(['taskkill.exe', '/T', '/F', '/PID', str(pid)]) |
759 else: | 759 else: |
760 os.kill(pid, signal.SIGTERM) | 760 os.kill(pid, signal.SIGTERM) |
761 | 761 |
762 @staticmethod | 762 @staticmethod |
763 def ChromeFlagsForSyncTestServer(port, xmpp_port): | |
764 """Creates the flags list for the browser to connect to the sync server. | |
765 | |
766 Use the |ExtraBrowser| class to launch a new browser with these flags. | |
767 | |
768 Args: | |
769 port: The HTTP port number. | |
770 xmpp_port: The XMPP port number. | |
771 | |
772 Returns: | |
773 A list with the flags. | |
774 """ | |
775 return [ | |
776 '--sync-url=http://127.0.0.1:%s/chromiumsync' % port, | |
777 '--sync-allow-insecure-xmpp-connection', | |
778 '--sync-notification-host-port=127.0.0.1:%s' % xmpp_port, | |
779 '--sync-notification-method=p2p', | |
780 ] | |
781 | |
782 @staticmethod | |
783 def GetPrivateInfo(): | 763 def GetPrivateInfo(): |
784 """Fetch info from private_tests_info.txt in private dir. | 764 """Fetch info from private_tests_info.txt in private dir. |
785 | 765 |
786 Returns: | 766 Returns: |
787 a dictionary of items from private_tests_info.txt | 767 a dictionary of items from private_tests_info.txt |
788 """ | 768 """ |
789 private_file = os.path.join( | 769 private_file = os.path.join( |
790 PyUITest.DataDir(), 'pyauto_private', 'private_tests_info.txt') | 770 PyUITest.DataDir(), 'pyauto_private', 'private_tests_info.txt') |
791 assert os.path.exists(private_file), '%s missing' % private_file | 771 assert os.path.exists(private_file), '%s missing' % private_file |
792 return PyUITest.EvalDataFrom(private_file) | 772 return PyUITest.EvalDataFrom(private_file) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 logging.debug('WaitUntil(%s:%d %s) still waiting. ' | 835 logging.debug('WaitUntil(%s:%d %s) still waiting. ' |
856 'Expecting %s. Last returned %s.', | 836 'Expecting %s. Last returned %s.', |
857 os.path.basename(inspect.getsourcefile(function)), | 837 os.path.basename(inspect.getsourcefile(function)), |
858 inspect.getsourcelines(function)[1], | 838 inspect.getsourcelines(function)[1], |
859 function_info, | 839 function_info, |
860 True if expect_retval is None else expect_retval, | 840 True if expect_retval is None else expect_retval, |
861 retval) | 841 retval) |
862 time.sleep(retry_sleep) | 842 time.sleep(retry_sleep) |
863 return retval if return_retval else False | 843 return retval if return_retval else False |
864 | 844 |
865 def StartSyncServer(self): | |
866 """Start a local sync server. | |
867 | |
868 Adds a dictionary attribute 'ports' in returned object. | |
869 | |
870 Returns: | |
871 A handle to Sync Server, an instance of TestServer | |
872 """ | |
873 sync_server = pyautolib.TestServer(pyautolib.TestServer.TYPE_SYNC, | |
874 '127.0.0.1', | |
875 pyautolib.FilePath('')) | |
876 assert sync_server.Start(), 'Could not start sync server' | |
877 sync_server.ports = dict(port=sync_server.GetPort(), | |
878 xmpp_port=sync_server.GetSyncXmppPort()) | |
879 logging.debug('Started sync server at ports %s.', sync_server.ports) | |
880 return sync_server | |
881 | |
882 def StopSyncServer(self, sync_server): | |
883 """Stop the local sync server.""" | |
884 assert sync_server, 'Sync Server not yet started' | |
885 assert sync_server.Stop(), 'Could not stop sync server' | |
886 logging.debug('Stopped sync server at ports %s.', sync_server.ports) | |
887 | |
888 def StartFTPServer(self, data_dir): | 845 def StartFTPServer(self, data_dir): |
889 """Start a local file server hosting data files over ftp:// | 846 """Start a local file server hosting data files over ftp:// |
890 | 847 |
891 Args: | 848 Args: |
892 data_dir: path where ftp files should be served | 849 data_dir: path where ftp files should be served |
893 | 850 |
894 Returns: | 851 Returns: |
895 handle to FTP Server, an instance of TestServer | 852 handle to FTP Server, an instance of TestServer |
896 """ | 853 """ |
897 ftp_server = pyautolib.TestServer(pyautolib.TestServer.TYPE_FTP, | 854 ftp_server = pyautolib.TestServer(pyautolib.TestServer.TYPE_FTP, |
(...skipping 2792 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3690 windex: index of the window. | 3647 windex: index of the window. |
3691 | 3648 |
3692 Returns: | 3649 Returns: |
3693 a string that was sent back via the domAutomationController.send method | 3650 a string that was sent back via the domAutomationController.send method |
3694 """ | 3651 """ |
3695 converted_args = map(lambda arg: json.dumps(arg), args) | 3652 converted_args = map(lambda arg: json.dumps(arg), args) |
3696 js = '%s(%s)' % (function, ', '.join(converted_args)) | 3653 js = '%s(%s)' % (function, ', '.join(converted_args)) |
3697 logging.debug('Executing javascript: %s', js) | 3654 logging.debug('Executing javascript: %s', js) |
3698 return self.ExecuteJavascript(js, tab_index, windex) | 3655 return self.ExecuteJavascript(js, tab_index, windex) |
3699 | 3656 |
3700 def SignInToSync(self, username, password): | |
3701 """Signs in to sync using the given username and password. | |
3702 | |
3703 Args: | |
3704 username: The account with which to sign in. Example: "user@gmail.com". | |
3705 password: Password for the above account. Example: "pa$$w0rd". | |
3706 | |
3707 Returns: | |
3708 True, on success. | |
3709 | |
3710 Raises: | |
3711 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
3712 """ | |
3713 cmd_dict = { | |
3714 'command': 'SignInToSync', | |
3715 'username': username, | |
3716 'password': password, | |
3717 } | |
3718 return self._GetResultFromJSONRequest(cmd_dict)['success'] | |
3719 | |
3720 def GetSyncInfo(self): | |
3721 """Returns info about sync. | |
3722 | |
3723 Returns: | |
3724 A dictionary of info about sync. | |
3725 Example dictionaries: | |
3726 {u'summary': u'SYNC DISABLED'} | |
3727 | |
3728 { u'authenticated': True, | |
3729 u'last synced': u'Just now', | |
3730 u'summary': u'READY', | |
3731 u'sync url': u'clients4.google.com', | |
3732 u'updates received': 42, | |
3733 u'synced datatypes': [ u'Bookmarks', | |
3734 u'Preferences', | |
3735 u'Passwords', | |
3736 u'Autofill', | |
3737 u'Themes', | |
3738 u'Extensions', | |
3739 u'Apps']} | |
3740 | |
3741 Raises: | |
3742 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
3743 """ | |
3744 cmd_dict = { | |
3745 'command': 'GetSyncInfo', | |
3746 } | |
3747 return self._GetResultFromJSONRequest(cmd_dict)['sync_info'] | |
3748 | |
3749 def AwaitSyncCycleCompletion(self): | |
3750 """Waits for the ongoing sync cycle to complete. Must be signed in to sync | |
3751 before calling this method. | |
3752 | |
3753 Returns: | |
3754 True, on success. | |
3755 | |
3756 Raises: | |
3757 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
3758 """ | |
3759 cmd_dict = { | |
3760 'command': 'AwaitSyncCycleCompletion', | |
3761 } | |
3762 return self._GetResultFromJSONRequest(cmd_dict)['success'] | |
3763 | |
3764 def AwaitSyncRestart(self): | |
3765 """Waits for sync to reinitialize itself. Typically used when the browser | |
3766 is restarted and a full sync cycle is not expected to occur. Must be | |
3767 previously signed in to sync before calling this method. | |
3768 | |
3769 Returns: | |
3770 True, on success. | |
3771 | |
3772 Raises: | |
3773 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
3774 """ | |
3775 cmd_dict = { | |
3776 'command': 'AwaitSyncRestart', | |
3777 } | |
3778 return self._GetResultFromJSONRequest(cmd_dict)['success'] | |
3779 | |
3780 def EnableSyncForDatatypes(self, datatypes): | |
3781 """Enables sync for a given list of sync datatypes. Must be signed in to | |
3782 sync before calling this method. | |
3783 | |
3784 Args: | |
3785 datatypes: A list of strings indicating the datatypes for which to enable | |
3786 sync. Strings that can be in the list are: | |
3787 Bookmarks, Preferences, Passwords, Autofill, Themes, | |
3788 Typed URLs, Extensions, Encryption keys, Sessions, Apps, All. | |
3789 For an updated list of valid sync datatypes, refer to the | |
3790 function ModelTypeToString() in the file | |
3791 chrome/browser/sync/syncable/model_type.cc. | |
3792 Examples: | |
3793 ['Bookmarks', 'Preferences', 'Passwords'] | |
3794 ['All'] | |
3795 | |
3796 Returns: | |
3797 True, on success. | |
3798 | |
3799 Raises: | |
3800 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
3801 """ | |
3802 cmd_dict = { | |
3803 'command': 'EnableSyncForDatatypes', | |
3804 'datatypes': datatypes, | |
3805 } | |
3806 return self._GetResultFromJSONRequest(cmd_dict)['success'] | |
3807 | |
3808 def DisableSyncForDatatypes(self, datatypes): | |
3809 """Disables sync for a given list of sync datatypes. Must be signed in to | |
3810 sync before calling this method. | |
3811 | |
3812 Args: | |
3813 datatypes: A list of strings indicating the datatypes for which to | |
3814 disable sync. Strings that can be in the list are: | |
3815 Bookmarks, Preferences, Passwords, Autofill, Themes, | |
3816 Typed URLs, Extensions, Encryption keys, Sessions, Apps, All. | |
3817 For an updated list of valid sync datatypes, refer to the | |
3818 function ModelTypeToString() in the file | |
3819 chrome/browser/sync/syncable/model_type.cc. | |
3820 Examples: | |
3821 ['Bookmarks', 'Preferences', 'Passwords'] | |
3822 ['All'] | |
3823 | |
3824 Returns: | |
3825 True, on success. | |
3826 | |
3827 Raises: | |
3828 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
3829 """ | |
3830 cmd_dict = { | |
3831 'command': 'DisableSyncForDatatypes', | |
3832 'datatypes': datatypes, | |
3833 } | |
3834 return self._GetResultFromJSONRequest(cmd_dict)['success'] | |
3835 | |
3836 def HeapProfilerDump(self, process_type, reason, tab_index=0, windex=0): | 3657 def HeapProfilerDump(self, process_type, reason, tab_index=0, windex=0): |
3837 """Dumps a heap profile. It works only on Linux and ChromeOS. | 3658 """Dumps a heap profile. It works only on Linux and ChromeOS. |
3838 | 3659 |
3839 We need an environment variable "HEAPPROFILE" set to a directory and a | 3660 We need an environment variable "HEAPPROFILE" set to a directory and a |
3840 filename prefix, for example, "/tmp/prof". In a case of this example, | 3661 filename prefix, for example, "/tmp/prof". In a case of this example, |
3841 heap profiles will be dumped into "/tmp/prof.(pid).0002.heap", | 3662 heap profiles will be dumped into "/tmp/prof.(pid).0002.heap", |
3842 "/tmp/prof.(pid).0003.heap", and so on. Nothing happens when this | 3663 "/tmp/prof.(pid).0003.heap", and so on. Nothing happens when this |
3843 function is called without the env. | 3664 function is called without the env. |
3844 | 3665 |
3845 Args: | 3666 Args: |
(...skipping 2651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6497 successful = result.wasSuccessful() | 6318 successful = result.wasSuccessful() |
6498 if not successful: | 6319 if not successful: |
6499 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6320 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
6500 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6321 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
6501 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6322 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
6502 sys.exit(not successful) | 6323 sys.exit(not successful) |
6503 | 6324 |
6504 | 6325 |
6505 if __name__ == '__main__': | 6326 if __name__ == '__main__': |
6506 Main() | 6327 Main() |
OLD | NEW |