44from pages .active_batch_list_page import ActiveBatchList
55from pages .manage_active_batch_page import ManageActiveBatch
66from pages .archived_batch_list_page import ArchivedBatchList
7- from utils .pdf_reader import extract_nhs_no_from_pdf
87from utils .screening_subject_page_searcher import verify_subject_event_status_by_nhs_no
98from utils .get_nhs_no_from_batch_id import get_nhs_no_from_batch_id
109import os
1110import pytest
1211from playwright .sync_api import Page
12+ import logging
1313
1414def batch_processing (page : Page , batch_type : str , batch_description : str , latest_event_status : str ):
15+ logging .info (f"Processing { batch_type } - { batch_description } batch" )
1516 NavigationBar (page ).click_main_menu_link ()
1617 MainMenu (page ).go_to_communications_production_page ()
1718 CommunicationsProduction (page ).go_to_active_batch_list_page ()
@@ -20,59 +21,81 @@ def batch_processing(page: Page, batch_type: str, batch_description: str, latest
2021 batch_description_cells = page .locator (f"//td[text()='{ batch_description } ']" )
2122
2223 if batch_description_cells .count () == 0 and batch_description == "Pre-invitation (FIT) (digital leaflet)" :
23- print ( f "No S1 Pre-invitation (FIT) (digital leaflet) batch found. Skipping to next step" )
24+ logging . warning ( "No S1 Pre-invitation (FIT) (digital leaflet) batch found. Skipping to next step" )
2425 return
2526 elif batch_description_cells .count () == 0 and page .locator ("td" , has_text = "No matching records found" ):
2627 pytest .fail (f"No { batch_type } { batch_description } batch found" )
2728
2829 for i in range (batch_description_cells .count ()):
2930 row = batch_description_cells .nth (i ).locator (".." ) # Get the parent row
3031
31- # Check if the row contains "Prepared" or " Open"
32- if row .locator ("td" , has_text = "Prepared" ). count () > 0 or row . locator ( "td" , has_text = " Open" ).count () > 0 :
32+ # Check if the row contains "Open"
33+ if row .locator ("td" , has_text = "Open" ).count () > 0 :
3334 # Find the first link in that row and click it
3435 link = row .locator ("a" ).first
3536 link_text = link .inner_text () # Get the batch id dynamically
36- nhs_no_df = get_nhs_no_from_batch_id (link_text )
37+ try :
38+ logging .info (f"Attempting to get NHS Numbers for batch: { link_text } from the DB" )
39+ nhs_no_df = get_nhs_no_from_batch_id (link_text )
40+ logging .info (f"Successfully retrieved NHS Numbers from batch: { link_text } " )
41+ except Exception as e :
42+ logging .error (f"Failed to retrieve NHS Numbers from batch: { link_text } " )
3743 link .click ()
3844 break
3945 else :
40- pytest .fail (f"No open/prepared '{ batch_type } - { batch_description } ' batch found" )
46+ pytest .fail (f"No open '{ batch_type } - { batch_description } ' batch found" )
4147
4248 ManageActiveBatch (page ).click_prepare_button ()
4349 page .wait_for_timeout (1000 ) # This one second timeout does not affect the time to execute, as it is just used to ensure the reprepare batch button is clicked and does not instantly advance to the next step
4450 ManageActiveBatch (page ).reprepare_batch_text .wait_for ()
4551
4652 # This loops through each Retrieve button and clicks each one
4753 retrieve_button_count = 0
48- for retrieve_button in range (ManageActiveBatch (page ).retrieve_button .count ()):
49- retrieve_button_count += 1
50- # Start waiting for the pdf download
51- with page .expect_download () as download_info :
52- # Perform the action that initiates download
53- ManageActiveBatch (page ).retrieve_button .nth (retrieve_button - 1 ).click ()
54- download_file = download_info .value
55- file = download_file .suggested_filename
56- # Wait for the download process to complete and save the downloaded file in a temp folder
57- download_file .save_as (file )
58- if file .endswith (".pdf" ):
59- os .remove (file ) # Deletes the file after extracting the necessary data
60- elif file .endswith (".csv" ):
54+ try :
55+ for retrieve_button in range (ManageActiveBatch (page ).retrieve_button .count ()):
56+ retrieve_button_count += 1
57+ logging .info (f"Clicking retrieve button { retrieve_button_count } " )
58+ # Start waiting for the pdf download
59+ with page .expect_download () as download_info :
60+ # Perform the action that initiates download
61+ ManageActiveBatch (page ).retrieve_button .nth (retrieve_button - 1 ).click ()
62+ download_file = download_info .value
63+ file = download_file .suggested_filename
64+ # Wait for the download process to complete and save the downloaded file in a temp folder
65+ download_file .save_as (file )
6166 os .remove (file ) # Deletes the file after extracting the necessary data
67+ except Exception as e :
68+ logging .error (f"No retrieve button available to click: { str (e )} " )
6269
6370 # This loops through each Confirm printed button and clicks each one
64- for _ in range (retrieve_button_count ):
65- page .on ("dialog" , lambda dialog : dialog .accept ())
66- ManageActiveBatch (page ).confirm_button .nth (0 ).click ()
71+ try :
72+ for confirm_printed_button in range (retrieve_button_count ):
73+ logging .info (f"Clicking confirm printed button { confirm_printed_button + 1 } " )
74+ page .once ("dialog" , lambda dialog : dialog .accept ())
75+ ManageActiveBatch (page ).confirm_button .nth (0 ).click ()
76+ except Exception as e :
77+ logging .error (f"No confirm printed button available to click: { str (e )} " )
6778
68- ActiveBatchList (page ).batch_successfully_archived_msg .wait_for ()
79+ try :
80+ ActiveBatchList (page ).batch_successfully_archived_msg .wait_for ()
81+ logging .info (f"Batch { link_text } successfully archived" )
82+ except Exception as e :
83+ logging .error (f"Batch successfully archived message is not shown: { str (e )} " )
6984
7085 NavigationBar (page ).click_main_menu_link ()
7186 MainMenu (page ).go_to_communications_production_page ()
7287 CommunicationsProduction (page ).go_to_archived_batch_list_page ()
7388 ArchivedBatchList (page ).enter_id_filter (link_text )
74- ArchivedBatchList (page ).verify_table_data (link_text )
89+ try :
90+ ArchivedBatchList (page ).verify_table_data (link_text )
91+ logging .info (f"Batch { link_text } visible in archived batch list" )
92+ except Exception :
93+ logging .error (f"Batch { link_text } not visible in archived batch list" )
7594
7695 first_nhs_no = nhs_no_df ["subject_nhs_number" ].iloc [0 ]
77- verify_subject_event_status_by_nhs_no (page , first_nhs_no , latest_event_status )
96+ try :
97+ verify_subject_event_status_by_nhs_no (page , first_nhs_no , latest_event_status )
98+ logging .info (f"Successfully verified NHS number { first_nhs_no } with status { latest_event_status } " )
99+ except Exception as e :
100+ logging .error (f"Verification failed for NHS number { first_nhs_no } with status { latest_event_status } : { str (e )} " )
78101 return nhs_no_df
0 commit comments