Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: pass the locator as a parameter to the handler of addLocatorHandler #29779

Closed
regseb opened this issue Mar 2, 2024 · 1 comment 路 Fixed by #30494
Closed

[Feature]: pass the locator as a parameter to the handler of addLocatorHandler #29779

regseb opened this issue Mar 2, 2024 · 1 comment 路 Fixed by #30494
Assignees
Labels

Comments

@regseb
Copy link
Contributor

regseb commented Mar 2, 2024

馃殌 Feature Request

With Page.addLocatorHandler(), pass the locator as a parameter to the handler function.

Example

  • Currently:

    await page.addLocatorHandler(
      page.getByRole('button', { name: 'Accept all cookies' }),
      () => page.getByRole('button', { name: 'Accept all cookies' }).click());
  • With this feature:

    await page.addLocatorHandler(
      page.getByRole('button', { name: 'Accept all cookies' }),
      (locator) => locator.click());

Motivation

  • Do not write the same selector twice.
  • Use a single generic handler to close several dialog.
@dgozman dgozman added the v1.43 label Mar 4, 2024
@dgozman dgozman self-assigned this Mar 4, 2024
@dgozman dgozman added v1.44 and removed v1.43 labels Mar 25, 2024
@regseb
Copy link
Contributor Author

regseb commented Apr 12, 2024

Suggested modification to packages/playwright-core/src/client/page.ts:

   async addLocatorHandler(locator: Locator, handler: Function): Promise<void> {
     if (locator._frame !== this._mainFrame)
       throw new Error(`Locator must belong to the main frame of this page`);
     const { uid } = await this._channel.registerLocatorHandler({ selector: locator._selector });
-    this._locatorHandlers.set(uid, handler);
+    this._locatorHandlers.set(uid, { locator, handler });
   }

   private async _onLocatorHandlerTriggered(uid: number) {
     try {
-      const handler = this._locatorHandlers.get(uid);
-      await handler?.();
+      const { locator, handler } = this._locatorHandlers.get(uid);
+      await handler?.(locator);
     } finally {
       this._wrapApiCall(() => this._channel.resolveLocatorHandlerNoReply({ uid }), true).catch(() => {});
     }
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants