@@ -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