11import sys
22import time
3+ import os
4+ import subprocess
35from pathlib import Path
46
57from selenium import webdriver
2123class MacTest (UnityTest ):
2224
2325 altdriver = None
24- seleniumdriver = None
2526
2627 @classmethod
2728 def setUpClass (cls ):
2829 open_sample_app ()
2930 cls .altdriver = AltDriver ()
31+ cls .stop_browser ()
3032
3133 @classmethod
3234 def tearDownClass (cls ):
3335 stop_sample_app ()
3436 cls .altdriver .stop ()
37+ cls .stop_browser ()
3538
3639 @classmethod
37- def setupChrome (cls ):
38- print ("Connect to Chrome" )
39- chrome_options = Options ()
40- chrome_options .add_argument ('--remote-debugging-port=9222' )
40+ def launch_browser (cls ):
41+ print ("Starting Browser..." )
42+ browser_paths = [
43+ "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
44+ ]
45+
46+ browser_path = None
47+ for path in browser_paths :
48+ if os .path .exists (path ):
49+ browser_path = path
50+ break
51+
52+ if not browser_path :
53+ print ("Brave executable not found." )
54+ exit (1 )
55+
56+ subprocess .Popen ([
57+ browser_path ,
58+ "--remote-debugging-port=9222"
59+ ])
4160
42- # Initialise Chrome driver
43- cls .seleniumdriver = webdriver .Chrome (options = chrome_options )
61+ time .sleep (5 )
4462
45- print ("Open a window on Chrome" )
46- cls .seleniumdriver .current_window_handle
63+ @classmethod
64+ def stop_browser (cls ):
65+ print ("Stopping Brave..." )
66+ try :
67+ # First try graceful shutdown using AppleScript
68+ subprocess .run ([
69+ "osascript" , "-e" ,
70+ 'tell application "Brave Browser" to quit'
71+ ], check = False , capture_output = True )
72+ time .sleep (2 )
73+
74+ # Check if still running, then force kill
75+ result = subprocess .run (["pgrep" , "-f" , "Brave Browser" ],
76+ capture_output = True , text = True )
77+ if result .returncode == 0 :
78+ # Still running, force kill
79+ subprocess .run (["pkill" , "-f" , "Brave Browser" ],
80+ check = False , capture_output = True )
81+
82+ print ("All Brave processes have been closed." )
83+ except Exception as e :
84+ print ("Brave might not be running." )
85+
86+ time .sleep (3 )
87+ print ("Stopped Brave" )
4788
4889 @classmethod
4990 def login (cls ):
50- print ("Waiting for new window..." )
51- WebDriverWait (cls .seleniumdriver , 30 ).until (EC .number_of_windows_to_be (2 ))
91+ print ("Connect to Chrome" )
92+ # Set up Chrome options to connect to the existing Chrome instance
93+ chrome_options = Options ()
94+ chrome_options .add_experimental_option ("debuggerAddress" , "localhost:9222" )
95+ # Connect to the existing Chrome instance
96+ cls .seleniumdriver = webdriver .Chrome (options = chrome_options )
5297
53- # Switch to the new window
54- all_windows = cls .seleniumdriver .window_handles
55- new_window = [window for window in all_windows if window != cls .seleniumdriver .current_window_handle ][0 ]
56- cls .seleniumdriver .switch_to .window (new_window )
57- print ("Switched to new window" )
98+ print ("Open a window on Chrome" )
5899
59- ## Device confirmation
60- contine_button = WebDriverWait (cls .seleniumdriver , 60 ).until (EC .element_to_be_clickable ((SeleniumBy .XPATH , "//button[span[text()='Continue']]" )))
61- contine_button .click ()
100+ wait = WebDriverWait (cls .seleniumdriver , 60 )
62101
63102 # Wait for email input and enter email
64103 email_field = WebDriverWait (cls .seleniumdriver , 60 ).until (EC .presence_of_element_located ((SeleniumBy .ID , ':r1:' )))
@@ -83,16 +122,23 @@ def login(cls):
83122 print ("Entering OTP..." )
84123 otp_field .send_keys (code )
85124
86- # Wait for success page and confirm
87- success = WebDriverWait (cls .seleniumdriver , 60 ).until (EC .presence_of_element_located ((SeleniumBy .CSS_SELECTOR , 'h1[data-testid="device_success_title"]' )))
88- print ("Connected to Passport!" )
89-
125+ time .sleep (5 )
126+
90127 cls .seleniumdriver .quit ()
91128
92- def test_1_device_code_login (self ):
93- # Select use device code auth
94- self .altdriver .find_object (By .NAME , "DeviceCodeAuth" ).tap ()
129+ @classmethod
130+ def logout (cls ):
131+ print ("Logging out..." )
132+ cls .launch_browser ()
133+ bring_sample_app_to_foreground ()
134+ cls .altdriver .find_object (By .NAME , "LogoutBtn" ).tap ()
135+ time .sleep (5 )
136+ cls .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
137+ time .sleep (2 )
138+ cls .stop_browser ()
139+ print ("Logged out" )
95140
141+ def test_1_login (self ):
96142 # Wait for unauthenticated screen
97143 self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
98144
@@ -104,15 +150,16 @@ def test_1_device_code_login(self):
104150
105151 # Login
106152 print ("Logging in..." )
107- self .setupChrome ()
153+ self .launch_browser ()
108154 bring_sample_app_to_foreground ()
109155 login_button .tap ()
110156 self .login ()
111- bring_sample_app_to_foreground ()
112157
113158 # Wait for authenticated screen
114159 self .altdriver .wait_for_current_scene_to_be ("AuthenticatedScene" )
115160 print ("Logged in" )
161+
162+ self .stop_browser ()
116163 return
117164 except Exception as err :
118165 if attempt == 0 :
@@ -127,16 +174,7 @@ def test_1_device_code_login(self):
127174 print ("Re-logged in" )
128175
129176 # Logout
130- print ("Logging out..." )
131- self .setupChrome ()
132- bring_sample_app_to_foreground ()
133- self .altdriver .find_object (By .NAME , "LogoutBtn" ).tap ()
134- time .sleep (5 )
135- bring_sample_app_to_foreground ()
136-
137- # Wait for unauthenticated screen
138- self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
139- self .seleniumdriver .quit ()
177+ self .logout ()
140178 print ("Logged out and successfully reset app" )
141179
142180 time .sleep (5 )
@@ -155,18 +193,16 @@ def test_4_imx_functions(self):
155193 def test_5_zkevm_functions (self ):
156194 self .test_3_zkevm_functions ()
157195
158- def test_6_device_code_relogin (self ):
196+ def test_6_relogin (self ):
159197 # Close and reopen app
160198 stop_sample_app ()
161199 open_sample_app ()
162200
163201 # Restart AltTester
164202 self .altdriver .stop ()
165- self .altdriver = AltDriver ()
203+ self .__class__ . altdriver = AltDriver ()
166204 time .sleep (5 )
167205
168- # Select use device code auth
169- self .altdriver .find_object (By .NAME , "DeviceCodeAuth" ).tap ()
170206 # Wait for unauthenticated screen
171207 self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
172208
@@ -189,18 +225,16 @@ def test_6_device_code_relogin(self):
189225
190226 self .altdriver .stop ()
191227
192- def test_7_reconnect_device_code_connect_imx (self ):
228+ def test_7_reconnect_connect_imx (self ):
193229 # Close and reopen app
194230 stop_sample_app ()
195231 open_sample_app ()
196232
197233 # Restart AltTester
198234 self .altdriver .stop ()
199- self .altdriver = AltDriver ()
235+ self .__class__ . altdriver = AltDriver ()
200236 time .sleep (5 )
201237
202- # Select use device code auth
203- self .altdriver .find_object (By .NAME , "DeviceCodeAuth" ).tap ()
204238 # Wait for unauthenticated screen
205239 self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
206240
@@ -222,28 +256,19 @@ def test_7_reconnect_device_code_connect_imx(self):
222256 self .assertEqual (TestConfig .WALLET_ADDRESS , output .get_text ())
223257
224258 # Logout
225- print ("Logging out..." )
226- self .setupChrome ()
227- bring_sample_app_to_foreground ()
228- self .altdriver .find_object (By .NAME , "LogoutBtn" ).tap ()
229- time .sleep (5 )
230- bring_sample_app_to_foreground ()
231-
232- # Wait for authenticated screen
233- self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
234- self .seleniumdriver .quit ()
235- print ("Logged out" )
259+ self .logout ()
236260
237261 # Connect IMX
262+ time .sleep (5 )
238263 print ("Logging in and connecting to IMX..." )
239- self .setupChrome ()
264+ self .launch_browser ()
240265 bring_sample_app_to_foreground ()
241266 self .altdriver .wait_for_object (By .NAME , "ConnectBtn" ).tap ()
242267 self .login ()
243- bring_sample_app_to_foreground ()
244268
245- # Wait for authenticated screen
246269 self .altdriver .wait_for_current_scene_to_be ("AuthenticatedScene" )
270+
271+ self .stop_browser ()
247272 print ("Logged in and connected to IMX" )
248273
249274 # Get access token
@@ -256,14 +281,4 @@ def test_7_reconnect_device_code_connect_imx(self):
256281 self .assertEqual (TestConfig .WALLET_ADDRESS , output .get_text ())
257282
258283 # Logout
259- print ("Logging out..." )
260- self .setupChrome ()
261- bring_sample_app_to_foreground ()
262- self .altdriver .find_object (By .NAME , "LogoutBtn" ).tap ()
263- time .sleep (5 )
264- bring_sample_app_to_foreground ()
265-
266- # Wait for authenticated screen
267- self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
268- self .seleniumdriver .quit ()
269- print ("Logged out" )
284+ self .logout ()
0 commit comments