Move cursor to (10, 0) instead of (0, 0) and polish the mouse move code
[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 forceRemove
11
12
13 _log = logging.getLogger(__name__)
14
15
16 class OSXSafariDriver(OSXBrowserDriver):
17     bundleIdentifier = 'com.apple.Safari'
18
19     def prepareEnv(self):
20         self.safariProcess = None
21         super(OSXSafariDriver, self).prepareEnv()
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.maximizeWindow()
25         self.safariPreferences = ["-HomePage", "about:blank", "-WarnAboutFraudulentWebsites", "0", "-ExtensionsEnabled", "0", "-ShowStatusBar", "0", "-NewWindowBehavior", "1", "-NewTabBehavior", "1"]
26
27     def launchUrl(self, url, browserBuildPath):
28         args = ['/Applications/Safari.app/Contents/MacOS/Safari']
29         env = {}
30         if browserBuildPath:
31             safariAppInBuildPath = os.path.join(browserBuildPath, 'Safari.app/Contents/MacOS/Safari')
32             if os.path.exists(safariAppInBuildPath):
33                 args = [safariAppInBuildPath]
34                 env = {'DYLD_FRAMEWORK_PATH': browserBuildPath, 'DYLD_LIBRARY_PATH': browserBuildPath, '__XPC_DYLD_LIBRARY_PATH': browserBuildPath}
35             else:
36                 _log.info('Could not find Safari.app at %s, using the system Safari instead' % safariAppInBuildPath)
37
38         args.extend(self.safariPreferences)
39         _log.info('Launching safari: %s with url: %s' % (args[0], url))
40         self.safariProcess = OSXSafariDriver.launchProcessWithCaffinate(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', url])
46
47     def closeBrowsers(self):
48         super(OSXSafariDriver, self).closeBrowsers()
49         if self.safariProcess and self.safariProcess.returncode:
50             sys.exit('Browser crashed with exitcode %d' % self._process.returncode)
51
52     @classmethod
53     def maximizeWindow(cls):
54         try:
55             subprocess.check_call(['/usr/bin/defaults', 'write', 'com.apple.Safari', 'NSWindow Frame BrowserWindowFrame', ' '.join(['0', '0', str(cls.screenSize().width), str(cls.screenSize().height)] * 2)])
56         except Exception as error:
57             _log.error('Reset safari window size failed - Error: {}'.format(error))