@@ -138,58 +138,103 @@ def assert_cell_to_right_has_expected_text(
138138 AssertionError: If the expected text is not found.
139139 """
140140 logging .info (f"Checking that the cell next to { text } contains { expected_text } " )
141+ scope = self .page .locator (f"div#{ div } " ) if div else self .page
141142
142- scope = self .page
143- if div :
144- scope = self .page .locator (f"div#{ div } " )
143+ if self ._check_table_row (scope , text , expected_text ):
144+ return
145+ if self ._check_span_structure (scope , text , expected_text ):
146+ return
145147
146- # Try table row structure first
147- row = scope .locator (f'.noTableRow:has-text("{ text } ")' ).first
148- if row .count () > 0 and row .is_visible ():
149- cells = row .locator ("xpath=./*" ).all ()
150- for idx , cell in enumerate (cells ):
151- cell_text = cell .inner_text ().strip ()
152- if text .strip () in cell_text :
153- if idx + 1 >= len (cells ):
154- raise AssertionError (f'No cell found to the right of "{ text } ".' )
155- right_cell = cells [idx + 1 ]
156-
157- # Check <input>
158- input_el = right_cell .locator ("input" )
159- if input_el .count () > 0 :
160- value = input_el .first .input_value ().strip ()
161- assert (
162- value == expected_text
163- ), f'Expected "{ expected_text } " but found "{ value } " in input.'
164- logging .info (
165- f"The cell next to { text } contains { expected_text } "
166- )
167- return
168-
169- # Check <select>
170- select_el = right_cell .locator ("select" )
171- if select_el .count () > 0 :
172- selected = (
173- select_el .locator ("option:checked" ).inner_text ().strip ()
174- )
175- assert (
176- selected == expected_text
177- ), f'Expected "{ expected_text } " but found "{ selected } " in select.'
178- logging .info (
179- f"The cell next to { text } contains { expected_text } "
180- )
181- return
182-
183- # Check <p>, <span>, etc.
184- generic_text = right_cell .inner_text ().strip ()
185- assert (
186- generic_text == expected_text
187- ), f'Expected "{ expected_text } " but found "{ generic_text } ".'
188- logging .info (f"The cell next to { text } contains { expected_text } " )
189- return
190-
191- raise AssertionError (f'Could not locate label "{ text } " in any cell.' )
148+ raise AssertionError (
149+ f'Could not find a visible row or span with text "{ text } ".'
150+ )
192151
152+ def _check_table_row (
153+ self , scope : Locator | Page , text : str , expected_text : str
154+ ) -> bool :
155+ """
156+ Checks if the expected text is present in the cell to the right of the cell containing `text`
157+ within a table row structure.
158+ Args:
159+ scope (Locator): The Playwright locator to scope the search.
160+ text (str): The label text to search for.
161+ expected_text (str): The expected value in the adjacent right-hand cell.
162+ Returns:
163+ bool: True if the expected text is found and matches, False otherwise.
164+ Raises:
165+ AssertionError: If the actual value does not match the expected value.
166+ """
167+ row = scope .locator (f'.noTableRow:has-text("{ text } ")' ).first
168+ if row .count () == 0 or not row .is_visible ():
169+ return False
170+ cells = row .locator ("xpath=./*" ).all ()
171+ cell_idx = next (
172+ (
173+ i
174+ for i , cell in enumerate (cells )
175+ if text .strip () in cell .inner_text ().strip ()
176+ ),
177+ None ,
178+ )
179+ if cell_idx is None or cell_idx + 1 >= len (cells ):
180+ return False
181+ right_cell = cells [cell_idx + 1 ]
182+ if self ._assert_right_cell (right_cell , text , expected_text ):
183+ logging .info (f"The cell next to { text } contains { expected_text } " )
184+ return True
185+ return False
186+
187+ def _assert_right_cell (
188+ self , right_cell : Locator , text : str , expected_text : str
189+ ) -> bool :
190+ """
191+ Asserts that the right cell contains the expected value, checking input, select, or generic text.
192+ Args:
193+ right_cell (Locator): The Playwright locator for the cell to the right.
194+ text (str): The label text for logging and error messages.
195+ expected_text (str): The expected value to check.
196+ Returns:
197+ bool: True if the expected value is found, False otherwise.
198+ Raises:
199+ AssertionError: If the actual value does not match the expected value.
200+ """
201+ input_el = right_cell .locator ("input" )
202+ if input_el .count () > 0 :
203+ value = input_el .first .input_value ().strip ()
204+ assert (
205+ value == expected_text
206+ ), f'Expected "{ expected_text } " but found "{ value } " in input to the right of "{ text } ".'
207+ logging .info (f'Input to the right of "{ text } " contains "{ expected_text } "' )
208+ return True
209+ select_el = right_cell .locator ("select" )
210+ if select_el .count () > 0 :
211+ selected = select_el .locator ("option:checked" ).inner_text ().strip ()
212+ assert (
213+ selected == expected_text
214+ ), f'Expected "{ expected_text } " but found "{ selected } " in select to the right of "{ text } ".'
215+ logging .info (f'Select to the right of "{ text } " contains "{ expected_text } "' )
216+ return True
217+ generic_text = right_cell .inner_text ().strip ()
218+ assert (
219+ generic_text == expected_text
220+ ), f'Expected "{ expected_text } " but found "{ generic_text } " in cell to the right of "{ text } ".'
221+ logging .info (f'Cell to the right of "{ text } " contains "{ expected_text } "' )
222+ return True
223+
224+ def _check_span_structure (
225+ self , scope : Locator | Page , text : str , expected_text : str
226+ ) -> bool :
227+ """
228+ Checks if the expected text is present in a span structure (label and userInput spans).
229+ Args:
230+ scope (Locator): The Playwright locator to scope the search.
231+ text (str): The label text to search for.
232+ expected_text (str): The expected value in the adjacent userInput span.
233+ Returns:
234+ bool: True if the expected text is found and matches, False otherwise.
235+ Raises:
236+ AssertionError: If the actual value does not match the expected value.
237+ """
193238 label_span = scope .locator (f'span.label:has-text("{ text } ")' ).first
194239 user_input_span = label_span .locator (
195240 'xpath=following-sibling::span[contains(@class,"userInput")]'
@@ -205,11 +250,8 @@ def assert_cell_to_right_has_expected_text(
205250 actual_text == expected_text
206251 ), f'Expected "{ expected_text } " but found "{ actual_text } " in userInput span next to "{ text } ".'
207252 logging .info (f"The span next to '{ text } ' contains '{ expected_text } '" )
208- return
209-
210- raise AssertionError (
211- f'Could not find a visible row or span with text "{ text } ".'
212- )
253+ return True
254+ return False
213255
214256 def assert_select_to_right_has_values (
215257 self , text : str , expected_values : List [str ], div : Optional [str ] = None
0 commit comments