diff --git a/apps/SeleniumService/selenium_CMSP_eligibilityHistoryRemainingCheckWorker.py b/apps/SeleniumService/selenium_CMSP_eligibilityHistoryRemainingCheckWorker.py index ce83f514..83aaa212 100644 --- a/apps/SeleniumService/selenium_CMSP_eligibilityHistoryRemainingCheckWorker.py +++ b/apps/SeleniumService/selenium_CMSP_eligibilityHistoryRemainingCheckWorker.py @@ -370,28 +370,26 @@ class AutomationCMSPEligibilityHistoryRemainingCheck: return f"ERROR:STEP6:{substep}" # ── step 7 — print accumulator PDF ────────────────────────────────────────── + # We wait for ng-if="vm.hasResults" button to become visible — that confirms + # Angular has finished loading the accumulator data — then CDP-print directly + # to avoid the new-tab timing race that produces an empty PDF. def step7_accumulator_pdf(self): - wait = WebDriverWait(self.driver, 30) - current_tab = self.driver.current_window_handle + wait = WebDriverWait(self.driver, 60) try: - # ng-click="vm.printResults()" and ng-if="vm.hasResults" identify this button - print_button = wait.until( - EC.element_to_be_clickable( + # Wait for the page to finish loading + wait.until(lambda d: d.execute_script("return document.readyState") == "complete") + + # Wait for the "Printer Friendly Format" button to appear — it is + # wrapped in ng-if="vm.hasResults", so it only renders once the + # accumulator data has fully loaded from the server. + wait.until( + EC.visibility_of_element_located( (By.CSS_SELECTOR, "button.btn.btn-primary[ng-click='vm.printResults()']") ) ) - print_button.click() - - try: - WebDriverWait(self.driver, 10).until(lambda d: len(d.window_handles) > 1) - new_tabs = [t for t in self.driver.window_handles if t != current_tab] - self.driver.switch_to.window(new_tabs[0]) - wait.until(lambda d: d.execute_script("return document.readyState") == "complete") - wait.until(EC.presence_of_element_located((By.TAG_NAME, "body"))) - time.sleep(2) - except TimeoutException: - print("No new tab for accumulator; printing current page directly") + print("[step7] accumulator data loaded (vm.hasResults is true)") + time.sleep(2) # brief pause for any final Angular digest cycle safe_member = "".join(c for c in str(self.memberId) if c.isalnum() or c in "-_.") pdf_filename = f"cmsp_accumulator_{safe_member}.pdf"