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.core import browser
11 from telemetry.core import platform as platform_module
12 from telemetry.core import possible_browser
13 from telemetry.core import util
14 from telemetry.core.backends.webdriver import webdriver_ie_backend
15 from telemetry.util import support_binaries
17 # Try to import the selenium python lib which may be not available.
18 util.AddDirToPythonPath(
19 util.GetChromiumSrcDir(), 'third_party', 'webdriver', 'pylib')
21 from selenium import webdriver # pylint: disable=F0401
26 class PossibleWebDriverBrowser(possible_browser.PossibleBrowser):
27 """A browser that can be controlled through webdriver API."""
29 def __init__(self, browser_type, finder_options):
30 target_os = sys.platform.lower()
31 super(PossibleWebDriverBrowser, self).__init__(browser_type, target_os,
32 finder_options, False)
33 assert browser_type in FindAllBrowserTypes(), \
34 ('Please add %s to webdriver_desktop_browser_finder.FindAllBrowserTypes'
37 def CreateWebDriverBackend(self, platform_backend):
38 raise NotImplementedError()
40 def _InitPlatformIfNeeded(self):
44 self._platform = platform_module.GetHostPlatform()
46 # pylint: disable=W0212
47 self._platform_backend = self._platform._platform_backend
50 self._InitPlatformIfNeeded()
51 backend = self.CreateWebDriverBackend(self._platform_backend)
52 return browser.Browser(backend, self._platform_backend)
54 def SupportsOptions(self, finder_options):
55 if len(finder_options.extensions_to_load) != 0:
59 def UpdateExecutableIfNeeded(self):
63 def last_modification_time(self):
67 class PossibleDesktopIE(PossibleWebDriverBrowser):
68 def __init__(self, browser_type, finder_options, architecture):
69 super(PossibleDesktopIE, self).__init__(browser_type, finder_options)
70 self._architecture = architecture
72 def CreateWebDriverBackend(self, platform_backend):
75 ie_driver_exe = support_binaries.FindPath(
76 'IEDriverServer_%s' % self._architecture, 'win')
77 return webdriver.Ie(executable_path=ie_driver_exe)
78 return webdriver_ie_backend.WebDriverIEBackend(
79 platform_backend, DriverCreator, self.finder_options.browser_options)
81 def SelectDefaultBrowser(_):
84 def FindAllBrowserTypes():
88 'internet-explorer-x64']
90 logging.warning('Webdriver backend is unsupported without selenium pylib. '
91 'For installation of selenium pylib, please refer to '
92 'https://code.google.com/p/selenium/wiki/PythonBindings.')
95 def FindAllAvailableBrowsers(finder_options):
96 """Finds all the desktop browsers available on this machine."""
101 # Look for the IE browser in the standard location.
102 if sys.platform.startswith('win'):
103 ie_path = os.path.join('Internet Explorer', 'iexplore.exe')
105 (32, os.getenv('PROGRAMFILES(X86)'), 'internet-explorer'),
106 (64, os.getenv('PROGRAMFILES'), 'internet-explorer-x64'),
108 for architecture, search_path, browser_type in search_paths:
111 if os.path.exists(os.path.join(search_path, ie_path)):
113 PossibleDesktopIE(browser_type, finder_options, architecture))