1
1
import sys
2
2
import time
3
+ import os
4
+ import subprocess
3
5
from pathlib import Path
4
6
5
7
from selenium import webdriver
21
23
class MacTest (UnityTest ):
22
24
23
25
altdriver = None
24
- seleniumdriver = None
25
26
26
27
@classmethod
27
28
def setUpClass (cls ):
28
29
open_sample_app ()
29
30
cls .altdriver = AltDriver ()
31
+ cls .stop_browser ()
30
32
31
33
@classmethod
32
34
def tearDownClass (cls ):
33
35
stop_sample_app ()
34
36
cls .altdriver .stop ()
37
+ cls .stop_browser ()
35
38
36
39
@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
+ ])
41
60
42
- # Initialise Chrome driver
43
- cls .seleniumdriver = webdriver .Chrome (options = chrome_options )
61
+ time .sleep (5 )
44
62
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" )
47
88
48
89
@classmethod
49
90
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 )
52
97
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" )
58
99
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 )
62
101
63
102
# Wait for email input and enter email
64
103
email_field = WebDriverWait (cls .seleniumdriver , 60 ).until (EC .presence_of_element_located ((SeleniumBy .ID , ':r1:' )))
@@ -83,16 +122,23 @@ def login(cls):
83
122
print ("Entering OTP..." )
84
123
otp_field .send_keys (code )
85
124
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
+
90
127
cls .seleniumdriver .quit ()
91
128
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" )
95
140
141
+ def test_1_login (self ):
96
142
# Wait for unauthenticated screen
97
143
self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
98
144
@@ -104,15 +150,16 @@ def test_1_device_code_login(self):
104
150
105
151
# Login
106
152
print ("Logging in..." )
107
- self .setupChrome ()
153
+ self .launch_browser ()
108
154
bring_sample_app_to_foreground ()
109
155
login_button .tap ()
110
156
self .login ()
111
- bring_sample_app_to_foreground ()
112
157
113
158
# Wait for authenticated screen
114
159
self .altdriver .wait_for_current_scene_to_be ("AuthenticatedScene" )
115
160
print ("Logged in" )
161
+
162
+ self .stop_browser ()
116
163
return
117
164
except Exception as err :
118
165
if attempt == 0 :
@@ -127,16 +174,7 @@ def test_1_device_code_login(self):
127
174
print ("Re-logged in" )
128
175
129
176
# 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 ()
140
178
print ("Logged out and successfully reset app" )
141
179
142
180
time .sleep (5 )
@@ -155,18 +193,16 @@ def test_4_imx_functions(self):
155
193
def test_5_zkevm_functions (self ):
156
194
self .test_3_zkevm_functions ()
157
195
158
- def test_6_device_code_relogin (self ):
196
+ def test_6_relogin (self ):
159
197
# Close and reopen app
160
198
stop_sample_app ()
161
199
open_sample_app ()
162
200
163
201
# Restart AltTester
164
202
self .altdriver .stop ()
165
- self .altdriver = AltDriver ()
203
+ self .__class__ . altdriver = AltDriver ()
166
204
time .sleep (5 )
167
205
168
- # Select use device code auth
169
- self .altdriver .find_object (By .NAME , "DeviceCodeAuth" ).tap ()
170
206
# Wait for unauthenticated screen
171
207
self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
172
208
@@ -189,18 +225,16 @@ def test_6_device_code_relogin(self):
189
225
190
226
self .altdriver .stop ()
191
227
192
- def test_7_reconnect_device_code_connect_imx (self ):
228
+ def test_7_reconnect_connect_imx (self ):
193
229
# Close and reopen app
194
230
stop_sample_app ()
195
231
open_sample_app ()
196
232
197
233
# Restart AltTester
198
234
self .altdriver .stop ()
199
- self .altdriver = AltDriver ()
235
+ self .__class__ . altdriver = AltDriver ()
200
236
time .sleep (5 )
201
237
202
- # Select use device code auth
203
- self .altdriver .find_object (By .NAME , "DeviceCodeAuth" ).tap ()
204
238
# Wait for unauthenticated screen
205
239
self .altdriver .wait_for_current_scene_to_be ("UnauthenticatedScene" )
206
240
@@ -222,28 +256,19 @@ def test_7_reconnect_device_code_connect_imx(self):
222
256
self .assertEqual (TestConfig .WALLET_ADDRESS , output .get_text ())
223
257
224
258
# 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 ()
236
260
237
261
# Connect IMX
262
+ time .sleep (5 )
238
263
print ("Logging in and connecting to IMX..." )
239
- self .setupChrome ()
264
+ self .launch_browser ()
240
265
bring_sample_app_to_foreground ()
241
266
self .altdriver .wait_for_object (By .NAME , "ConnectBtn" ).tap ()
242
267
self .login ()
243
- bring_sample_app_to_foreground ()
244
268
245
- # Wait for authenticated screen
246
269
self .altdriver .wait_for_current_scene_to_be ("AuthenticatedScene" )
270
+
271
+ self .stop_browser ()
247
272
print ("Logged in and connected to IMX" )
248
273
249
274
# Get access token
@@ -256,14 +281,4 @@ def test_7_reconnect_device_code_connect_imx(self):
256
281
self .assertEqual (TestConfig .WALLET_ADDRESS , output .get_text ())
257
282
258
283
# 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