Skip to content

Commit d61c201

Browse files
Refactoring the code for compartment 1
1 parent 06ef95f commit d61c201

File tree

1 file changed

+59
-116
lines changed

1 file changed

+59
-116
lines changed
Lines changed: 59 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import re
21
import pytest
32
from pypdf import PdfReader
43
from playwright.sync_api import Page, expect
5-
import oracledb
64
import pandas as pd
75
import sys
86
sys.path.append("../utils")
@@ -33,7 +31,7 @@ def test_example(page: Page) -> None:
3331
page.locator("#saveNote").get_by_role("button", name="Save").click()
3432

3533
# Generate Invitations
36-
page.get_by_role("link", name="Main Menu").click()
34+
main_menu_available(page)
3735
page.get_by_role("link", name="Call and Recall").click()
3836
page.get_by_role("link", name="Generate Invitations").click()
3937
page.get_by_role("button", name="Generate Invitations").click()
@@ -65,23 +63,23 @@ def test_example(page: Page) -> None:
6563
expect(page.locator("#displayRS")).to_contain_text("Completed")
6664

6765
# 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")
66+
batch_processing(page, "S1", "Pre-invitation (FIT)", "S9 - Pre-invitation Sent")
67+
batch_processing(page, "S1", "Pre-invitation (FIT) (digital leaflet)", "S9 - Pre-invitation Sent") # Sometimes it is "S10 - Invitation & Test Kit Sent"
7068

7169
database_connection_exec("bcss_timed_events")
7270

7371
# Print the batch of Invitation & Test Kit Letters
74-
active_batch_processing_csv(page, "S9", "Invitation & Test Kit (FIT)", "S10 - Invitation & Test Kit Sent")
72+
batch_processing(page, "S9", "Invitation & Test Kit (FIT)", "S10 - Invitation & Test Kit Sent")
7573

7674
# Print a set of reminder letters
77-
active_batch_processing(page, "S10", "Test Kit Reminder", " S19 - Reminder of Initial Test Sent")
75+
batch_processing(page, "S10", "Test Kit Reminder", "S19 - Reminder of Initial Test Sent")
7876

7977
# Log out
8078
page.get_by_role("link", name="Log-out").click()
8179
expect(page.get_by_role("heading", name="You have logged out")).to_be_visible()
8280

83-
def active_batch_processing(page: Page, batch_type: str, batch_description: str, latest_event_status: str) -> None:
84-
page.get_by_role("link", name="Main Menu").click()
81+
def batch_processing(page: Page, batch_type: str, batch_description: str, latest_event_status: str):
82+
main_menu_available(page)
8583
page.get_by_role("link", name="Communications Production").click()
8684
page.get_by_role("link", name="Active Batch List").click()
8785
page.locator("#eventCodeFilter").click()
@@ -98,99 +96,57 @@ def active_batch_processing(page: Page, batch_type: str, batch_description: str,
9896
link = row.locator("a").first
9997
link_text = link.inner_text() # Get the link text dynamically
10098
link.click()
99+
else:
100+
pytest.fail(f"No {batch_type} batch found")
101101

102102
# Checks to see if batch is already prepared
103103
page.wait_for_timeout(3000) # Without this timeout prepare_button is always set to false
104104
prepare_button = page.get_by_role("button", name="Prepare Batch").is_visible()
105105

106-
#If not prepared it will click on the prepare button and wait 5 seconds (ideally would be a loop)
106+
#If not prepared it will click on the prepare button
107107
if prepare_button:
108108
page.get_by_role("button", name="Prepare Batch").click()
109-
page.wait_for_timeout(5000)
110-
111-
# Start waiting for the download
112-
with page.expect_download() as download_info:
113-
# Perform the action that initiates download
114-
page.get_by_role("button", name="Retrieve").click()
115-
download = download_info.value
116-
117-
# Wait for the download process to complete and save the downloaded file in a temp folder
118-
download.save_as(f"/temp/{download.suggested_filename}")
119-
120-
reader = PdfReader(f"/temp/{download.suggested_filename}")
121-
122-
# For loop looping through all pages of the file to find the NHS Number
123-
for pages in reader.pages:
124-
text = pages.extract_text()
125-
if "NHS No" in text:
126-
# If NHS number is found split the text by every new line into a list
127-
text = text.splitlines(True)
128-
for split_text in text:
129-
if "NHS No" in split_text:
130-
# If a string is found containing "NHS No" all characters but digits are stored into nhs_no
131-
nhs_no = res = "".join([ele for ele in split_text if ele.isdigit()])
132-
break
133109

134-
page.on("dialog", lambda dialog: dialog.accept())
135-
page.get_by_role("button", name="Confirm Printed").click()
136-
137-
page.get_by_role("link", name="Back").click()
138-
page.get_by_role("link", name="Back").click()
110+
page.locator('text="Retrieve"').nth(0).wait_for()
111+
page.wait_for_timeout(5000) # This 5 second wait is to allow other Retrieve buttons to show as they do not show up at the same time
112+
113+
# This loops through each Retrieve button and clicks each one
114+
for retrieve_button in range (page.get_by_role("button", name="Retrieve").count()):
115+
# Start waiting for the pdf download
116+
with page.expect_download() as download_info:
117+
# Perform the action that initiates download
118+
page.get_by_role("button", name="Retrieve").nth(retrieve_button-1).click()
119+
download_file = download_info.value
120+
file = download_file.suggested_filename
121+
# Wait for the download process to complete and save the downloaded file in a temp folder
122+
download_file.save_as(f"/temp/{file}")
123+
page.wait_for_timeout(1000)
124+
if file.endswith(".pdf"):
125+
nhs_no = pdf_Reader(file)
126+
elif file.endswith(".csv"):
127+
csv_df = csv_Reader(file)
128+
129+
page.locator('text="Confirm Printed"').nth(0).wait_for()
130+
page.wait_for_timeout(1000) # This 1 second wait is to allow other Confirm printed buttons to show as they do not show up at the same time
131+
132+
# This loops through each Confirm printed button and clicks each one
133+
for confirm_button in range (page.get_by_role("button", name="Confirm Printed").count()):
134+
page.on("dialog", lambda dialog: dialog.accept())
135+
page.get_by_role("button", name="Confirm Printed").nth(0).click()
136+
page.wait_for_timeout(1000)
137+
138+
main_menu_available(page)
139+
page.get_by_role("link", name="Communications Production").click()
139140
page.get_by_role("link", name="Archived Batch List").click()
140141
page.locator("#batchIdFilter").click()
141142
page.locator("#batchIdFilter").fill(link_text)
142143
page.locator("#batchIdFilter").press("Enter")
143-
expect(page.locator("td").filter(has_text=link_text)).to_be_visible()
144-
145-
page.get_by_role("link", name="Main Menu").click()
146-
page.get_by_role("link", name="Screening Subject Search").click()
147-
page.get_by_role("textbox", name="NHS Number").click()
148-
page.get_by_role("textbox", name="NHS Number").fill(nhs_no)
149-
page.get_by_role("textbox", name="NHS Number").press("Enter")
150-
page.get_by_role("button", name="Search").click()
151-
expect(page.get_by_role("cell", name="Subject Screening Summary", exact=True)).to_be_visible()
152-
expect(page.get_by_role("cell", name="Latest Event Status", exact=True)).to_be_visible()
153-
expect(page.get_by_role("cell", name=latest_event_status, exact=True)).to_be_visible()
154-
155-
156-
def active_batch_processing_csv(page: Page, batch_type: str, batch_description: str, latest_event_status: str) -> None:
157-
# Print the batch of Pre-Invitation Letters
158-
page.get_by_role("link", name="Main Menu").click()
159-
page.get_by_role("link", name="Communications Production").click()
160-
page.get_by_role("link", name="Active Batch List").click()
161-
page.locator("#eventCodeFilter").click()
162-
page.locator("#eventCodeFilter").fill(batch_type)
163-
page.locator("#eventCodeFilter").press("Enter")
164-
pre_invitation_cells = page.locator(f"//td[text()='{batch_description}']")
165-
166-
for i in range(pre_invitation_cells.count()):
167-
row = pre_invitation_cells.nth(i).locator("..") # Get the parent row
168-
169-
# Check if the row contains "Prepared" or "Open"
170-
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:
171-
# Find the first link in that row and click it
172-
link = row.locator("a").first
173-
link_text = link.inner_text() # Get the link text dynamically
174-
link.click()
144+
expect(page.locator("td").filter(has_text=link_text)).to_be_visible() # Checks to see if the batch is now archived
175145

176-
# Checks to see if batch is already prepared
177-
page.wait_for_timeout(3000) # Without this timeout prepare_button is always set to false
178-
prepare_button = page.get_by_role("button", name="Prepare Batch").is_visible()
179-
180-
#If not prepared it will click on the prepare button and wait 5 seconds (ideally would be a loop)
181-
if prepare_button:
182-
page.get_by_role("button", name="Prepare Batch").click()
183-
page.wait_for_timeout(5000)
146+
subject_search_by_nhs_no(page, nhs_no, latest_event_status)
184147

185-
# Start waiting for the pdf download
186-
with page.expect_download() as download_info:
187-
# Perform the action that initiates download
188-
page.get_by_role("button", name="Retrieve").nth(0).click()
189-
download_pdf = download_info.value
190-
# Wait for the download process to complete and save the downloaded file in a temp folder
191-
download_pdf.save_as(f"/temp/{download_pdf.suggested_filename}")
192-
193-
reader = PdfReader(f"/temp/{download_pdf.suggested_filename}")
148+
def pdf_Reader(file: str):
149+
reader = PdfReader(f"/temp/{file}")
194150

195151
# For loop looping through all pages of the file to find the NHS Number
196152
for pages in reader.pages:
@@ -200,41 +156,28 @@ def active_batch_processing_csv(page: Page, batch_type: str, batch_description:
200156
text = text.splitlines(True)
201157
for split_text in text:
202158
if "NHS No" in split_text:
203-
# If a string is found containing "NHS No" all characters but digits are stored into nhs_no
204-
nhs_no_pdf = res = "".join([ele for ele in split_text if ele.isdigit()])
159+
# If a string is found containing "NHS No" only digits are stored into nhs_no
160+
nhs_no = res = "".join([ele for ele in split_text if ele.isdigit()])
205161
break
162+
return nhs_no
206163

207-
page.on("dialog", lambda dialog: dialog.accept())
208-
expect(page.get_by_role("button", name="Confirm Printed").nth(0)).to_be_visible()
209-
page.get_by_role("button", name="Confirm Printed").nth(0).click()
210-
# Start waiting for the csv download
211-
with page.expect_download() as download_info:
212-
# Perform the action that initiates download
213-
page.get_by_role("button", name="Retrieve").nth(1).click()
214-
download_csv = download_info.value
215-
# Wait for the download process to complete and save the downloaded file in a temp folder
216-
download_csv.save_as(f"/temp/{download_csv.suggested_filename}")
217-
218-
page.on("dialog", lambda dialog: dialog.accept())
219-
expect(page.get_by_role("button", name="Confirm Printed").nth(0)).to_be_visible()
220-
page.get_by_role("button", name="Confirm Printed").nth(0).click()
221-
# Storing the downloaded csv into a pandas dataframe
222-
csv_df = pd.read_csv(download_csv.suggested_filename)
223-
224-
page.get_by_role("link", name="Back").click()
225-
page.get_by_role("link", name="Back").click()
226-
page.get_by_role("link", name="Archived Batch List").click()
227-
page.locator("#batchIdFilter").click()
228-
page.locator("#batchIdFilter").fill(link_text)
229-
page.locator("#batchIdFilter").press("Enter")
230-
expect(page.locator("td").filter(has_text=link_text)).to_be_visible()
164+
def csv_Reader(file: str):
165+
csv_df = pd.read_csv(f"/temp/{file}")
166+
return csv_df
231167

232-
page.get_by_role("link", name="Main Menu").click()
168+
def subject_search_by_nhs_no(page: Page, nhs_no: str, latest_event_status: str):
169+
main_menu_available(page)
233170
page.get_by_role("link", name="Screening Subject Search").click()
234171
page.get_by_role("textbox", name="NHS Number").click()
235-
page.get_by_role("textbox", name="NHS Number").fill(nhs_no_pdf)
172+
page.get_by_role("textbox", name="NHS Number").fill(nhs_no)
236173
page.get_by_role("textbox", name="NHS Number").press("Enter")
237174
page.get_by_role("button", name="Search").click()
238175
expect(page.get_by_role("cell", name="Subject Screening Summary", exact=True)).to_be_visible()
239176
expect(page.get_by_role("cell", name="Latest Event Status", exact=True)).to_be_visible()
240177
expect(page.get_by_role("cell", name=latest_event_status, exact=True)).to_be_visible()
178+
179+
def main_menu_available(page: str):
180+
is_main_menu_available = page.get_by_role("link", name="Main Menu").is_visible()
181+
182+
if is_main_menu_available:
183+
page.get_by_role("link", name="Main Menu").click()

0 commit comments

Comments
 (0)