@@ -28,61 +28,114 @@ def restart_app_and_altdriver(self):
28
28
self .__class__ .altdriver = AltDriver (timeout = 120 )
29
29
30
30
def login (self ):
31
- # Wait for unauthenticated screen
32
- self .get_altdriver ().wait_for_current_scene_to_be ("UnauthenticatedScene" )
33
-
34
- for attempt in range (2 ):
31
+ """
32
+ Smart login method that handles different app states:
33
+ - UnauthenticatedScene: Proceed with normal login
34
+ - AuthenticatedScene: Logout first, then login
35
+ - Other scenes: Wait for proper state
36
+ """
37
+ print ("=== SMART LOGIN: Checking app state ===" )
38
+
39
+ # Check what scene we're starting in
40
+ try :
41
+ current_scene = self .get_altdriver ().get_current_scene ()
42
+ print (f"Current scene: { current_scene } " )
43
+ except Exception as e :
44
+ print (f"Could not get current scene: { e } " )
45
+ raise SystemExit ("Failed to determine app state" )
46
+
47
+ # Handle different starting states
48
+ if current_scene == "UnauthenticatedScene" :
49
+ print ("[OK] App is already unauthenticated - proceeding with login" )
50
+ self ._perform_login ()
51
+
52
+ elif current_scene == "AuthenticatedScene" :
53
+ print ("[WARNING] App is already authenticated - need to logout first" )
54
+ self ._logout_and_login ()
55
+
56
+ else :
57
+ print (f"[ERROR] Unexpected scene: { current_scene } " )
58
+ # Try to wait for a known state
59
+ print ("Waiting for app to reach a known state..." )
60
+ for wait_attempt in range (3 ):
61
+ try :
62
+ current_scene = self .get_altdriver ().get_current_scene ()
63
+ if current_scene in ["UnauthenticatedScene" , "AuthenticatedScene" ]:
64
+ print (f"App reached known state: { current_scene } " )
65
+ return self .login () # Recursive call with known state
66
+ time .sleep (5 )
67
+ except Exception as e :
68
+ print (f"Wait attempt { wait_attempt + 1 } failed: { e } " )
69
+
70
+ raise SystemExit (f"App stuck in unknown scene: { current_scene } " )
71
+
72
+ def _perform_login (self ):
73
+ """Perform normal login flow when app is in UnauthenticatedScene"""
74
+ try :
75
+ # Check for login button
76
+ login_button = self .get_altdriver ().find_object (By .NAME , "LoginBtn" )
77
+ print ("Found login button - performing login" )
78
+
79
+ # Login
80
+ launch_browser ()
81
+ bring_sample_app_to_foreground ()
82
+ login_button .tap ()
83
+ login ()
84
+ bring_sample_app_to_foreground ()
85
+
86
+ # Wait for authenticated screen
87
+ self .get_altdriver ().wait_for_current_scene_to_be ("AuthenticatedScene" )
88
+ stop_browser ()
89
+ print ("[SUCCESS] Login successful" )
90
+
91
+ except Exception as err :
92
+ stop_browser ()
93
+ raise SystemExit (f"Login failed: { err } " )
94
+
95
+ def _logout_and_login (self ):
96
+ """Handle logout and then login when app starts authenticated"""
97
+ print ("Attempting logout to reset to unauthenticated state..." )
98
+
99
+ try :
100
+ # Use our improved logout method
101
+ print ("Using controlled browser logout..." )
102
+ logout_with_controlled_browser ()
103
+
104
+ # Wait for unauthenticated state
105
+ print ("Waiting for UnauthenticatedScene after logout..." )
106
+ self .get_altdriver ().wait_for_current_scene_to_be ("UnauthenticatedScene" , timeout = 30 )
107
+ print ("[SUCCESS] Successfully logged out" )
108
+
109
+ # Now perform normal login
110
+ self ._perform_login ()
111
+
112
+ except Exception as logout_err :
113
+ print (f"Controlled logout failed: { logout_err } " )
114
+ print ("Trying fallback logout method..." )
115
+
35
116
try :
36
- # Check app state
37
- login_button = self .get_altdriver ().find_object (By .NAME , "LoginBtn" )
38
- print ("Found login button, app is in the correct state" )
39
-
40
- # Login
41
- print ("Logging in..." )
117
+ # Fallback: Direct logout button approach
42
118
launch_browser ()
43
119
bring_sample_app_to_foreground ()
44
- login_button .tap ()
45
- login ()
120
+ logout_button = self .get_altdriver ().find_object (By .NAME , "LogoutBtn" )
121
+ logout_button .tap ()
122
+ time .sleep (10 ) # Give more time for logout
46
123
bring_sample_app_to_foreground ()
47
-
48
- # Wait for authenticated screen
49
- self .get_altdriver ().wait_for_current_scene_to_be ("AuthenticatedScene" )
124
+
125
+ # Wait for unauthenticated screen
126
+ self .get_altdriver ().wait_for_current_scene_to_be ("UnauthenticatedScene" , timeout = 30 )
50
127
stop_browser ()
51
- print ("Logged in" )
52
- return
53
- except Exception as err :
128
+ print ("[SUCCESS] Fallback logout successful" )
129
+
130
+ # Now perform normal login
131
+ self ._perform_login ()
132
+
133
+ except Exception as fallback_err :
54
134
stop_browser ()
55
-
56
- if attempt == 0 :
57
- # Reset app
58
-
59
- # Relogin (optional: only if the button is present)
60
- print ("Try reset the app and log out once..." )
61
- try :
62
- self .get_altdriver ().wait_for_object (By .NAME , "ReloginBtn" ).tap ()
63
- except Exception as e :
64
- print ("ReloginBtn not found, skipping relogin step. User may already be in AuthenticatedScene." )
65
-
66
- # Wait for authenticated screen
67
- self .get_altdriver ().wait_for_current_scene_to_be ("AuthenticatedScene" )
68
- print ("Re-logged in" )
69
-
70
- # Logout
71
- print ("Logging out..." )
72
- launch_browser ()
73
- bring_sample_app_to_foreground ()
74
- self .get_altdriver ().find_object (By .NAME , "LogoutBtn" ).tap ()
75
- time .sleep (5 )
76
- bring_sample_app_to_foreground ()
77
-
78
- # Wait for unauthenticated screen
79
- self .get_altdriver ().wait_for_current_scene_to_be ("UnauthenticatedScene" )
80
- stop_browser ()
81
- print ("Logged out and successfully reset app" )
82
-
83
- time .sleep (5 )
84
- else :
85
- raise SystemExit (f"Failed to reset app { err } " )
135
+ print (f"[ERROR] Both logout methods failed:" )
136
+ print (f" - Controlled logout: { logout_err } " )
137
+ print (f" - Fallback logout: { fallback_err } " )
138
+ raise SystemExit ("Could not logout to reset app state" )
86
139
87
140
def test_1_login (self ):
88
141
print ("=" * 60 )
0 commit comments