Make arguments of run-benchmark more user friendly
[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 # We assume that this handle can only be used when the platform is OSX.
9 from AppKit import NSRunningApplication
10 from browser_driver import BrowserDriver
11 from webkitpy.benchmark_runner.utils import forceRemove
12
13
14 _log = logging.getLogger(__name__)
15
16
17 class OSXSafariDriver(BrowserDriver):
18
19     def prepareEnv(self):
20         self.safariProcess = None
21         self.closeBrowsers()
22         forceRemove(os.path.join(os.path.expanduser('~'), 'Library/Saved Application State/com.apple.Safari.savedState'))
23         forceRemove(os.path.join(os.path.expanduser('~'), 'Library/Safari/LastSession.plist'))
24         self.safariPreferences = ["-HomePage", "about:blank", "-WarnAboutFraudulentWebsites", "0", "-ExtensionsEnabled", "0", "-ShowStatusBar", "0", "-NewWindowBehavior", "1", "-NewTabBehavior", "1"]
25
26     def launchUrl(self, url, browserBuildPath):
27         args = ['/Applications/Safari.app/Contents/MacOS/SafariForWebKitDevelopment']
28         env = {}
29         if browserBuildPath:
30             safariAppInBuildPath = os.path.join(browserBuildPath, 'Safari.app/Contents/MacOS/Safari')
31             if os.path.exists(safariAppInBuildPath):
32                 args = [safariAppInBuildPath]
33                 env = {'DYLD_FRAMEWORK_PATH': browserBuildPath, 'DYLD_LIBRARY_PATH': browserBuildPath}
34             else:
35                 _log.info('Could not find Safari.app at %s, using the system SafariForWebKitDevelopment in /Applications instead' % safariAppInBuildPath)
36
37         args.extend(self.safariPreferences)
38         _log.info('Launching safari: %s with url: %s' % (args[0], url))
39         self.safariProcess = subprocess.Popen(args, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
40         # Stop for initialization of the safari process, otherwise, open
41         # command may use the system safari.
42         time.sleep(3)
43         subprocess.Popen(['open', url])
44
45     def closeBrowsers(self):
46         _log.info('Closing all existing safari processes')
47         safariInstances = NSRunningApplication.runningApplicationsWithBundleIdentifier_('com.apple.Safari')
48         for safariInstance in safariInstances:
49             safariInstance.terminate()
50         if self.safariProcess:
51             _log.info('Safari process console output:\nstdout: %s\nstderr: %s' % self.safariProcess.communicate())
52             if self.safariProcess.returncode:
53                 _log.error('Safari Crashed!')