1- from playwright .sync_api import Page , Locator ,expect
1+ from playwright .sync_api import Page , Locator , expect
22from pages .base_page import BasePage
33import logging
4- from random import randrange
4+ from random import secrets
55
66
77class TableUtils :
@@ -21,8 +21,10 @@ def __init__(self, page: Page, table_locator: str) -> None:
2121 A TableUtils object ready to use.
2222 """
2323 self .page = page
24- self .table_id = table_locator # Store the table locator as a string
25- self .table = page .locator (table_locator ) # Create a locator object for the table
24+ self .table_id = table_locator # Store the table locator as a string
25+ self .table = page .locator (
26+ table_locator
27+ ) # Create a locator object for the table
2628
2729 def get_column_index (self , column_name : str ) -> int :
2830 """
@@ -32,9 +34,9 @@ def get_column_index(self, column_name: str) -> int:
3234 :param column_name: Name of the column (e.g., 'NHS Number')
3335 :return: 1-based column index or -1 if not found
3436 """
35- header_row = self . table . locator ( "tbody tr" ). filter (
36- has = self .page .locator ("th" )
37- ). first
37+ header_row = (
38+ self . table . locator ( "tbody tr" ). filter ( has = self .page .locator ("th" )). first
39+ )
3840
3941 headers = header_row .locator ("th" )
4042 header_texts = headers .evaluate_all ("ths => ths.map(th => th.innerText.trim())" )
@@ -118,16 +120,18 @@ def pick_random_row(self) -> Locator:
118120 Returns:
119121 A playwright.sync_api.Locator with the row object.
120122 """
121- return self .page .locator (f"{ self .table_id } > tbody tr" ).nth (randrange (0 , self .get_row_count ()))
123+ return self .page .locator (f"{ self .table_id } > tbody tr" ).nth (
124+ secrets .randbelow (self .get_row_count ())
125+ )
122126
123127 def pick_random_row_number (self ) -> int :
124128 """
125- This picks a random row from the table in BS-Select and returns its position
129+ This picks a random row from the table in BCSS and returns its position
126130
127131 Returns:
128132 An int representing a random row on the table.
129133 """
130- return randrange ( 0 , self .get_row_count ())
134+ return secrets . randbelow ( self .get_row_count ())
131135
132136 def get_row_data_with_headers (self , row_number : int ) -> dict :
133137 """
@@ -141,7 +145,10 @@ def get_row_data_with_headers(self, row_number: int) -> dict:
141145 """
142146 headers = self .get_table_headers ()
143147 row_data = self ._format_inner_text (
144- self .page .locator (f"{ self .table_id } > tbody tr" ).nth (row_number ).inner_text ())
148+ self .page .locator (f"{ self .table_id } > tbody tr" )
149+ .nth (row_number )
150+ .inner_text ()
151+ )
145152 results = {}
146153
147154 for key in headers :
@@ -161,12 +168,3 @@ def get_full_table_with_headers(self) -> dict:
161168 for row in range (self .get_row_count ()):
162169 full_results [row + 1 ] = self .get_row_data_with_headers (row )
163170 return full_results
164-
165- def wait_for_table_to_populate (self ) -> None :
166- """
167- This checks that the following phrases are no longer present in the body of the table:
168- - "Waiting for typing to finish..."
169- - "Searching..."
170- """
171- expect (self .page .locator (self .table_id )).not_to_contain_text ("Waiting for typing to finish..." )
172- expect (self .page .locator (self .table_id )).not_to_contain_text ("Searching..." , timeout = 10000 )
0 commit comments