Skip to content

Commit c01b9ac

Browse files
committed
test: clean up mac brave browser
1 parent ca60386 commit c01b9ac

2 files changed

Lines changed: 69 additions & 34 deletions

File tree

.github/workflows/ui-tests.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ jobs:
158158
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
159159
working-directory: sample-unity6/Tests
160160
run: pytest -xs test/test_mac.py::MacTest
161+
- name: Close Brave Browser
162+
if: always()
163+
run: |
164+
osascript -e 'tell application "Brave Browser" to quit' 2>/dev/null || true
165+
sleep 2
166+
pkill -f "Brave Browser" 2>/dev/null || true
161167
- name: Remove temporary keychain
162168
if: always()
163169
run: |
@@ -400,6 +406,12 @@ jobs:
400406
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
401407
working-directory: ${{ matrix.projectPath }}/Tests
402408
run: ${{ matrix.test_script }}
409+
- name: Close Brave Browser
410+
if: contains(matrix.targetPlatform, 'StandaloneOSX') && always()
411+
run: |
412+
osascript -e 'tell application "Brave Browser" to quit' 2>/dev/null || true
413+
sleep 2
414+
pkill -f "Brave Browser" 2>/dev/null || true
403415
- name: Remove temporary keychain
404416
if: contains(matrix.targetPlatform, 'StandaloneOSX') && (github.event_name != 'workflow_dispatch' || github.event.inputs.targetPlatform == 'All' || github.event.inputs.targetPlatform == matrix.targetPlatform)
405417
run: |

sample/Tests/test/test_mac.py

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,53 @@ def tearDownClass(cls):
3737
cls.altdriver.stop()
3838
cls.stop_browser()
3939

40+
@classmethod
41+
def ensure_browser_clean(cls):
42+
"""Kill any running Brave instance and delete session restore files.
43+
Call this before any action that may open the browser (login, logout, etc.)
44+
so Brave always starts with a clean slate."""
45+
result = subprocess.run(["pgrep", "-f", "Brave Browser"], capture_output=True, text=True)
46+
if result.returncode == 0:
47+
print("Brave Browser already running, stopping it...")
48+
subprocess.run(["osascript", "-e", 'tell application "Brave Browser" to quit'],
49+
check=False, capture_output=True, timeout=5)
50+
time.sleep(2)
51+
subprocess.run(["pkill", "-f", "Brave Browser"], check=False, capture_output=True)
52+
time.sleep(2)
53+
print("Existing Brave Browser stopped")
54+
55+
brave_profile = os.path.expanduser(
56+
"~/Library/Application Support/BraveSoftware/Brave-Browser/Default"
57+
)
58+
59+
# Clear session data (Brave stores sessions in Sessions/ directory)
60+
import shutil
61+
sessions_dir = os.path.join(brave_profile, "Sessions")
62+
if os.path.isdir(sessions_dir):
63+
shutil.rmtree(sessions_dir, ignore_errors=True)
64+
print("Removed Sessions directory")
65+
session_storage_dir = os.path.join(brave_profile, "Session Storage")
66+
if os.path.isdir(session_storage_dir):
67+
shutil.rmtree(session_storage_dir, ignore_errors=True)
68+
print("Removed Session Storage directory")
69+
70+
# Patch Preferences so Brave thinks it shut down cleanly,
71+
# otherwise it forces session restore on "Crashed" exit_type
72+
prefs_path = os.path.join(brave_profile, "Preferences")
73+
try:
74+
if os.path.exists(prefs_path):
75+
import json
76+
with open(prefs_path, "r") as f:
77+
prefs = json.load(f)
78+
prefs.setdefault("profile", {})["exit_type"] = "Normal"
79+
prefs["profile"]["exited_cleanly"] = True
80+
prefs.setdefault("session", {})["restore_on_startup"] = 5
81+
with open(prefs_path, "w") as f:
82+
json.dump(prefs, f)
83+
print("Patched Preferences: exit_type=Normal, restore_on_startup=NewTab")
84+
except Exception as e:
85+
print(f"Could not patch Preferences: {e}")
86+
4087
@classmethod
4188
def launch_browser(cls):
4289
print("Starting Browser...")
@@ -54,18 +101,7 @@ def launch_browser(cls):
54101
print("Brave Browser executable not found.")
55102
exit(1)
56103

57-
# Delete session/tab restore files so Brave starts with a clean slate
58-
brave_profile = os.path.expanduser(
59-
"~/Library/Application Support/BraveSoftware/Brave-Browser/Default"
60-
)
61-
for session_file in ["Current Session", "Current Tabs", "Last Session", "Last Tabs"]:
62-
path = os.path.join(brave_profile, session_file)
63-
try:
64-
if os.path.exists(path):
65-
os.remove(path)
66-
print(f"Removed {session_file}")
67-
except OSError:
68-
pass
104+
cls.ensure_browser_clean()
69105

70106
subprocess.Popen([
71107
browser_path,
@@ -76,11 +112,10 @@ def launch_browser(cls):
76112
"--restore-last-session=false"
77113
])
78114

79-
# Give Brave more time to fully initialize remote debugging
80115
print("Waiting for Brave to fully initialize...")
81116
time.sleep(10)
82117

83-
# Dismiss any macOS keychain dialog that may appear from a previous failed cleanup
118+
# Dismiss any macOS keychain dialog from a previous failed cleanup
84119
subprocess.run([
85120
"osascript", "-e",
86121
'tell application "System Events"\n'
@@ -92,8 +127,7 @@ def launch_browser(cls):
92127
'end tell'
93128
], check=False, capture_output=True, timeout=5)
94129
time.sleep(1)
95-
96-
# Verify remote debugging is accessible
130+
97131
try:
98132
import urllib.request
99133
with urllib.request.urlopen("http://127.0.0.1:9222/json", timeout=5) as response:
@@ -107,34 +141,23 @@ def launch_browser(cls):
107141
def stop_browser(cls):
108142
print("Stopping Brave Browser...")
109143
try:
110-
# Close all tabs first so the browser won't restore them on next launch
111144
subprocess.run([
112145
"osascript", "-e",
113-
'tell application "Brave Browser" to repeat while (count of windows) > 0\n'
114-
'tell front window to close\n'
115-
'end repeat'
116-
], check=False, capture_output=True, timeout=10)
117-
time.sleep(1)
118-
119-
subprocess.run([
120-
"osascript", "-e",
121146
'tell application "Brave Browser" to quit'
122147
], check=False, capture_output=True, timeout=10)
123148
time.sleep(2)
124-
125-
# Check if still running, then force kill
126-
result = subprocess.run(["pgrep", "-f", "Brave Browser"],
127-
capture_output=True, text=True)
149+
150+
result = subprocess.run(["pgrep", "-f", "Brave Browser"],
151+
capture_output=True, text=True)
128152
if result.returncode == 0:
129-
# Still running, force kill
130-
subprocess.run(["pkill", "-f", "Brave Browser"],
131-
check=False, capture_output=True)
153+
subprocess.run(["pkill", "-f", "Brave Browser"],
154+
check=False, capture_output=True)
132155
print("Killed Brave Browser processes")
133-
156+
134157
print("Brave Browser has been closed.")
135158
except Exception as e:
136159
print("Brave Browser might not be running.")
137-
160+
138161
time.sleep(3)
139162
print("Stopped Brave Browser")
140163

0 commit comments

Comments
 (0)