Skip to content

Commit 7c9b102

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

2 files changed

Lines changed: 49 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: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ 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+
for session_file in ["Current Session", "Current Tabs", "Last Session", "Last Tabs"]:
59+
filepath = os.path.join(brave_profile, session_file)
60+
try:
61+
if os.path.exists(filepath):
62+
os.remove(filepath)
63+
print(f"Removed {session_file}")
64+
except OSError:
65+
pass
66+
4067
@classmethod
4168
def launch_browser(cls):
4269
print("Starting Browser...")
@@ -54,18 +81,7 @@ def launch_browser(cls):
5481
print("Brave Browser executable not found.")
5582
exit(1)
5683

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
84+
cls.ensure_browser_clean()
6985

7086
subprocess.Popen([
7187
browser_path,
@@ -76,11 +92,10 @@ def launch_browser(cls):
7692
"--restore-last-session=false"
7793
])
7894

79-
# Give Brave more time to fully initialize remote debugging
8095
print("Waiting for Brave to fully initialize...")
8196
time.sleep(10)
8297

83-
# Dismiss any macOS keychain dialog that may appear from a previous failed cleanup
98+
# Dismiss any macOS keychain dialog from a previous failed cleanup
8499
subprocess.run([
85100
"osascript", "-e",
86101
'tell application "System Events"\n'
@@ -92,8 +107,7 @@ def launch_browser(cls):
92107
'end tell'
93108
], check=False, capture_output=True, timeout=5)
94109
time.sleep(1)
95-
96-
# Verify remote debugging is accessible
110+
97111
try:
98112
import urllib.request
99113
with urllib.request.urlopen("http://127.0.0.1:9222/json", timeout=5) as response:
@@ -107,34 +121,23 @@ def launch_browser(cls):
107121
def stop_browser(cls):
108122
print("Stopping Brave Browser...")
109123
try:
110-
# Close all tabs first so the browser won't restore them on next launch
111124
subprocess.run([
112125
"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",
121126
'tell application "Brave Browser" to quit'
122127
], check=False, capture_output=True, timeout=10)
123128
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)
129+
130+
result = subprocess.run(["pgrep", "-f", "Brave Browser"],
131+
capture_output=True, text=True)
128132
if result.returncode == 0:
129-
# Still running, force kill
130-
subprocess.run(["pkill", "-f", "Brave Browser"],
131-
check=False, capture_output=True)
133+
subprocess.run(["pkill", "-f", "Brave Browser"],
134+
check=False, capture_output=True)
132135
print("Killed Brave Browser processes")
133-
136+
134137
print("Brave Browser has been closed.")
135138
except Exception as e:
136139
print("Brave Browser might not be running.")
137-
140+
138141
time.sleep(3)
139142
print("Stopped Brave Browser")
140143

0 commit comments

Comments
 (0)