@@ -11752,35 +11752,37 @@ def set_checked(
11752
11752
def add_locator_handler(self, locator: "Locator", handler: typing.Callable) -> None:
11753
11753
"""Page.add_locator_handler
11754
11754
11755
- Sometimes, the web page can show an overlay that obstructs elements behind it and prevents certain actions, like
11756
- click, from completing. When such an overlay is shown predictably, we recommend dismissing it as a part of your
11757
- test flow. However, sometimes such an overlay may appear non-deterministically, for example certain cookies consent
11758
- dialogs behave this way. In this case, `page.add_locator_handler()` allows handling an overlay during an
11759
- action that it would block.
11760
-
11761
- This method registers a handler for an overlay that is executed once the locator is visible on the page. The
11762
- handler should get rid of the overlay so that actions blocked by it can proceed. This is useful for
11763
- nondeterministic interstitial pages or dialogs, like a cookie consent dialog.
11764
-
11765
- Note that execution time of the handler counts towards the timeout of the action/assertion that executed the
11766
- handler.
11767
-
11768
- You can register multiple handlers. However, only a single handler will be running at a time. Any actions inside a
11769
- handler must not require another handler to run.
11770
-
11771
- **NOTE** Running the interceptor will alter your page state mid-test. For example it will change the currently
11772
- focused element and move the mouse. Make sure that the actions that run after the interceptor are self-contained
11773
- and do not rely on the focus and mouse state. <br /> <br /> For example, consider a test that calls
11755
+ When testing a web page, sometimes unexpected overlays like a coookie consent dialog appear and block actions you
11756
+ want to automate, e.g. clicking a button. These overlays don't always show up in the same way or at the same time,
11757
+ making them tricky to handle in automated tests.
11758
+
11759
+ This method lets you set up a special function, called a handler, that activates when it detects that overlay is
11760
+ visible. The handler's job is to remove the overlay, allowing your test to continue as if the overlay wasn't there.
11761
+
11762
+ Things to keep in mind:
11763
+ - When an overlay is shown predictably, we recommend explicitly waiting for it in your test and dismissing it as
11764
+ a part of your normal test flow, instead of using `page.add_locator_handler()`.
11765
+ - Playwright checks for the overlay every time before executing or retrying an action that requires an
11766
+ [actionability check](https://playwright.dev/python/docs/actionability), or before performing an auto-waiting assertion check. When overlay
11767
+ is visible, Playwright calls the handler first, and then proceeds with the action/assertion.
11768
+ - The execution time of the handler counts towards the timeout of the action/assertion that executed the handler.
11769
+ If your handler takes too long, it might cause timeouts.
11770
+ - You can register multiple handlers. However, only a single handler will be running at a time. Make sure the
11771
+ actions within a handler don't depend on another handler.
11772
+
11773
+ **NOTE** Running the handler will alter your page state mid-test. For example it will change the currently focused
11774
+ element and move the mouse. Make sure that actions that run after the handler are self-contained and do not rely on
11775
+ the focus and mouse state being unchanged. <br /> <br /> For example, consider a test that calls
11774
11776
`locator.focus()` followed by `keyboard.press()`. If your handler clicks a button between these two
11775
11777
actions, the focused element most likely will be wrong, and key press will happen on the unexpected element. Use
11776
11778
`locator.press()` instead to avoid this problem. <br /> <br /> Another example is a series of mouse
11777
11779
actions, where `mouse.move()` is followed by `mouse.down()`. Again, when the handler runs between
11778
- these two actions, the mouse position will be wrong during the mouse down. Prefer methods like
11779
- `locator.click()` that are self-contained .
11780
+ these two actions, the mouse position will be wrong during the mouse down. Prefer self-contained actions like
11781
+ `locator.click()` that do not rely on the state being unchanged by a handler .
11780
11782
11781
11783
**Usage**
11782
11784
11783
- An example that closes a cookie dialog when it appears:
11785
+ An example that closes a cookie consent dialog when it appears:
11784
11786
11785
11787
```py
11786
11788
# Setup the handler.
0 commit comments