Make run-benchmark script supports 'config' key in test plan.
[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 re
6 import subprocess
7 import time
8
9 from osx_browser_driver import OSXBrowserDriver
10 from webkitpy.benchmark_runner.utils import force_remove
11
12
13 _log = logging.getLogger(__name__)
14
15
16 class OSXSafariDriver(OSXBrowserDriver):
17     process_name = 'Safari'
18     browser_name = 'safari'
19
20     def prepare_env(self, config):
21         self._safari_process = None
22         super(OSXSafariDriver, self).prepare_env(config)
23         force_remove(os.path.join(os.path.expanduser('~'), 'Library/Saved Application State/com.apple.Safari.savedState'))
24         force_remove(os.path.join(os.path.expanduser('~'), 'Library/Safari/LastSession.plist'))
25         self._maximize_window()
26         self._safari_preferences = ["-HomePage", "about:blank", "-WarnAboutFraudulentWebsites", "0", "-ExtensionsEnabled", "0", "-ShowStatusBar", "0", "-NewWindowBehavior", "1", "-NewTabBehavior", "1"]
27
28     def launch_url(self, url, options, browser_build_path):
29         args = ['/Applications/Safari.app/Contents/MacOS/Safari']
30         env = {}
31         if browser_build_path:
32             safari_app_in_build_path = os.path.join(browser_build_path, 'Safari.app/Contents/MacOS/Safari')
33             if os.path.exists(safari_app_in_build_path):
34                 args = [safari_app_in_build_path]
35                 env = {'DYLD_FRAMEWORK_PATH': browser_build_path, 'DYLD_LIBRARY_PATH': browser_build_path, '__XPC_DYLD_LIBRARY_PATH': browser_build_path}
36             else:
37                 _log.info('Could not find Safari.app at %s, using the system Safari instead' % safari_app_in_build_path)
38
39         args.extend(self._safari_preferences)
40         _log.info('Launching safari: %s with url: %s' % (args[0], url))
41         self._safari_process = OSXSafariDriver._launch_process_with_caffinate(args, env)
42
43         # Stop for initialization of the safari process, otherwise, open
44         # command may use the system safari.
45         time.sleep(3)
46         subprocess.Popen(['open', '-a', args[0], url])
47
48     def close_browsers(self):
49         super(OSXSafariDriver, self).close_browsers()
50         if self._safari_process and self._safari_process.returncode:
51             sys.exit('Browser crashed with exitcode %d' % self._process.returncode)
52
53     @classmethod
54     def _maximize_window(cls):
55         try:
56             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)])
57         except Exception as error:
58             _log.error('Reset safari window size failed - Error: {}'.format(error))