33from pypdf import PdfReader
44from playwright .sync_api import Page , expect
55import oracledb
6+ import pandas as pd
7+ import sys
8+ sys .path .append ("../utils" )
9+ from utils .oracle import *
610
711@pytest .mark .wip
812def test_example (page : Page ) -> None :
@@ -33,10 +37,6 @@ def test_example(page: Page) -> None:
3337 page .get_by_role ("link" , name = "Call and Recall" ).click ()
3438 page .get_by_role ("link" , name = "Generate Invitations" ).click ()
3539 page .get_by_role ("button" , name = "Generate Invitations" ).click ()
36- # #TODO Add loop for below steps
37- # expect(page.locator("#displayRS")).to_contain_text("Queued")
38- # page.get_by_role("button", name="Refresh").click()
39- # expect(page.locator("#displayRS")).to_contain_text("Completed")
4040
4141 page .wait_for_selector ("#displayRS" , timeout = 5000 )
4242
@@ -56,17 +56,31 @@ def test_example(page: Page) -> None:
5656 page .get_by_role ("button" , name = "Refresh" ).click ()
5757 page .wait_for_timeout (wait_interval )
5858 elapsed += wait_interval
59+ elif "Failed" in table_text :
60+ pytest .fail ("Invitation has failed to generate" )
5961 else :
6062 break
6163
6264 # Final check: ensure that the table now contains "Completed"
6365 expect (page .locator ("#displayRS" )).to_contain_text ("Completed" )
6466
65- active_batch_processing (page , "S1" , "Pre-invitation (FIT)" )
66- active_batch_processing (page , "S1" , "Pre-invitation (FIT) (digital leaflet)" )
67-
68- def active_batch_processing (page : Page , batch_type : str , batch_description : str ) -> None :
6967 # Print the batch of Pre-Invitation Letters
68+ active_batch_processing (page , "S1" , "Pre-invitation (FIT)" , "S9 - Pre-invitation Sent" )
69+ active_batch_processing (page , "S1" , "Pre-invitation (FIT) (digital leaflet)" , "S9 - Pre-invitation Sent" )
70+
71+ database_connection_exec ("bcss_timed_events" )
72+
73+ # Print the batch of Invitation & Test Kit Letters
74+ active_batch_processing_csv (page , "S9" , "Invitation & Test Kit (FIT)" , "S10 - Invitation & Test Kit Sent" )
75+
76+ # Print a set of reminder letters
77+ active_batch_processing (page , "S10" , "Test Kit Reminder" , " S19 - Reminder of Initial Test Sent" )
78+
79+ # Log out
80+ page .get_by_role ("link" , name = "Log-out" ).click ()
81+ expect (page .get_by_role ("heading" , name = "You have logged out" )).to_be_visible ()
82+
83+ def active_batch_processing (page : Page , batch_type : str , batch_description : str , latest_event_status : str ) -> None :
7084 page .get_by_role ("link" , name = "Main Menu" ).click ()
7185 page .get_by_role ("link" , name = "Communications Production" ).click ()
7286 page .get_by_role ("link" , name = "Active Batch List" ).click ()
@@ -119,7 +133,7 @@ def active_batch_processing(page: Page, batch_type: str, batch_description: str)
119133
120134 page .on ("dialog" , lambda dialog : dialog .accept ())
121135 page .get_by_role ("button" , name = "Confirm Printed" ).click ()
122- expect ( page . get_by_text ( "Batch Successfully Archived" )). to_be_visible ()
136+
123137 page .get_by_role ("link" , name = "Back" ).click ()
124138 page .get_by_role ("link" , name = "Archived Batch List" ).click ()
125139 page .locator ("#batchIdFilter" ).click ()
@@ -134,3 +148,91 @@ def active_batch_processing(page: Page, batch_type: str, batch_description: str)
134148 page .get_by_role ("textbox" , name = "NHS Number" ).press ("Enter" )
135149 page .get_by_role ("button" , name = "Search" ).click ()
136150 expect (page .get_by_role ("cell" , name = "Subject Screening Summary" , exact = True )).to_be_visible ()
151+ expect (page .get_by_role ("cell" , name = "Latest Event Status" , exact = True )).to_be_visible ()
152+ expect (page .get_by_role ("cell" , name = latest_event_status , exact = True )).to_be_visible ()
153+
154+
155+ def active_batch_processing_csv (page : Page , batch_type : str , batch_description : str , latest_event_status : str ) -> None :
156+ # Print the batch of Pre-Invitation Letters
157+ page .get_by_role ("link" , name = "Main Menu" ).click ()
158+ page .get_by_role ("link" , name = "Communications Production" ).click ()
159+ page .get_by_role ("link" , name = "Active Batch List" ).click ()
160+ page .locator ("#eventCodeFilter" ).click ()
161+ page .locator ("#eventCodeFilter" ).fill (batch_type )
162+ page .locator ("#eventCodeFilter" ).press ("Enter" )
163+ pre_invitation_cells = page .locator (f"//td[text()='{ batch_description } ']" )
164+
165+ for i in range (pre_invitation_cells .count ()):
166+ row = pre_invitation_cells .nth (i ).locator (".." ) # Get the parent row
167+
168+ # Check if the row contains "Prepared" or "Open"
169+ if row .locator ("td" , has_text = "Prepared" ).count () > 0 or row .locator ("td" , has_text = "Open" ).count () > 0 or row .locator ("td" , has_text = "Closed" ).count () > 0 :
170+ # Find the first link in that row and click it
171+ link = row .locator ("a" ).first
172+ link_text = link .inner_text () # Get the link text dynamically
173+ link .click ()
174+
175+ # Checks to see if batch is already prepared
176+ page .wait_for_timeout (3000 ) # Without this timeout prepare_button is always set to false
177+ prepare_button = page .get_by_role ("button" , name = "Prepare Batch" ).is_visible ()
178+
179+ #If not prepared it will click on the prepare button and wait 5 seconds (ideally would be a loop)
180+ if prepare_button :
181+ page .get_by_role ("button" , name = "Prepare Batch" ).click ()
182+ page .wait_for_timeout (5000 )
183+
184+ # Start waiting for the pdf download
185+ with page .expect_download () as download_info :
186+ # Perform the action that initiates download
187+ page .locator ("[id=\" \\ 32 8336\" ]" ).get_by_role ("button" , name = "Retrieve" ).click () # Needs to be changed, locator ID is dynamic and changes each run
188+ download_pdf = download_info .value
189+ # Wait for the download process to complete and save the downloaded file in a temp folder
190+ download_pdf .save_as (f"/temp/{ download_pdf .suggested_filename } " )
191+
192+ reader = PdfReader (f"/temp/{ download_pdf .suggested_filename } " )
193+
194+ # For loop looping through all pages of the file to find the NHS Number
195+ for pages in reader .pages :
196+ text = pages .extract_text ()
197+ if "NHS No" in text :
198+ # If NHS number is found split the text by every new line into a list
199+ text = text .splitlines (True )
200+ for split_text in text :
201+ if "NHS No" in split_text :
202+ # If a string is found containing "NHS No" all characters but digits are stored into nhs_no
203+ nhs_no_pdf = res = "" .join ([ele for ele in split_text if ele .isdigit ()])
204+ break
205+
206+ page .on ("dialog" , lambda dialog : dialog .accept ())
207+ expect (page .locator ("[id=\" \\ 32 8336\" ]" ).get_by_role ("button" , name = "Confirm Printed" )).to_be_visible () # Needs to be changed, locator ID is dynamic and changes each run
208+
209+ # Start waiting for the csv download
210+ with page .expect_download () as download_info :
211+ # Perform the action that initiates download
212+ page .locator ("[id=\" \\ 32 8337\" ]" ).get_by_role ("button" , name = "Retrieve" ).click () # Needs to be changed, locator ID is dynamic and changes each run
213+ download_csv = download_info .value
214+ # Wait for the download process to complete and save the downloaded file in a temp folder
215+ download_csv .save_as (f"/temp/{ download_csv .suggested_filename } " )
216+
217+ page .on ("dialog" , lambda dialog : dialog .accept ())
218+ expect (page .locator ("[id=\" \\ 32 8337\" ]" ).get_by_role ("button" , name = "Confirm Printed" )).to_be_visible () # Needs to be changed, locator ID is dynamic and changes each run
219+
220+ # Storing the downloaded csv into a pandas dataframe
221+ csv_df = pd .read_csv (download_csv .suggested_filename )
222+
223+ page .get_by_role ("link" , name = "Back" ).click ()
224+ page .get_by_role ("link" , name = "Archived Batch List" ).click ()
225+ page .locator ("#batchIdFilter" ).click ()
226+ page .locator ("#batchIdFilter" ).fill (link_text )
227+ page .locator ("#batchIdFilter" ).press ("Enter" )
228+ expect (page .locator ("td" ).filter (has_text = link_text )).to_be_visible ()
229+
230+ page .get_by_role ("link" , name = "Main Menu" ).click ()
231+ page .get_by_role ("link" , name = "Screening Subject Search" ).click ()
232+ page .get_by_role ("textbox" , name = "NHS Number" ).click ()
233+ page .get_by_role ("textbox" , name = "NHS Number" ).fill (nhs_no_pdf )
234+ page .get_by_role ("textbox" , name = "NHS Number" ).press ("Enter" )
235+ page .get_by_role ("button" , name = "Search" ).click ()
236+ expect (page .get_by_role ("cell" , name = "Subject Screening Summary" , exact = True )).to_be_visible ()
237+ expect (page .get_by_role ("cell" , name = "Latest Event Status" , exact = True )).to_be_visible ()
238+ expect (page .get_by_role ("cell" , name = latest_event_status , exact = True )).to_be_visible ()
0 commit comments