Fix return code issue, check return code of safari process and dump results to log
[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=None):
27         args = [os.path.join(browserBuildPath, 'Safari.app/Contents/MacOS/Safari')]
28         args.extend(self.safariPreferences)
29         _log.info('Launching safari: %s with url: %s' % (args[0], url))
30         self.safariProcess = subprocess.Popen(args, env={'DYLD_FRAMEWORK_PATH': browserBuildPath, 'DYLD_LIBRARY_PATH': browserBuildPath}, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
31         # Stop for initialization of the safari process, otherwise, open
32         # command may use the system safari.
33         time.sleep(3)
34         subprocess.Popen(['open', url])
35
36     def closeBrowsers(self):
37         _log.info('Closing all existing safari processes')
38         safariInstances = NSRunningApplication.runningApplicationsWithBundleIdentifier_('com.apple.Safari')
39         for safariInstance in safariInstances:
40             safariInstance.terminate()
41         if self.safariProcess:
42             _log.info('Safari process console output:\nstdout: %s\nstderr: %s' % self.safariProcess.communicate())
43             if self.safariProcess.returncode:
44                 _log.error('Safari Crashed!')