Skip to content

Commit a8f5741

Browse files
Converting functions in test_compartment_1.py into utils
Adding functionality of execution bcss_time_events to progress subjects instead of updating parameters on the DB
1 parent c2b00ce commit a8f5741

File tree

7 files changed

+161
-114
lines changed

7 files changed

+161
-114
lines changed

tests/Smokescreen/my_pages.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
from pages.invitations_plans_page import *
1414
from pages.generate_invitations_page import *
1515
from pages.log_out_page import *
16-
from utils.oracle import *
Lines changed: 7 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import pytest
2-
from pypdf import PdfReader
3-
from playwright.sync_api import Page, expect
4-
import pandas as pd
5-
import os
2+
from playwright.sync_api import Page
63
from my_pages import *
4+
from utils.batch_processing import batch_processing
5+
from utils.oracle import exec_bcss_timed_events
76

87
# To Do:
9-
# Create more POMs
10-
# Add more fail states
11-
# Add more logging to understand what is going on
12-
# Convert import pages * into 1 line
13-
# Move functions to utils
14-
# Remove as many hard coded timeouts as possible
158
# Create a generic click() function -> this aims to solve an issue where sometimes it thinks it has clicked the element but the page does not change
169

1710
@pytest.mark.wip
@@ -41,113 +34,17 @@ def test_example(page: Page) -> None:
4134
GenerateInvitations(page).wait_for_invitation_generation_complete()
4235

4336
# Print the batch of Pre-Invitation Letters
44-
batch_processing(page, "S1", "Pre-invitation (FIT)", "S9 - Pre-invitation Sent")
37+
s1_nhs_no = batch_processing(page, "S1", "Pre-invitation (FIT)", "S9 - Pre-invitation Sent")
38+
exec_bcss_timed_events(s1_nhs_no)
4539
batch_processing(page, "S1", "Pre-invitation (FIT) (digital leaflet)", "S9 - Pre-invitation Sent")
4640

47-
database_connection_exec("bcss_timed_events")
48-
4941
# Print the batch of Invitation & Test Kit Letters
50-
batch_processing(page, "S9", "Invitation & Test Kit (FIT)", "S10 - Invitation & Test Kit Sent")
42+
s9_nhs_no = batch_processing(page, "S9", "Invitation & Test Kit (FIT)", "S10 - Invitation & Test Kit Sent")
43+
exec_bcss_timed_events(s9_nhs_no)
5144

5245
# Print a set of reminder letters
5346
batch_processing(page, "S10", "Test Kit Reminder", "S19 - Reminder of Initial Test Sent")
5447

5548
# Log out
5649
NavigationBar(page).click_log_out_link()
5750
Logout(page).verify_log_out_page()
58-
59-
def batch_processing(page: Page, batch_type: str, batch_description: str, latest_event_status: str):
60-
NavigationBar(page).click_main_menu_link()
61-
MainMenu(page).go_to_communications_production_page()
62-
CommunicationsProduction(page).go_to_active_batch_list_page()
63-
ActiveBatchList(page).enter_event_code_filter(batch_type)
64-
65-
batch_description_cells = page.locator(f"//td[text()='{batch_description}']")
66-
67-
if batch_description_cells.count() == 0 and batch_description == "Pre-invitation (FIT) (digital leaflet)":
68-
print(f"No S1 Pre-invitation (FIT) (digital leaflet) batch found. Skipping to next step")
69-
return
70-
elif batch_description_cells.count() == 0 and page.locator("td", has_text="No matching records found"):
71-
pytest.fail(f"No {batch_type} {batch_description} batch found")
72-
73-
for i in range(batch_description_cells.count()):
74-
row = batch_description_cells.nth(i).locator("..") # Get the parent row
75-
76-
# Check if the row contains "Prepared" or "Open"
77-
if row.locator("td", has_text="Prepared").count() > 0 or row.locator("td", has_text="Open").count() > 0:
78-
# Find the first link in that row and click it
79-
link = row.locator("a").first
80-
link_text = link.inner_text() # Get the batch id dynamically
81-
link.click()
82-
break
83-
else:
84-
pytest.fail(f"No open/prepared '{batch_type} - {batch_description}' batch found")
85-
86-
ManageActiveBatch(page).click_prepare_button()
87-
88-
ManageActiveBatch(page).reprepare_batch_text.wait_for()
89-
90-
# This loops through each Retrieve button and clicks each one
91-
retrieve_button_count = 0
92-
for retrieve_button in range (ManageActiveBatch(page).retrieve_button.count()):
93-
retrieve_button_count += 1
94-
# Start waiting for the pdf download
95-
with page.expect_download() as download_info:
96-
# Perform the action that initiates download
97-
ManageActiveBatch(page).retrieve_button.nth(retrieve_button-1).click()
98-
download_file = download_info.value
99-
file = download_file.suggested_filename
100-
# Wait for the download process to complete and save the downloaded file in a temp folder
101-
download_file.save_as(file)
102-
if file.endswith(".pdf"):
103-
nhs_no = pdf_Reader(file)
104-
os.remove(file) # Deletes the file after extracting the necessary data
105-
elif file.endswith(".csv"):
106-
# csv_df = csv_Reader(file) # Currently no use in compartment 1, will be necessary for future compartments
107-
os.remove(file) # Deletes the file after extracting the necessary data
108-
109-
# This loops through each Confirm printed button and clicks each one
110-
for _ in range (retrieve_button_count):
111-
page.on("dialog", lambda dialog: dialog.accept())
112-
ManageActiveBatch(page).confirm_button.nth(0).click()
113-
114-
ActiveBatchList(page).batch_successfully_archived_msg.wait_for()
115-
116-
NavigationBar(page).click_main_menu_link()
117-
MainMenu(page).go_to_communications_production_page()
118-
CommunicationsProduction(page).go_to_archived_batch_list_page()
119-
ArchivedBatchList(page).enter_id_filter(link_text)
120-
ArchivedBatchList(page).verify_table_data(link_text)
121-
122-
subject_search_by_nhs_no(page, nhs_no, latest_event_status)
123-
124-
def pdf_Reader(file: str):
125-
reader = PdfReader(file)
126-
127-
# For loop looping through all pages of the file to find the NHS Number
128-
for pages in reader.pages:
129-
text = pages.extract_text()
130-
if "NHS No" in text:
131-
# If NHS number is found split the text by every new line into a list
132-
text = text.splitlines(True)
133-
for split_text in text:
134-
if "NHS No" in split_text:
135-
# If a string is found containing "NHS No" only digits are stored into nhs_no
136-
nhs_no = "".join([ele for ele in split_text if ele.isdigit()])
137-
break
138-
return nhs_no
139-
140-
def csv_Reader(file: str):
141-
csv_df = pd.read_csv(file)
142-
return csv_df
143-
144-
def subject_search_by_nhs_no(page: Page, nhs_no: str, latest_event_status: str):
145-
NavigationBar(page).click_main_menu_link()
146-
MainMenu(page).go_to_screening_subject_search_page()
147-
SubjectScreeningPage(page).click_nhs_number_filter()
148-
SubjectScreeningPage(page).nhs_number_filter.fill(nhs_no)
149-
SubjectScreeningPage(page).nhs_number_filter.press("Enter")
150-
SubjectScreeningPage(page).click_search_button()
151-
SubjectScreeningSummary(page).verify_subject_screening_summary()
152-
SubjectScreeningSummary(page).verify_latest_event_status_header()
153-
SubjectScreeningSummary(page).verify_latest_event_status_value(latest_event_status)

utils/batch_processing.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from pages.navigation_bar_links import NavigationBar
2+
from pages.bcss_home_page import MainMenu
3+
from pages.communications_production_page import CommunicationsProduction
4+
from pages.active_batch_list_page import ActiveBatchList
5+
from pages.manage_active_batch_page import ManageActiveBatch
6+
from pages.archived_batch_list_page import ArchivedBatchList
7+
from utils.extract_nhs_no_from_pdf import pdf_reader
8+
from utils.csv_reader import csv_reader
9+
from utils.subject_search_by_nhs_no import subject_search_by_nhs_no
10+
from utils.oracle import database_connection_exec, database_connection_query
11+
import os
12+
import pytest
13+
from playwright.sync_api import Page
14+
15+
def batch_processing(page: Page, batch_type: str, batch_description: str, latest_event_status: str):
16+
NavigationBar(page).click_main_menu_link()
17+
MainMenu(page).go_to_communications_production_page()
18+
CommunicationsProduction(page).go_to_active_batch_list_page()
19+
ActiveBatchList(page).enter_event_code_filter(batch_type)
20+
21+
batch_description_cells = page.locator(f"//td[text()='{batch_description}']")
22+
23+
if batch_description_cells.count() == 0 and batch_description == "Pre-invitation (FIT) (digital leaflet)":
24+
print(f"No S1 Pre-invitation (FIT) (digital leaflet) batch found. Skipping to next step")
25+
return
26+
elif batch_description_cells.count() == 0 and page.locator("td", has_text="No matching records found"):
27+
pytest.fail(f"No {batch_type} {batch_description} batch found")
28+
29+
for i in range(batch_description_cells.count()):
30+
row = batch_description_cells.nth(i).locator("..") # Get the parent row
31+
32+
# Check if the row contains "Prepared" or "Open"
33+
if row.locator("td", has_text="Prepared").count() > 0 or row.locator("td", has_text="Open").count() > 0:
34+
# Find the first link in that row and click it
35+
link = row.locator("a").first
36+
link_text = link.inner_text() # Get the batch id dynamically
37+
link.click()
38+
break
39+
else:
40+
pytest.fail(f"No open/prepared '{batch_type} - {batch_description}' batch found")
41+
42+
ManageActiveBatch(page).click_prepare_button()
43+
44+
ManageActiveBatch(page).reprepare_batch_text.wait_for()
45+
46+
# This loops through each Retrieve button and clicks each one
47+
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+
nhs_no = pdf_reader(file)
60+
os.remove(file) # Deletes the file after extracting the necessary data
61+
elif file.endswith(".csv"):
62+
csv_df = csv_reader(file) # Currently no use in compartment 1, will be necessary for future compartments
63+
os.remove(file) # Deletes the file after extracting the necessary data
64+
65+
# This loops through each Confirm printed button and clicks each one
66+
for _ in range (retrieve_button_count):
67+
page.on("dialog", lambda dialog: dialog.accept())
68+
ManageActiveBatch(page).confirm_button.nth(0).click()
69+
70+
ActiveBatchList(page).batch_successfully_archived_msg.wait_for()
71+
72+
NavigationBar(page).click_main_menu_link()
73+
MainMenu(page).go_to_communications_production_page()
74+
CommunicationsProduction(page).go_to_archived_batch_list_page()
75+
ArchivedBatchList(page).enter_id_filter(link_text)
76+
ArchivedBatchList(page).verify_table_data(link_text)
77+
78+
subject_search_by_nhs_no(page, nhs_no, latest_event_status)
79+
return nhs_no

utils/csv_reader.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
def csv_reader(file: str):
4+
csv_df = pd.read_csv(file)
5+
return csv_df

utils/extract_nhs_no_from_pdf.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pypdf import PdfReader
2+
3+
def pdf_reader(file: str):
4+
reader = PdfReader(file)
5+
6+
# For loop looping through all pages of the file to find the NHS Number
7+
for pages in reader.pages:
8+
text = pages.extract_text()
9+
if "NHS No" in text:
10+
# If NHS number is found split the text by every new line into a list
11+
text = text.splitlines(True)
12+
for split_text in text:
13+
if "NHS No" in split_text:
14+
# If a string is found containing "NHS No" only digits are stored into nhs_no
15+
nhs_no = "".join([ele for ele in split_text if ele.isdigit()])
16+
break
17+
return nhs_no

utils/oracle.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def database_connection_exec(procedure: str): # To use when "exec xxxx" (stored
1616
print(f"Attempting to execute stored procedure: {procedure}")
1717
cursor = conn.cursor()
1818
cursor.callproc(procedure)
19-
print(conn.version, "DB write successful!")
19+
print(conn.version, "stored procedure execution successful!")
2020
except Exception as executionError:
2121
print(f"Failed to execute stored procedure with execution error: {executionError}")
2222
finally:
@@ -25,7 +25,39 @@ def database_connection_exec(procedure: str): # To use when "exec xxxx" (stored
2525
except Exception as connectionError:
2626
print(f"Failed to connect to the DB! with connection error: {connectionError}")
2727

28-
def database_connection_query(query: str): # To use when "exec xxxx" (stored procedures)
28+
def exec_bcss_timed_events(nhs_no: str): # To use when "exec xxxx" (stored procedures)
29+
load_dotenv()
30+
un = os.getenv("un")
31+
cs = os.getenv("cs")
32+
pw = os.getenv("pw")
33+
34+
try:
35+
print("Attempting DB connection...")
36+
conn = oracledb.connect(user=un, password=pw, dsn=cs)
37+
print(conn.version, "DB connection successful!")
38+
try:
39+
print(f"Attempting to get subject_id from nhs number: {nhs_no}")
40+
cursor = conn.cursor()
41+
cursor.execute(f"SELECT SCREENING_SUBJECT_ID FROM SCREENING_SUBJECT_T WHERE SUBJECT_NHS_NUMBER = {int(nhs_no)}")
42+
result = cursor.fetchall()
43+
subject_id = result[0][0]
44+
print(conn.version, "Able to extract subject ID")
45+
try:
46+
print(f"Attempting to execute stored procedure: {f"'bcss_timed_events', [subject_id,'Y']"}")
47+
cursor = conn.cursor()
48+
cursor.callproc('bcss_timed_events', [subject_id,'Y'])
49+
print(conn.version, "stored procedure execution successful!")
50+
except Exception as spExecutionError:
51+
print(f"Failed to execute stored procedure with execution error: {spExecutionError}")
52+
except Exception as queryExecutionError:
53+
print(f"Failed to to extract subject ID with error: {queryExecutionError}")
54+
finally:
55+
if conn is not None:
56+
conn.close()
57+
except Exception as connectionError:
58+
print(f"Failed to connect to the DB! with connection error: {connectionError}")
59+
60+
def database_connection_query(query: str,): # To use when "exec xxxx" (stored procedures)
2961
load_dotenv()
3062
un = os.getenv("un")
3163
cs = os.getenv("cs")
@@ -39,12 +71,14 @@ def database_connection_query(query: str): # To use when "exec xxxx" (stored pro
3971
print(f"Attempting to execute query: {query}")
4072
cursor = conn.cursor()
4173
cursor.execute(query)
42-
print(conn.version, "DB write successful!")
74+
result = cursor.fetchall()
75+
print(conn.version, "query execution successful!")
4376
except Exception as executionError:
4477
print(f"Failed to execute query with execution error {executionError}")
4578
finally:
4679
if conn is not None:
4780
conn.close()
81+
return result
4882
except Exception as connectionError:
4983
print(f"Failed to connect to the DB! with connection error {connectionError}")
5084

utils/subject_search_by_nhs_no.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pages.navigation_bar_links import NavigationBar
2+
from pages.bcss_home_page import MainMenu
3+
from pages.subject_screening_page import SubjectScreeningPage
4+
from pages.subject_screening_summary import SubjectScreeningSummary
5+
from playwright.sync_api import Page
6+
7+
def subject_search_by_nhs_no(page: Page, nhs_no: str, latest_event_status: str):
8+
NavigationBar(page).click_main_menu_link()
9+
MainMenu(page).go_to_screening_subject_search_page()
10+
SubjectScreeningPage(page).click_nhs_number_filter()
11+
SubjectScreeningPage(page).nhs_number_filter.fill(nhs_no)
12+
SubjectScreeningPage(page).nhs_number_filter.press("Enter")
13+
SubjectScreeningPage(page).click_search_button()
14+
SubjectScreeningSummary(page).verify_subject_screening_summary()
15+
SubjectScreeningSummary(page).verify_latest_event_status_header()
16+
SubjectScreeningSummary(page).verify_latest_event_status_value(latest_event_status)

0 commit comments

Comments
 (0)