Fix BenchmarkRunner to only import selenium when needed
[WebKit-https.git] / Tools / Scripts / webkitpy / benchmark_runner / browser_driver / osx_safari_driver.py
1 #!/usr/bin/env python
2
3 import logging
4 import os
5 import subprocess
6 import time
7
8 from osx_browser_driver import OSXBrowserDriver
9 from webkitpy.benchmark_runner.utils import force_remove
10
11
12 _log = logging.getLogger(__name__)
13
14
15 class OSXSafariDriver(OSXBrowserDriver):
16     process_name = 'Safari'
17     browser_name = 'safari'
18
19     def prepare_env(self, config):
20         self._safari_process = None
21         super(OSXSafariDriver, self).prepare_env(config)
22         force_remove(os.path.join(os.path.expanduser('~'), 'Library/Saved Application State/com.apple.Safari.savedState'))
23         force_remove(os.path.join(os.path.expanduser('~'), 'Library/Safari/LastSession.plist'))
24         self._maximize_window()
25         self._safari_preferences = ["-HomePage", "about:blank", "-WarnAboutFraudulentWebsites", "0", "-ExtensionsEnabled", "0", "-ShowStatusBar", "0", "-NewWindowBehavior", "1", "-NewTabBehavior", "1"]
26
27     def launch_url(self, url, options, browser_build_path):
28         args = ['/Applications/Safari.app/Contents/MacOS/Safari']
29         env = {}
30         if browser_build_path:
31             safari_app_in_build_path = os.path.join(browser_build_path, 'Safari.app/Contents/MacOS/Safari')
32             if os.path.exists(safari_app_in_build_path):
33                 args = [safari_app_in_build_path]
34                 env = {'DYLD_FRAMEWORK_PATH': browser_build_path, 'DYLD_LIBRARY_PATH': browser_build_path, '__XPC_DYLD_LIBRARY_PATH': browser_build_path}
35             else:
36                 _log.info('Could not find Safari.app at %s, using the system Safari instead' % safari_app_in_build_path)
37
38         args.extend(self._safari_preferences)
39         _log.info('Launching safari: %s with url: %s' % (args[0], url))
40         self._safari_process = OSXSafariDriver._launch_process_with_caffinate(args, env)
41
42         # Stop for initialization of the safari process, otherwise, open
43         # command may use the system safari.
44         time.sleep(3)
45         subprocess.Popen(['open', '-a', args[0], url])
46
47     def launch_driver(self, url, options, browser_build_path):
48         from webkitpy.thirdparty.autoinstalled.selenium import webdriver
49         driver = webdriver.Safari(quiet=False)
50         self._launch_webdriver(url=url, driver=driver)
51         return driver
52
53     def close_browsers(self):
54         super(OSXSafariDriver, self).close_browsers()
55         if self._safari_process and self._safari_process.returncode:
56             sys.exit('Browser crashed with exitcode %d' % self._process.returncode)
57
58     @classmethod
59     def _maximize_window(cls):
60         try:
61             subprocess.check_call(['/usr/bin/defaults', 'write', 'com.apple.Safari', 'NSWindow Frame BrowserWindowFrame', ' '.join(['0', '0', str(cls._screen_size().width), str(cls._screen_size().height)] * 2)])
62         except Exception as error:
63             _log.error('Reset safari window size failed - Error: {}'.format(error))