1# Copyright 2013 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. 4"""Finds desktop browsers that can be controlled by telemetry.""" 5 6import logging 7import os 8import sys 9 10from telemetry.core import browser 11from telemetry.core import possible_browser 12from telemetry.core import util 13from telemetry.core.backends.webdriver import webdriver_ie_backend 14from telemetry.core.platform import factory 15from telemetry.util import support_binaries 16 17# Try to import the selenium python lib which may be not available. 18util.AddDirToPythonPath( 19 util.GetChromiumSrcDir(), 'third_party', 'webdriver', 'pylib') 20try: 21 from selenium import webdriver # pylint: disable=F0401 22except ImportError: 23 webdriver = None 24 25ALL_BROWSER_TYPES = [] 26if webdriver: 27 ALL_BROWSER_TYPES = [ 28 'internet-explorer', 29 'internet-explorer-x64'] 30else: 31 logging.warning('Webdriver backend is unsupported without selenium pylib. ' 32 'For installation of selenium pylib, please refer to ' 33 'https://code.google.com/p/selenium/wiki/PythonBindings.') 34 35 36class PossibleWebDriverBrowser(possible_browser.PossibleBrowser): 37 """A browser that can be controlled through webdriver API.""" 38 39 def __init__(self, browser_type, finder_options): 40 target_os = sys.platform.lower() 41 super(PossibleWebDriverBrowser, self).__init__(browser_type, target_os, 42 finder_options) 43 assert browser_type in ALL_BROWSER_TYPES, \ 44 'Please add %s to ALL_BROWSER_TYPES' % browser_type 45 46 @property 47 def _platform_backend(self): 48 return factory.GetPlatformBackendForCurrentOS() 49 50 def CreateWebDriverBackend(self, platform_backend): 51 raise NotImplementedError() 52 53 def Create(self): 54 backend = self.CreateWebDriverBackend(self._platform_backend) 55 return browser.Browser(backend, self._platform_backend) 56 57 def SupportsOptions(self, finder_options): 58 if len(finder_options.extensions_to_load) != 0: 59 return False 60 return True 61 62 def UpdateExecutableIfNeeded(self): 63 pass 64 65 @property 66 def last_modification_time(self): 67 return -1 68 69 70class PossibleDesktopIE(PossibleWebDriverBrowser): 71 def __init__(self, browser_type, finder_options, architecture): 72 super(PossibleDesktopIE, self).__init__(browser_type, finder_options) 73 self._architecture = architecture 74 75 def CreateWebDriverBackend(self, platform_backend): 76 assert webdriver 77 def DriverCreator(): 78 ie_driver_exe = support_binaries.FindPath( 79 'IEDriverServer_%s' % self._architecture, 'win') 80 return webdriver.Ie(executable_path=ie_driver_exe) 81 return webdriver_ie_backend.WebDriverIEBackend( 82 platform_backend, DriverCreator, self.finder_options.browser_options) 83 84def SelectDefaultBrowser(_): 85 return None 86 87def FindAllAvailableBrowsers(finder_options): 88 """Finds all the desktop browsers available on this machine.""" 89 browsers = [] 90 if not webdriver: 91 return browsers 92 93 # Look for the IE browser in the standard location. 94 if sys.platform.startswith('win'): 95 ie_path = os.path.join('Internet Explorer', 'iexplore.exe') 96 win_search_paths = { 97 '32' : { 'path' : os.getenv('PROGRAMFILES(X86)'), 98 'type' : 'internet-explorer'}, 99 '64' : { 'path' : os.getenv('PROGRAMFILES'), 100 'type' : 'internet-explorer-x64'}} 101 for architecture, ie_info in win_search_paths.iteritems(): 102 if not ie_info['path']: 103 continue 104 if os.path.exists(os.path.join(ie_info['path'], ie_path)): 105 browsers.append( 106 PossibleDesktopIE(ie_info['type'], finder_options, architecture)) 107 108 return browsers 109