Skip to content

Commit c26aaac

Browse files
test: unify mac and windows tests
1 parent 4b97b5d commit c26aaac

File tree

4 files changed

+392
-422
lines changed

4 files changed

+392
-422
lines changed

sample/Tests/test/common_test_base.py

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
import time
2+
from alttester import By
3+
from test import TestConfig, UnityTest
4+
5+
class PlatformTest(UnityTest):
6+
7+
# Methods that should be implemented by platform-specific subclasses
8+
def restart_app_and_altdriver(self):
9+
"""Restart the app and AltDriver. Must be implemented by subclasses."""
10+
raise NotImplementedError("restart_app_and_altdriver must be implemented by subclass")
11+
12+
def launch_browser(self):
13+
"""Launch the browser. Must be implemented by subclasses."""
14+
raise NotImplementedError("launch_browser must be implemented by subclass")
15+
16+
def stop_browser(self):
17+
"""Stop the browser. Must be implemented by subclasses."""
18+
raise NotImplementedError("stop_browser must be implemented by subclass")
19+
20+
def bring_app_to_foreground(self):
21+
"""Bring the app to the foreground. Must be implemented by subclasses."""
22+
raise NotImplementedError("bring_app_to_foreground must be implemented by subclass")
23+
24+
def helper_login(self, use_pkce=False):
25+
"""Perform login in browser. Must be implemented by subclasses."""
26+
raise NotImplementedError("helper_login must be implemented by subclass")
27+
28+
def select_auth_type(self, use_pkce: bool):
29+
"""Select authentication type in the app."""
30+
auth_type = "PKCE" if use_pkce else "DeviceCodeAuth"
31+
self.get_altdriver().find_object(By.NAME, auth_type).tap()
32+
33+
def login(self, use_pkce=False):
34+
self.select_auth_type(use_pkce)
35+
36+
# Wait for unauthenticated screen
37+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
38+
39+
for attempt in range(2):
40+
try:
41+
# Check app state
42+
login_button = self.get_altdriver().find_object(By.NAME, "LoginBtn")
43+
print("Found login button, app is in the correct state")
44+
45+
# Login
46+
print("Logging in...")
47+
self.launch_browser()
48+
self.bring_app_to_foreground()
49+
login_button.tap()
50+
self.helper_login(use_pkce)
51+
self.bring_app_to_foreground()
52+
53+
# Wait for authenticated screen
54+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
55+
self.stop_browser()
56+
print("Logged in")
57+
return
58+
except Exception as err:
59+
print(f"Login attempt {attempt+1} failed with error: {err}")
60+
self.stop_browser()
61+
62+
if attempt == 0:
63+
# Reset app
64+
print("Try reset the app and log out once...")
65+
66+
# Relogin (optional: only if the button is present)
67+
try:
68+
self.get_altdriver().wait_for_object(By.NAME, "ReloginBtn", timeout=10).tap()
69+
# Wait for authenticated screen
70+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene", timeout=20)
71+
print("Re-logged in")
72+
except Exception as e:
73+
print(f"ReloginBtn not found or timeout waiting for AuthenticatedScene: {e}")
74+
print("Skipping relogin step. User may already be in AuthenticatedScene or not logged in.")
75+
continue
76+
77+
# Logout
78+
print("Logging out...")
79+
self.launch_browser()
80+
self.bring_app_to_foreground()
81+
82+
try:
83+
self.get_altdriver().find_object(By.NAME, "LogoutBtn").tap()
84+
time.sleep(5)
85+
self.bring_app_to_foreground()
86+
87+
# Wait for unauthenticated screen
88+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
89+
print("Logged out and successfully reset app")
90+
except Exception as e:
91+
print(f"Error during logout: {e}")
92+
finally:
93+
self.stop_browser()
94+
time.sleep(5)
95+
else:
96+
raise SystemExit(f"Failed to reset app {err}")
97+
98+
def test_1a_pkce_login(self):
99+
self.login(True)
100+
101+
def test_1b_device_code_login(self):
102+
self.restart_app_and_altdriver()
103+
self.login(False)
104+
105+
def test_2_other_functions(self):
106+
self.test_0_other_functions()
107+
108+
def test_3_passport_functions(self):
109+
self.test_1_passport_functions()
110+
111+
def test_4_imx_functions(self):
112+
self.test_2_imx_functions()
113+
114+
def test_5_zkevm_functions(self):
115+
self.test_3_zkevm_functions()
116+
117+
def test_6_relogin(self):
118+
self.restart_app_and_altdriver()
119+
120+
# Select use device code auth
121+
self.select_auth_type(use_pkce=False)
122+
123+
# Relogin
124+
print("Re-logging in...")
125+
try:
126+
self.get_altdriver().wait_for_object(By.NAME, "ReloginBtn", timeout=10).tap()
127+
128+
# Wait for authenticated screen
129+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
130+
print("Re-logged in")
131+
132+
# Get access token
133+
self.get_altdriver().find_object(By.NAME, "GetAccessTokenBtn").tap()
134+
output = self.get_altdriver().find_object(By.NAME, "Output")
135+
self.assertTrue(len(output.get_text()) > 50)
136+
137+
# Click Connect to IMX button
138+
self.get_altdriver().find_object(By.NAME, "ConnectBtn").tap()
139+
self.assertEqual("Connected to IMX", output.get_text())
140+
except Exception as e:
141+
print(f"Error during relogin: {e}")
142+
raise
143+
144+
def test_7_reconnect_device_code_connect_imx(self):
145+
self.restart_app_and_altdriver()
146+
147+
# Select use device code auth
148+
self.select_auth_type(use_pkce=False)
149+
150+
# Reconnect
151+
print("Reconnecting...")
152+
try:
153+
self.get_altdriver().wait_for_object(By.NAME, "ReconnectBtn", timeout=10).tap()
154+
155+
# Wait for authenticated screen
156+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
157+
print("Reconnected")
158+
159+
# Get access token
160+
self.get_altdriver().find_object(By.NAME, "GetAccessTokenBtn").tap()
161+
output = self.get_altdriver().find_object(By.NAME, "Output")
162+
self.assertTrue(len(output.get_text()) > 50)
163+
164+
# Get address without having to click Connect to IMX button
165+
self.get_altdriver().find_object(By.NAME, "GetAddressBtn").tap()
166+
self.assertEqual(TestConfig.WALLET_ADDRESS, output.get_text())
167+
except Exception as e:
168+
print(f"Error during reconnect: {e}")
169+
raise
170+
171+
# Logout
172+
print("Logging out...")
173+
self.launch_browser()
174+
self.bring_app_to_foreground()
175+
176+
try:
177+
self.get_altdriver().find_object(By.NAME, "LogoutBtn").tap()
178+
time.sleep(5)
179+
self.bring_app_to_foreground()
180+
181+
# Wait for authenticated screen
182+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
183+
print("Logged out")
184+
except Exception as e:
185+
print(f"Error during logout: {e}")
186+
finally:
187+
self.stop_browser()
188+
189+
# Connect IMX
190+
print("Logging in and connecting to IMX...")
191+
self.launch_browser()
192+
self.bring_app_to_foreground()
193+
194+
try:
195+
self.get_altdriver().wait_for_object(By.NAME, "ConnectBtn").tap()
196+
self.helper_login(use_pkce=False)
197+
self.bring_app_to_foreground()
198+
199+
# Wait for authenticated screen
200+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
201+
print("Logged in and connected to IMX")
202+
self.stop_browser()
203+
204+
# Get access token
205+
self.get_altdriver().find_object(By.NAME, "GetAccessTokenBtn").tap()
206+
output = self.get_altdriver().find_object(By.NAME, "Output")
207+
self.assertTrue(len(output.get_text()) > 50)
208+
209+
# Get address without having to click Connect to IMX button
210+
self.get_altdriver().find_object(By.NAME, "GetAddressBtn").tap()
211+
self.assertEqual(TestConfig.WALLET_ADDRESS, output.get_text())
212+
except Exception as e:
213+
print(f"Error during IMX connect: {e}")
214+
raise
215+
216+
# Logout
217+
print("Logging out...")
218+
self.launch_browser()
219+
self.bring_app_to_foreground()
220+
221+
try:
222+
self.get_altdriver().find_object(By.NAME, "LogoutBtn").tap()
223+
time.sleep(5)
224+
self.bring_app_to_foreground()
225+
226+
# Wait for authenticated screen
227+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
228+
print("Logged out")
229+
except Exception as e:
230+
print(f"Error during final logout: {e}")
231+
finally:
232+
self.stop_browser()

0 commit comments

Comments
 (0)