I've been tooling around with Playwright on Python (v1.16) and thought I'd migrate some of our Cypress tests as an experiment. However, I've run into several snags all related to the same problem: I need to wait for some arbitrary DOM state and don't know how.
I know Playwright can wait for a handful of element states such as visible or detached, but I want something that waits for other DOM states too, like text to become present, element attributes to have a certain value, or HTML structure within an element to change, etc.
For example, in one test I have a progress indicator that is updated a second or so after a form is completed. I want to assert that the progress percent arrives at an expected value, but in order to do that I need to wait for the progress element's text to change accordingly. I don't see how to do this without writing my own polling scheme.
In another case I have a Submit
button that was built in this "really cool" dynamic way that uses a .disabled
CSS class to indicate to the user they can't submit the form (the button also has its click handlers dynamically applied/removed to enforce the disabled state). Playwright doesn't know anything about this and thinks the button is ready to be clicked immediately after calling click()
because all the actionability checks pass right away. I need to wait for the disabled
class to be removed from the button before calling click()
. How can I do this?
I know I can make new selectors for elements that include the DOM bits that I want to wait for (e.g. add a condition text=5%
to my progress indicator selector), but I don't like this because if it fails, I get a generic "we couldn't find the selector" error instead of something more assertion-like such as "Failed waiting for the element's text to contain 5%". I also use page objects to store my selectors and it would really suck if I had to make a new selector for each assertion on each element.
Is there an idiomatic way to wait for arbitrary DOM states that doesn't involve duplicating/modifying selectors?