1# Copyright 2023 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7from crossbench.browsers.applescript import AppleScriptBrowser 8from crossbench.browsers.attributes import BrowserAttributes 9from crossbench.browsers.safari.safari import Safari 10 11 12class SafariAppleScript(Safari, AppleScriptBrowser): 13 APPLE_SCRIPT_ALLOW_JS_MENU: str = ( 14 "Develop > Allow JavaScript from Apple Events") 15 APPLE_SCRIPT_JS_COMMAND: str = ( 16 "tell current tab of front window to do javascript %(js_script)s") 17 APPLE_SCRIPT_SET_URL: str = ( 18 "set URL of the current tab of front window to %(url)s") 19 20 def _setup_window(self) -> None: 21 self._exec_apple_script(f""" 22 tell application "System Events" 23 click menu item "New Private Window" of menu "File" of menu bar 1 of process "{self.bundle_name}" 24 end tell 25 set URL of current tab of front window to "" 26 """) 27 self.platform.sleep(0.5) 28 if self.viewport.is_fullscreen: 29 self._exec_apple_script(""" 30 tell application "System Events" 31 keystroke "f" using {command down, control down} 32 end tell""") 33 elif self.viewport.is_maximized: 34 self._exec_apple_script(""" 35 tell application "System Events" 36 keystroke "m" 37 end tell""") 38 else: 39 bounds = (f"{self.viewport.x},{self.viewport.y}," 40 f"{self.viewport.width},{self.viewport.height}") 41 self._exec_apple_script("set the bounds of the first window to {%s}" % 42 bounds) 43 44 def quit(self) -> None: 45 super().quit() 46 # Safari doesn't react to "quit" when using the full app path. 47 self.platform.exec_apple_script(f""" 48 tell application "{self.bundle_name}" 49 quit 50 end tell""") 51 52 @property 53 def attributes(self) -> BrowserAttributes: 54 return BrowserAttributes.SAFARI | BrowserAttributes.APPLESCRIPT 55