@@ -86,6 +86,53 @@ def get_auth_url_from_unity_logs():
8686 print ("No auth URL found in Unity logs" )
8787 return None
8888
89+ def handle_cached_authentication (driver ):
90+ """Handle scenarios where user is already authenticated (cached session)"""
91+ print ("Handling cached authentication scenario..." )
92+ print (f"Current URL: { driver .current_url } " )
93+ print (f"Page title: { driver .title } " )
94+
95+ # Give a moment for any page transitions to complete
96+ time .sleep (3 )
97+
98+ # Handle deep link processing based on environment
99+ is_ci = os .getenv ('CI' ) or os .getenv ('GITHUB_ACTIONS' ) or os .getenv ('BUILD_ID' )
100+
101+ if is_ci :
102+ print ("CI environment - checking if authentication completed automatically" )
103+ print ("Monitoring Unity logs for authentication completion..." )
104+
105+ auth_success = False
106+ for check_attempt in range (30 ): # Check for 30 seconds
107+ try :
108+ with open ("C:\\ Users\\ WindowsBuildsdkServi\\ AppData\\ LocalLow\\ Immutable\\ Immutable Sample\\ Player.log" , 'r' , encoding = 'utf-8' , errors = 'ignore' ) as f :
109+ content = f .read ()
110+ # Look for signs of successful authentication
111+ if any (phrase in content for phrase in [
112+ "AuthenticatedScene" ,
113+ "authentication successful" ,
114+ "logged in successfully" ,
115+ "Passport token received"
116+ ]):
117+ print ("Authentication success detected in Unity logs!" )
118+ auth_success = True
119+ break
120+ except :
121+ pass
122+ time .sleep (1 )
123+
124+ if not auth_success :
125+ print ("No authentication success detected - attempting automated dialog handling" )
126+ # Add automated dialog clicking here if needed
127+
128+ else :
129+ print ("Local environment - cached authentication should work automatically" )
130+ print ("Waiting for Unity to receive the deep link callback..." )
131+ time .sleep (5 )
132+ print ("Cached authentication processing complete" )
133+
134+ return # Exit since cached auth is complete
135+
89136def login ():
90137 print ("Connect to Chrome" )
91138 # Set up Chrome options to connect to the existing Chrome instance
@@ -94,155 +141,79 @@ def login():
94141 # Connect to the existing Chrome instance
95142 driver = webdriver .Chrome (options = chrome_options )
96143
97- print ("Looking for auth URL in Unity logs..." )
98- # Try to get the auth URL from Unity logs instead of waiting for new window
99- auth_url = None
100- for attempt in range (30 ): # Try for 30 seconds
101- auth_url = get_auth_url_from_unity_logs ()
102- if auth_url :
103- break
104- time .sleep (1 )
144+ # HYBRID APPROACH: Try multi-window detection first (proven to work in CI),
145+ # then fall back to Unity log monitoring if needed
105146
106- if auth_url :
107- print (f"Navigating to captured auth URL: { auth_url } " )
108- driver .get (auth_url )
109- # We're already on the auth page, no need to find windows
110- target_window = driver .current_window_handle
111-
112- # Debug: Check what page we landed on
113- time .sleep (3 ) # Let page load
114- print (f"After navigation - URL: { driver .current_url } " )
115- print (f"After navigation - Title: { driver .title } " )
116-
117- # Check if we have email field (login page) or if we skipped to redirect
118- email_fields = driver .find_elements (By .ID , ':r1:' )
119- if not email_fields :
120- print ("No email field found - likely skipped to auth complete, looking for deep link dialog..." )
121- # Skip directly to deep link handling
122- # Handle deep link permission dialog
123- print ("Waiting for deep link permission dialog..." )
124- print (f"Current URL: { driver .current_url } " )
125- print (f"Page title: { driver .title } " )
126-
127- # Give a moment for any page transitions to complete
128- time .sleep (3 )
129-
130- # Handle deep link processing based on environment
131- is_ci = os .getenv ('CI' ) or os .getenv ('GITHUB_ACTIONS' ) or os .getenv ('BUILD_ID' )
132-
133- if is_ci :
134- print ("CI environment - checking if authentication completed automatically" )
135- # In CI, the browser window may close immediately if auth succeeds
136- # Check Unity logs to see if authentication was successful
137- print ("Monitoring Unity logs for authentication completion..." )
138-
139- auth_success = False
140- for check_attempt in range (30 ): # Check for 30 seconds
141- try :
142- with open ("C:\\ Users\\ WindowsBuildsdkServi\\ AppData\\ LocalLow\\ Immutable\\ Immutable Sample\\ Player.log" , 'r' , encoding = 'utf-8' , errors = 'ignore' ) as f :
143- content = f .read ()
144- # Look for signs of successful authentication
145- if any (phrase in content for phrase in [
146- "AuthenticatedScene" ,
147- "authentication successful" ,
148- "logged in successfully" ,
149- "Passport token received"
150- ]):
151- print ("Authentication success detected in Unity logs!" )
152- auth_success = True
153- break
154- except :
155- pass
156- time .sleep (1 )
157-
158- if not auth_success :
159- print ("No authentication success detected - attempting automated dialog handling" )
160- print ("Looking for protocol permission dialog to click automatically..." )
161-
162- # Try to find and click the protocol dialog automatically
163- try :
164- ps_script = '''
165- for ($i = 0; $i -lt 10; $i++) {
166- $windows = Get-Process | Where-Object { $_.MainWindowTitle -like "*auth.immutable.com*" -or $_.MainWindowTitle -like "*Open*" }
167- foreach ($window in $windows) {
168- try {
169- Add-Type -AssemblyName UIAutomationClient
170- $element = [Windows.Automation.AutomationElement]::FromHandle($window.MainWindowHandle)
171- if ($element) {
172- $buttons = $element.FindAll([Windows.Automation.TreeScope]::Descendants,
173- [Windows.Automation.Condition]::new([Windows.Automation.AutomationElement]::ControlTypeProperty,
174- [Windows.Automation.ControlType]::Button))
175- foreach ($button in $buttons) {
176- $buttonText = $button.Current.Name
177- if ($buttonText -like "*Open*" -or $buttonText -like "*Allow*" -or $buttonText -like "*Yes*") {
178- $button.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern).Invoke()
179- Write-Host "Clicked protocol dialog button: $buttonText"
180- exit 0
181- }
182- }
183- }
184- } catch {}
185- }
186- Start-Sleep 1
187- }
188- Write-Host "No protocol dialog found"
189- '''
190-
191- result = subprocess .run (["powershell" , "-Command" , ps_script ],
192- capture_output = True , text = True , timeout = 15 )
193- if "Clicked protocol dialog" in result .stdout :
194- print ("Successfully automated protocol dialog click in CI!" )
195- # Wait a bit more for Unity to process
196- time .sleep (5 )
197- else :
198- print ("Could not find protocol dialog to automate" )
199- except Exception as e :
200- print (f"CI dialog automation error: { e } " )
201- print ("Protocol dialog may require manual setup in CI environment" )
202-
203- else :
204- print ("Local environment - protocol association should work automatically" )
205- print ("Waiting for Unity to receive the deep link callback..." )
206- time .sleep (5 )
207- print ("Deep link processing complete - authentication should be successful" )
208-
209- return # Exit function since we're done
210- else :
211- print ("Could not find auth URL in Unity logs, falling back to window waiting..." )
212- # Fallback to original approach
147+ print ("Attempting multi-window detection (primary method - proven to work)..." )
148+ try :
149+ # Wait for Unity to open auth URL in new browser window
213150 print ("Waiting for new window..." )
214- WebDriverWait (driver , 60 ).until (EC .number_of_windows_to_be (2 ))
215-
151+ WebDriverWait (driver , 15 ).until (EC .number_of_windows_to_be (2 ))
152+
216153 # Get all window handles
217154 all_windows = driver .window_handles
218- print (f"Found { len (all_windows )} new windows to check: { all_windows } " )
219-
155+ print (f"Found { len (all_windows )} windows to check: { all_windows } " )
156+
220157 # Find the window with email input
221158 target_window = None
222159 for window in all_windows :
223160 try :
224161 print (f"Checking window: { window } " )
225162 driver .switch_to .window (window )
226- driver .find_element (By .ID , ':r1:' )
163+ # Try to find email input in this window
164+ email_field = driver .find_element (By .CSS_SELECTOR , '[data-testid="TextInput__input"]' )
227165 target_window = window
228166 print (f"Found email input in window: { window } " )
229167 break
230168 except :
231169 print (f"Email input not found in window: { window } , trying next..." )
232170 continue
233171
234- if not target_window :
235- print ("Could not find email input field in any window!" )
172+ if target_window :
173+ print ("Switch to the target window" )
174+ driver .switch_to .window (target_window )
175+ print ("Multi-window detection successful - proceeding with login flow" )
176+ else :
177+ raise Exception ("No window with email input found" )
178+
179+ except Exception as e :
180+ print (f"Multi-window detection failed: { e } " )
181+ print ("Falling back to Unity log monitoring method..." )
182+
183+ # FALLBACK: Unity log monitoring approach
184+ print ("Looking for auth URL in Unity logs..." )
185+ auth_url = None
186+ for attempt in range (30 ): # Try for 30 seconds
187+ auth_url = get_auth_url_from_unity_logs ()
188+ if auth_url :
189+ break
190+ time .sleep (1 )
191+
192+ if auth_url :
193+ print (f"Navigating to captured auth URL: { auth_url } " )
194+ driver .get (auth_url )
195+
196+ # Debug: Check what page we landed on
197+ time .sleep (3 ) # Let page load
198+ print (f"After navigation - URL: { driver .current_url } " )
199+ print (f"After navigation - Title: { driver .title } " )
200+
201+ # Check if we have email field (login page) or if we skipped to redirect
202+ try :
203+ email_field = driver .find_element (By .CSS_SELECTOR , '[data-testid="TextInput__input"]' )
204+ print ("Found email field via Unity log method - proceeding with login flow" )
205+ except :
206+ print ("No email field found - likely cached session, handling deep link..." )
207+ return handle_cached_authentication (driver )
208+ else :
209+ print ("Could not find auth URL in Unity logs either!" )
236210 driver .quit ()
237211 return
238212
239- print ("Switch to the target window" )
240- driver .switch_to .window (target_window )
241-
242213 wait = WebDriverWait (driver , 60 )
243214
244215 print ("Wait for email input..." )
245- email_field = wait .until (EC .presence_of_element_located ((By .ID , ':r1: ' )))
216+ email_field = wait .until (EC .presence_of_element_located ((By .CSS_SELECTOR , '[data-testid="TextInput__input"] ' )))
246217 print ("Enter email..." )
247218 email_field .send_keys (EMAIL )
248219
0 commit comments