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 supports_tab_control=False)
33 assert browser_type in FindAllBrowserTypes(finder_options), \
34 ('Please add %s to webdriver_desktop_browser_finder.FindAllBrowserTypes'
37 def CreateWebDriverBackend(self, platform_backend, finder_options):
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
49 def Create(self, finder_options):
50 self._InitPlatformIfNeeded()
51 backend = self.CreateWebDriverBackend(
52 self._platform_backend, finder_options)
53 return browser.Browser(backend, self._platform_backend,
54 self._credentials_path)
56 def SupportsOptions(self, finder_options):
57 if len(finder_options.extensions_to_load) != 0:
61 def UpdateExecutableIfNeeded(self):
65 def last_modification_time(self):
69 class PossibleDesktopIE(PossibleWebDriverBrowser):
70 def __init__(self, browser_type, finder_options, architecture):
71 super(PossibleDesktopIE, self).__init__(browser_type, finder_options)
72 self._architecture = architecture
74 def CreateWebDriverBackend(self, platform_backend, finder_options):
77 ie_driver_exe = support_binaries.FindPath(
78 'IEDriverServer_%s' % self._architecture,
81 return webdriver.Ie(executable_path=ie_driver_exe)
82 return webdriver_ie_backend.WebDriverIEBackend(
83 platform_backend, DriverCreator, finder_options.browser_options)
85 def SelectDefaultBrowser(_):
88 def FindAllBrowserTypes(_):
92 'internet-explorer-x64']
94 logging.warning('Webdriver backend is unsupported without selenium pylib. '
95 'For installation of selenium pylib, please refer to '
96 'https://code.google.com/p/selenium/wiki/PythonBindings.')
99 def FindAllAvailableBrowsers(finder_options):
100 """Finds all the desktop browsers available on this machine."""
105 # Look for the IE browser in the standard location.
106 if sys.platform.startswith('win'):
107 ie_path = os.path.join('Internet Explorer', 'iexplore.exe')
109 (32, os.getenv('PROGRAMFILES(X86)'), 'internet-explorer'),
110 (64, os.getenv('PROGRAMFILES'), 'internet-explorer-x64'),
112 for architecture, search_path, browser_type in search_paths:
115 if os.path.exists(os.path.join(search_path, ie_path)):
117 PossibleDesktopIE(browser_type, finder_options, architecture))