Skip to content

Commit 53c5605

Browse files
authored
tests(bug): Use locator instead of element handles (#1716)
1 parent ce10a37 commit 53c5605

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

shiny/playwright/controller/_accordion.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,16 @@ def set(
297297
"""
298298
if isinstance(open, str):
299299
open = [open]
300-
for element in self.loc.element_handles():
301-
element.wait_for_element_state(state="visible", timeout=timeout)
302-
element.scroll_into_view_if_needed(timeout=timeout)
303-
elem_value = element.get_attribute("data-value")
300+
301+
# TODO-future: XOR on the next open state and the current open state
302+
for i in range(self.loc.count()):
303+
el_loc = self.loc.nth(i)
304+
el_loc.element_handle().wait_for_element_state(
305+
state="visible", timeout=timeout
306+
)
307+
el_loc.scroll_into_view_if_needed(timeout=timeout)
308+
309+
elem_value = el_loc.get_attribute("data-value")
304310
if elem_value is None:
305311
raise ValueError(
306312
"Accordion panel does not have a `data-value` attribute"

shiny/playwright/controller/_input_controls.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -805,16 +805,17 @@ def in_selected(value: str) -> bool:
805805
return True
806806
return False
807807

808-
# Could do with multiple locator calls,
809-
# but unchecking the elements that are not in `selected` is not possible
810-
# as `set_checked()` likes a single element.
811-
for checkbox in self.loc_choices.element_handles():
808+
for i in range(self.loc_choices.count()):
809+
checkbox = self.loc_choices.nth(i)
812810
is_selected = in_selected(checkbox.input_value(timeout=timeout))
813-
checkbox.set_checked(
814-
is_selected,
815-
timeout=timeout,
816-
**kwargs, # pyright: ignore[reportArgumentType]
817-
)
811+
currently_selected = checkbox.is_checked()
812+
# Only update if needed
813+
if is_selected != currently_selected:
814+
checkbox.set_checked(
815+
is_selected,
816+
timeout=timeout,
817+
**kwargs, # pyright: ignore[reportArgumentType]
818+
)
818819

819820
def expect_choices(
820821
self,

shiny/playwright/controller/_output.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,12 +1212,10 @@ def set_filter(
12121212
The maximum time to wait for the action to complete. Defaults to `None`.
12131213
"""
12141214
# reset all filters
1215-
all_input_handles = self.loc_column_filter.locator(
1216-
"> input, > div > input"
1217-
).element_handles()
1218-
for input_handle in all_input_handles:
1219-
input_handle.scroll_into_view_if_needed()
1220-
input_handle.fill("", timeout=timeout)
1215+
all_input_locs = self.loc_column_filter.locator("> input, > div > input")
1216+
for i in range(all_input_locs.count()):
1217+
input_el = all_input_locs.nth(i)
1218+
input_el.fill("", timeout=timeout)
12211219

12221220
if filter is None:
12231221
return

tests/playwright/shiny/components/data_frame/filter_reset/test_filter_reset.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def test_filters_are_reset(page: Page, local_app: ShinyAppProc) -> None:
1717
expect(filter_inputs).to_have_count(8 + 5) # 8 columns including 5 numeric columns
1818

1919
penguin_code.expect_value("()")
20-
for element in filter_inputs.element_handles():
21-
assert element.input_value() == ""
20+
for i in range(filter_inputs.count()):
21+
expect(filter_inputs.nth(i)).to_have_value("")
2222

2323
update_filters.click()
2424

@@ -30,14 +30,13 @@ def test_filters_are_reset(page: Page, local_app: ShinyAppProc) -> None:
3030
"{'col': 4, 'value': (220, 225)}"
3131
")"
3232
)
33-
for value, element in zip(
34-
["Gentoo", "", "50", "", "", "17", "220", "225", "", "", "", "", ""],
35-
filter_inputs.element_handles(),
33+
for i, value in enumerate(
34+
["Gentoo", "", "50", "", "", "17", "220", "225", "", "", "", "", ""]
3635
):
37-
assert element.input_value() == value
36+
expect(filter_inputs.nth(i)).to_have_value(value)
3837

3938
reset_filters.click()
4039

4140
penguin_code.expect_value("()")
42-
for element in filter_inputs.element_handles():
43-
assert element.input_value() == ""
41+
for i in range(filter_inputs.count()):
42+
expect(filter_inputs.nth(i)).to_have_value("")

0 commit comments

Comments
 (0)