| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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. |
| (...skipping 2764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2775 Returns: | 2775 Returns: |
| 2776 an instance of history_info.HistoryInfo | 2776 an instance of history_info.HistoryInfo |
| 2777 """ | 2777 """ |
| 2778 cmd_dict = { # Prepare command for the json interface | 2778 cmd_dict = { # Prepare command for the json interface |
| 2779 'command': 'GetHistoryInfo', | 2779 'command': 'GetHistoryInfo', |
| 2780 'search_text': search_text, | 2780 'search_text': search_text, |
| 2781 } | 2781 } |
| 2782 return history_info.HistoryInfo( | 2782 return history_info.HistoryInfo( |
| 2783 self._GetResultFromJSONRequest(cmd_dict, windex=windex)) | 2783 self._GetResultFromJSONRequest(cmd_dict, windex=windex)) |
| 2784 | 2784 |
| 2785 def GetTranslateInfo(self, tab_index=0, window_index=0): |
| 2786 """Returns info about translate for the given page. |
| 2787 |
| 2788 If the translate bar is showing, also returns information about the bar. |
| 2789 |
| 2790 Args: |
| 2791 tab_index: The tab index, default is 0. |
| 2792 window_index: The window index, default is 0. |
| 2793 |
| 2794 Returns: |
| 2795 A dictionary of information about translate for the page. Example: |
| 2796 { u'always_translate_lang_button_showing': False, |
| 2797 u'never_translate_lang_button_showing': False, |
| 2798 u'can_translate_page': True, |
| 2799 u'original_language': u'es', |
| 2800 u'page_translated': False, |
| 2801 # The below will only appear if the translate bar is showing. |
| 2802 u'translate_bar': { u'bar_state': u'BEFORE_TRANSLATE', |
| 2803 u'original_lang_code': u'es', |
| 2804 u'target_lang_code': u'en'}} |
| 2805 """ |
| 2806 cmd_dict = { # Prepare command for the json interface |
| 2807 'command': 'GetTranslateInfo', |
| 2808 'tab_index': tab_index |
| 2809 } |
| 2810 return self._GetResultFromJSONRequest(cmd_dict, windex=window_index) |
| 2811 |
| 2812 def ClickTranslateBarTranslate(self, tab_index=0, window_index=0): |
| 2813 """If the translate bar is showing, clicks the 'Translate' button on the |
| 2814 bar. This will show the 'this page has been translated...' infobar. |
| 2815 |
| 2816 Args: |
| 2817 tab_index: The index of the tab, default is 0. |
| 2818 window_index: The index of the window, default is 0. |
| 2819 |
| 2820 Returns: |
| 2821 True if the translation was successful or false if there was an error. |
| 2822 Note that an error shouldn't neccessarily mean a failed test - retry the |
| 2823 call on error. |
| 2824 |
| 2825 Raises: |
| 2826 pyauto_errors.JSONInterfaceError if the automation returns an error. |
| 2827 """ |
| 2828 cmd_dict = { # Prepare command for the json interface |
| 2829 'command': 'SelectTranslateOption', |
| 2830 'tab_index': tab_index, |
| 2831 'option': 'translate_page' |
| 2832 } |
| 2833 return self._GetResultFromJSONRequest( |
| 2834 cmd_dict, windex=window_index)['translation_success'] |
| 2835 |
| 2836 def RevertPageTranslation(self, tab_index=0, window_index=0): |
| 2837 """Select the 'Show original' button on the 'this page has been |
| 2838 translated...' infobar. This will remove the infobar and revert the |
| 2839 page translation. |
| 2840 |
| 2841 Args: |
| 2842 tab_index: The index of the tab, default is 0. |
| 2843 window_index: The index of the window, default is 0. |
| 2844 """ |
| 2845 cmd_dict = { # Prepare command for the json interface |
| 2846 'command': 'SelectTranslateOption', |
| 2847 'tab_index': tab_index, |
| 2848 'option': 'revert_translation' |
| 2849 } |
| 2850 self._GetResultFromJSONRequest(cmd_dict, windex=window_index) |
| 2851 |
| 2852 def ChangeTranslateToLanguage(self, new_language, tab_index=0, |
| 2853 window_index=0): |
| 2854 """Set the target language to be a new language. |
| 2855 |
| 2856 This is equivalent to selecting a different language from the 'to' |
| 2857 drop-down menu on the translate bar. If the page was already translated |
| 2858 before calling this function, this will trigger a re-translate to the |
| 2859 new language. |
| 2860 |
| 2861 Args: |
| 2862 new_language: The new target language. The string should be equivalent |
| 2863 to the text seen in the translate bar options. |
| 2864 Example: 'English'. |
| 2865 tab_index: The tab index - default is 0. |
| 2866 window_index: The window index - default is 0. |
| 2867 |
| 2868 Returns: |
| 2869 False, if a new translation was triggered and the translation failed. |
| 2870 True on success. |
| 2871 """ |
| 2872 cmd_dict = { # Prepare command for the json interface |
| 2873 'command': 'SelectTranslateOption', |
| 2874 'tab_index': tab_index, |
| 2875 'option': 'set_target_language', |
| 2876 'target_language': new_language |
| 2877 } |
| 2878 return self._GetResultFromJSONRequest( |
| 2879 cmd_dict, windex=window_index)['translation_success'] |
| 2880 |
| 2881 def SelectTranslateOption(self, option, tab_index=0, window_index=0): |
| 2882 """Selects one of the options in the drop-down menu for the translate bar. |
| 2883 |
| 2884 Args: |
| 2885 option: One of 'never_translate_language', 'never_translate_site', or |
| 2886 'toggle_always_translate'. See notes on each below. |
| 2887 tab_index: The index of the tab, default is 0. |
| 2888 window_index: The index of the window, default is 0. |
| 2889 |
| 2890 *Notes* |
| 2891 never_translate_language: Selecting this means that no sites in this |
| 2892 language will be translated. This dismisses the infobar. |
| 2893 never_translate_site: Selecting this means that this site will never be |
| 2894 translated, regardless of the language. This dismisses the infobar. |
| 2895 toggle_always_translate: This does not dismiss the infobar or translate the |
| 2896 page. See ClickTranslateBarTranslate and PerformActioOnInfobar to do |
| 2897 those. If a language is selected to be always translated, then whenver |
| 2898 the user visits a page with that language, the infobar will show the |
| 2899 'This page has been translated...' message. |
| 2900 decline_translation: Equivalent to selecting 'Nope' on the translate bar. |
| 2901 click_never_translate_lang_button: This button appears when the user has |
| 2902 declined translation of this language several times. Selecting it causes |
| 2903 the language to never be translated. Look at GetTranslateInfo to |
| 2904 determine if the button is showing. |
| 2905 click_always_translate_lang_button: This button appears when the user has |
| 2906 accepted translation of this language several times. Selecting it causes |
| 2907 the language to always be translated. Look at GetTranslateInfo to |
| 2908 determine if the button is showing. |
| 2909 |
| 2910 Raises: |
| 2911 pyauto_errors.JSONInterfaceError if the automation returns an error. |
| 2912 """ |
| 2913 cmd_dict = { # Prepare command for the json interface |
| 2914 'command': 'SelectTranslateOption', |
| 2915 'option': option, |
| 2916 'tab_index': tab_index |
| 2917 } |
| 2918 self._GetResultFromJSONRequest(cmd_dict, windex=window_index) |
| 2919 |
| 2920 def WaitUntilTranslateComplete(self, tab_index=0, window_index=0): |
| 2921 """Waits until an attempted translation has finished. |
| 2922 |
| 2923 This should be called after navigating to a page that should be translated |
| 2924 automatically (because the language always-translate is on). It does not |
| 2925 need to be called after 'ClickTranslateBarTranslate'. |
| 2926 |
| 2927 Do not call this function if you are not expecting a page translation - it |
| 2928 will hang. If you call it when there is no translate bar, it will return |
| 2929 False. |
| 2930 |
| 2931 Args: |
| 2932 tab_index: The tab index, default is 0. |
| 2933 window_index: The window index, default is 0. |
| 2934 |
| 2935 Returns: |
| 2936 True if the translation was successful, False if there was an error. |
| 2937 """ |
| 2938 cmd_dict = { # Prepare command for the json interface |
| 2939 'command': 'WaitUntilTranslateComplete', |
| 2940 'tab_index': tab_index |
| 2941 } |
| 2942 # TODO(phajdan.jr): We need a solid automation infrastructure to handle |
| 2943 # these cases. See crbug.com/53647. |
| 2944 return self.WaitUntil( |
| 2945 lambda tab_index, window_index: self.GetTranslateInfo( |
| 2946 tab_index=tab_index, window_index=window_index)['page_translated'], |
| 2947 args=[tab_index, window_index]) |
| 2948 |
| 2785 def InstallExtension(self, extension_path, with_ui=False, from_webstore=None, | 2949 def InstallExtension(self, extension_path, with_ui=False, from_webstore=None, |
| 2786 windex=0): | 2950 windex=0): |
| 2787 """Installs an extension from the given path. | 2951 """Installs an extension from the given path. |
| 2788 | 2952 |
| 2789 The path must be absolute and may be a crx file or an unpacked extension | 2953 The path must be absolute and may be a crx file or an unpacked extension |
| 2790 directory. Returns the extension ID if successfully installed and loaded. | 2954 directory. Returns the extension ID if successfully installed and loaded. |
| 2791 Otherwise, throws an exception. The extension must not already be installed. | 2955 Otherwise, throws an exception. The extension must not already be installed. |
| 2792 | 2956 |
| 2793 Args: | 2957 Args: |
| 2794 extension_path: The absolute path to the extension to install. If the | 2958 extension_path: The absolute path to the extension to install. If the |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3016 """ | 3180 """ |
| 3017 def _IsExtensionViewClosed(): | 3181 def _IsExtensionViewClosed(): |
| 3018 extension_views = self.GetBrowserInfo()['extension_views'] | 3182 extension_views = self.GetBrowserInfo()['extension_views'] |
| 3019 for extension_view in extension_views: | 3183 for extension_view in extension_views: |
| 3020 if view == extension_view['view']: | 3184 if view == extension_view['view']: |
| 3021 return False | 3185 return False |
| 3022 return True | 3186 return True |
| 3023 | 3187 |
| 3024 return self.WaitUntil(lambda: _IsExtensionViewClosed()) | 3188 return self.WaitUntil(lambda: _IsExtensionViewClosed()) |
| 3025 | 3189 |
| 3190 def AddHistoryItem(self, item): |
| 3191 """Forge a history item for Chrome. |
| 3192 |
| 3193 Args: |
| 3194 item: a python dictionary representing the history item. Example: |
| 3195 { |
| 3196 # URL is the only mandatory item. |
| 3197 'url': 'http://news.google.com', |
| 3198 # Title is optional. |
| 3199 'title': 'Google News', |
| 3200 # Time is optional; if not set, assume "now". Time is in |
| 3201 # seconds since the Epoch. The python construct to get "Now" |
| 3202 # in the right scale is "time.time()". Can be float or int. |
| 3203 'time': 1271781612 |
| 3204 } |
| 3205 """ |
| 3206 cmd_dict = { # Prepare command for the json interface |
| 3207 'command': 'AddHistoryItem', |
| 3208 'item': item |
| 3209 } |
| 3210 if not 'url' in item: |
| 3211 raise JSONInterfaceError('must specify url') |
| 3212 self._GetResultFromJSONRequest(cmd_dict) |
| 3213 |
| 3026 def GetPluginsInfo(self, windex=0): | 3214 def GetPluginsInfo(self, windex=0): |
| 3027 """Return info about plugins. | 3215 """Return info about plugins. |
| 3028 | 3216 |
| 3029 This is the info available from about:plugins | 3217 This is the info available from about:plugins |
| 3030 | 3218 |
| 3031 Returns: | 3219 Returns: |
| 3032 an instance of plugins_info.PluginsInfo | 3220 an instance of plugins_info.PluginsInfo |
| 3033 """ | 3221 """ |
| 3034 return plugins_info.PluginsInfo( | 3222 return plugins_info.PluginsInfo( |
| 3035 self._GetResultFromJSONRequest({'command': 'GetPluginsInfo'}, | 3223 self._GetResultFromJSONRequest({'command': 'GetPluginsInfo'}, |
| (...skipping 3472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6508 successful = result.wasSuccessful() | 6696 successful = result.wasSuccessful() |
| 6509 if not successful: | 6697 if not successful: |
| 6510 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6698 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
| 6511 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6699 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
| 6512 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6700 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
| 6513 sys.exit(not successful) | 6701 sys.exit(not successful) |
| 6514 | 6702 |
| 6515 | 6703 |
| 6516 if __name__ == '__main__': | 6704 if __name__ == '__main__': |
| 6517 Main() | 6705 Main() |
| OLD | NEW |