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."""
10 from telemetry import decorators
11 from telemetry.core import browser
12 from telemetry.core import possible_browser
13 from telemetry.core import platform
14 from telemetry.core import util
15 from telemetry.core.backends.webdriver import webdriver_ie_backend
16 from telemetry.page import cloud_storage
18 # Try to import the selenium python lib which may be not available.
19 util.AddDirToPythonPath(
20 util.GetChromiumSrcDir(), 'third_party', 'webdriver', 'pylib')
22 from selenium import webdriver # pylint: disable=F0401
26 ALL_BROWSER_TYPES = []
30 'internet-explorer-x64']
32 logging.warning('Webdriver backend is unsupported without selenium pylib. '
33 'For installation of selenium pylib, please refer to '
34 'https://code.google.com/p/selenium/wiki/PythonBindings.')
37 class PossibleWebDriverBrowser(possible_browser.PossibleBrowser):
38 """A browser that can be controlled through webdriver API."""
40 def __init__(self, browser_type, finder_options):
41 target_os = sys.platform.lower()
42 super(PossibleWebDriverBrowser, self).__init__(browser_type, target_os,
44 assert browser_type in ALL_BROWSER_TYPES, \
45 'Please add %s to ALL_BROWSER_TYPES' % browser_type
49 def _platform_backend(self):
50 return platform.CreatePlatformBackendForCurrentOS()
52 def CreateWebDriverBackend(self, platform_backend):
53 raise NotImplementedError()
56 backend = self.CreateWebDriverBackend(self._platform_backend)
57 return browser.Browser(backend, self._platform_backend)
59 def SupportsOptions(self, finder_options):
60 if len(finder_options.extensions_to_load) != 0:
64 def UpdateExecutableIfNeeded(self):
68 def last_modification_time(self):
72 class PossibleDesktopIE(PossibleWebDriverBrowser):
73 def __init__(self, browser_type, finder_options, architecture):
74 super(PossibleDesktopIE, self).__init__(browser_type, finder_options)
75 self._architecture = architecture
77 def CreateWebDriverBackend(self, platform_backend):
80 ie_driver_exe = os.path.join(util.GetTelemetryDir(), 'bin',
81 'IEDriverServer_%s.exe' % self._architecture)
82 cloud_storage.GetIfChanged(ie_driver_exe, cloud_storage.PUBLIC_BUCKET)
83 return webdriver.Ie(executable_path=ie_driver_exe)
84 return webdriver_ie_backend.WebDriverIEBackend(
85 platform_backend, DriverCreator, self.finder_options.browser_options)
87 def SelectDefaultBrowser(_):
90 def FindAllAvailableBrowsers(finder_options):
91 """Finds all the desktop browsers available on this machine."""
96 # Look for the IE browser in the standard location.
97 if sys.platform.startswith('win'):
98 ie_path = os.path.join('Internet Explorer', 'iexplore.exe')
100 '32' : { 'path' : os.getenv('PROGRAMFILES(X86)'),
101 'type' : 'internet-explorer'},
102 '64' : { 'path' : os.getenv('PROGRAMFILES'),
103 'type' : 'internet-explorer-x64'}}
104 for architecture, ie_info in win_search_paths.iteritems():
105 if not ie_info['path']:
107 if os.path.exists(os.path.join(ie_info['path'], ie_path)):
109 PossibleDesktopIE(ie_info['type'], finder_options, architecture))