Skip to content

Commit 336ec90

Browse files
Reducing cognitive complexity of methods
1 parent e11670e commit 336ec90

File tree

1 file changed

+75
-45
lines changed

1 file changed

+75
-45
lines changed

pages/datasets/investigation_dataset_page.py

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def __init__(self, page: Page):
9393
self.visible_search_text_input = self.page.locator(
9494
'input[id^="UI_SEARCH_"]:visible'
9595
)
96-
self.diagnostic_test_result = self.page.locator('#datasetContent > div:nth-child(1) > div:nth-child(7) > span.userInput')
96+
self.diagnostic_test_result = self.page.locator(
97+
"#datasetContent > div:nth-child(1) > div:nth-child(7) > span.userInput"
98+
)
9799

98100
# Repeat strings:
99101
self.bowel_preparation_administered_string = "Bowel Preparation Administered"
@@ -1198,7 +1200,6 @@ def does_field_contain_expected_value(
11981200
logging.debug(
11991201
f"START: does_field_contain_expected_value({dataset_area}, {dataset_subsection}, {field_name}, {expected_value})"
12001202
)
1201-
field_and_value_found = False
12021203
map_of_field_and_elements = self.map_fields_and_values(
12031204
dataset_area, dataset_subsection
12041205
)
@@ -1208,7 +1209,7 @@ def does_field_contain_expected_value(
12081209
if actual_value.replace(" ", "") == "":
12091210
actual_value = ""
12101211
if expected_value.lower() in actual_value.lower():
1211-
field_and_value_found = True
1212+
pass
12121213
else:
12131214
raise ValueError(
12141215
f"Value not as expected. Expected: {expected_value}, Actual: '{actual_value}'"
@@ -1217,24 +1218,21 @@ def does_field_contain_expected_value(
12171218
raise ValueError(
12181219
f"Field '{field_name}' not found in the dataset area '{dataset_area}' and subsection '{dataset_subsection}'."
12191220
)
1221+
logging.info(
1222+
f"[UI ASSERTIONS COMPLETE] Value as expected. Expected: {expected_value}, Actual: '{actual_value}'"
1223+
)
12201224
logging.debug("END: does_field_contain_expected_value")
1221-
if field_and_value_found:
1222-
logging.info(
1223-
f"[UI ASSERTIONS COMPLETE] Value as expected. Expected: {expected_value}, Actual: '{actual_value}'"
1224-
)
1225-
else:
1226-
raise ValueError(
1227-
f"[UI ASSERTIONS FAILED] Field '{field_name}' with expected value '{expected_value}' not found."
1228-
)
12291225

12301226
def map_fields_and_values(
12311227
self, dataset_section: str, dataset_subsection: str | None
12321228
) -> dict[str, str]:
12331229
"""
12341230
Maps field labels to their values for a given dataset section and subsection.
1231+
12351232
Args:
12361233
dataset_section (str): The name of the dataset section.
12371234
dataset_subsection (str | None): The name of the dataset subsection.
1235+
12381236
Returns:
12391237
dict[str, str]: A dictionary mapping field labels to their corresponding values.
12401238
"""
@@ -1245,49 +1243,60 @@ def map_fields_and_values(
12451243
fields_with_values = {}
12461244

12471245
for field_label, element in fields_and_elements.items():
1248-
# Find all child elements
1249-
value_list = element.locator("xpath=.//*").all()
1250-
field_value = ""
1251-
if not value_list:
1252-
field_value = element.inner_text()
1253-
else:
1254-
for value_from_list in value_list:
1255-
tag_name = value_from_list.evaluate(
1256-
"el => el.tagName.toLowerCase()"
1257-
)
1258-
match tag_name:
1259-
case "select":
1260-
# Get selected option text
1261-
selected_option = value_from_list.locator("option:checked")
1262-
field_value += selected_option.inner_text()
1263-
case "li":
1264-
input_elem = value_from_list.locator("input")
1265-
if input_elem.is_checked():
1266-
label_elem = value_from_list.locator("label")
1267-
field_value = label_elem.inner_text()
1268-
case "input":
1269-
input_type = value_from_list.get_attribute("type")
1270-
if input_type == "text":
1271-
field_value += value_from_list.input_value()
1272-
case "p":
1273-
field_value += value_from_list.inner_text()
1274-
case _:
1275-
logging.debug(
1276-
f"tag type not specified, tag ignored = {tag_name}"
1277-
)
1278-
fields_with_values[field_label] = field_value
1246+
fields_with_values[field_label] = self._extract_field_value(element)
12791247

12801248
logging.debug("end: map_fields_and_values()")
12811249
return fields_with_values
12821250

1251+
def _extract_field_value(self, element: Locator) -> str:
1252+
"""
1253+
Extracts the value from a field element, handling different HTML tags.
1254+
1255+
Args:
1256+
element (Locator): The Playwright Locator for the field element.
1257+
1258+
Returns:
1259+
str: The extracted value as a string.
1260+
"""
1261+
value_list = element.locator("xpath=.//*").all()
1262+
if not value_list:
1263+
return element.inner_text()
1264+
field_value = ""
1265+
for value_from_list in value_list:
1266+
tag_name = value_from_list.evaluate("el => el.tagName.toLowerCase()")
1267+
match tag_name:
1268+
case "select":
1269+
selected_option = value_from_list.locator("option:checked")
1270+
field_value += selected_option.inner_text()
1271+
continue
1272+
case "li":
1273+
input_elem = value_from_list.locator("input")
1274+
if input_elem.is_checked():
1275+
label_elem = value_from_list.locator("label")
1276+
return label_elem.inner_text()
1277+
continue
1278+
case "input":
1279+
input_type = value_from_list.get_attribute("type")
1280+
if input_type == "text":
1281+
field_value += value_from_list.input_value()
1282+
continue
1283+
case "p":
1284+
field_value += value_from_list.inner_text()
1285+
continue
1286+
case _:
1287+
logging.debug(f"tag type not specified, tag ignored = {tag_name}")
1288+
return field_value
1289+
12831290
def map_fields_and_elements(
12841291
self, dataset_section: str, dataset_subsection: Optional[str] = None
12851292
) -> dict[str, Locator]:
12861293
"""
12871294
Maps field labels to their corresponding Playwright Locator elements for a given dataset section and subsection.
1295+
12881296
Args:
12891297
dataset_section (str): The name of the dataset section.
12901298
dataset_subsection (Optional[str]): The name of the dataset subsection, if any.
1299+
12911300
Returns:
12921301
dict[str, Locator]: A dictionary mapping field labels to their corresponding Locator elements.
12931302
"""
@@ -1311,7 +1320,31 @@ def map_fields_and_elements(
13111320
)
13121321

13131322
list_of_rows = section.locator(".noTableRow").all()
1323+
self._process_rows_for_field_mapping(
1324+
list_of_rows, fields_and_elements, previous_field, field_name, line_counter
1325+
)
13141326

1327+
logging.debug("end: map_fields_and_elements()")
1328+
return fields_and_elements
1329+
1330+
def _process_rows_for_field_mapping(
1331+
self,
1332+
list_of_rows: list,
1333+
fields_and_elements: dict,
1334+
previous_field: str,
1335+
field_name: str,
1336+
line_counter: int,
1337+
) -> None:
1338+
"""
1339+
Processes each row in the dataset section/subsection and maps field labels to their corresponding Locator elements.
1340+
1341+
Args:
1342+
list_of_rows (list): List of Playwright Locator rows in the section/subsection.
1343+
fields_and_elements (dict): Dictionary to store field label to Locator mappings.
1344+
previous_field (str): The previous field label encountered.
1345+
field_name (str): The current field label being processed.
1346+
line_counter (int): Counter for multi-line fields.
1347+
"""
13151348
for row in list_of_rows:
13161349
elements_in_row = row.locator("xpath=.//*").all()
13171350
element_to_map = None
@@ -1343,9 +1376,6 @@ def map_fields_and_elements(
13431376
)
13441377
line_counter += 1
13451378

1346-
logging.debug("end: map_fields_and_elements()")
1347-
return fields_and_elements
1348-
13491379
def assert_test_result(self, expected_text: str) -> None:
13501380
"""
13511381
Asserts that the text in the test result matches the expected text.

0 commit comments

Comments
 (0)