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]: Subscribe to arbitrary event bindings #2931

Closed
RenderMichael opened this issue May 7, 2024 · 4 comments
Closed

[Feature]: Subscribe to arbitrary event bindings #2931

RenderMichael opened this issue May 7, 2024 · 4 comments

Comments

@RenderMichael
Copy link
Contributor

馃殌 Feature Request

The ability to subscribe to arbitrary events, which would parallel the node.js implementation of page.on/page.off/page.once

Example

private static async Task DragToTheRight(IPage page, ILocator element, int steps)
{
    int x = -1;
    int y = -1;

    page.OnAsync("mousemove", CaptureCoordinates);

    await element.HoverAsync().ConfigureAwait(false);

    int pinX = x;
    int pinY = y;

    await page.Mouse.DownAsync().ConfigureAwait(false);
    await page.Mouse.MoveAsync(pinX * steps, pinY, new() { Steps = steps, }).ConfigureAwait(false);
    await page.Mouse.UpAsync().ConfigureAwait(false);

    void CaptureCoordinates(JsonElement args)
    {
        x = args["clientX"];
        y = args["clientY"];
    }
}

Motivation

I'm trying to implement a feature where I drag a slider a certain number of paces to the right. Playwright has support for dragging an element to another element, but not in an arbitrary direction. Seeing commentary in issues like microsoft/playwright#13109, I doubt support would be added for relative mouse coordinates.

Hence, I'm trying to do it manually, and I would like access to the raw mousemove event for this.

@mxschmitt
Copy link
Member

The Node.js implementation of Playwright does not support arbitrary events. Only "Playwright events" are supported here. They need to be manually dispatched from the browser side to the Node.js side, via e.g. exposeBinding. See a full list here of Playwright events on a Page instance.

@RenderMichael RenderMichael changed the title [Feature]: [Feature]: Sucscribe to arbitrary event bindings May 7, 2024
@RenderMichael
Copy link
Contributor Author

I see. Looking at the documentation for exposeBinding, it looks like its designed to inject HTML into the DOM, and wouldn't work for the mousemove scenario I laid out.

Is there a way around this? Or rather, to avoid an XY problem, is there any support for dragging a slider a certain number of paces to the left or the right?

@mxschmitt
Copy link
Member

(It's designed to expose functions into the browser which when you call them (from the browser) get invoked on the Node.js side.)

You could do something like that and wrap it inside a small function like we do in some of our tests: https://github.com/microsoft/playwright/blob/81e907f4c158502118e79f6045a115a50c4019d3/tests/library/inspector/cli-codegen-1.spec.ts#L754-L762

Does your element look like that?

<input type="range" min="0" max="10" value="5">

@RenderMichael RenderMichael changed the title [Feature]: Sucscribe to arbitrary event bindings [Feature]: Subscribe to arbitrary event bindings May 9, 2024
@mxschmitt
Copy link
Member

Closing as part of the triage process since it seemed stale. Please create a new issue with a detailed reproducible or feature request if you still face issues.

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

No branches or pull requests

2 participants