[Feat] Deprecate uncheck() and parameterize check(bool)
I find dealing with both check() and uncheck() to be cumbersome and would prefer a single check method that can handle both through a parameter.
Say I have a checkbox that configures whether a customer is a "gold" member or not. I will be testing both cases so my page object needs to both check and uncheck the checkbox, which, paradoxically, is where I run into bothers.
I can either create two methods:
def is_gold_member(page):
page.check('#gold')
def is_not_gold_member(page):
page.uncheck('#gold')
Or I can create a single parameterized method that requires logic in the body:
def is_gold_member(page, value: bool):
if value:
page.check('#gold')
else:
page.uncheck('#gold')
I dislike both of these options because both replicate the selector for the checkbox (which I feel moves me in the wrong direction for maintenance), and I prefer not having logic in my page objects since that is where bugs tend to hide. Parameterizing check would solve both of these woes:
def is_gold_member(page, value):
page.check('#gold', value)
No duplication and my guys don't need to write logic. Not only do I feel this is a cleaner solution but I think it fits better with the rest of the Playwright API too since most other inputs are also parameterized. Take press for example. It takes a parameter that handles all the cases instead of creating methods for each possible key press:
# We don't do this for a reason
def press_enter()
def press_escape()
def press_control_plus_enter()
def press_F1()
# etc
This is what check and uncheck are: a method for each possible input. I'd love to see it parameterized instead. Thanks for listening!
Hey, I transferred it to the Playwright repo since in Playwright for Python only the official upstream Playwright API is mirrored.
I think we could add setChecked, sure. But it seems like it is implementable in the user land with a one-liner:
def set_checked(page, selector: str, value: bool):
(page.check if value else page.uncheck)(selector)
Closing as per above for now since users seem to be satisfied with the existing shape of the API and your variation is easily achievable in the user land.