Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(locator): extend mouse movement steps to dragTo (#38184)

Adam Gastineaucba582647b1c
diff --git a/docs/src/api/class-frame.md b/docs/src/api/class-frame.md
index e286c63bfa..f9c366e427 100644
--- a/docs/src/api/class-frame.md
+++ b/docs/src/api/class-frame.md
@@ -482,6 +482,9 @@ Optional event-specific initialization properties.
### option: Frame.dragAndDrop.targetPosition = %%-input-target-position-%%
* since: v1.14
+### option: Frame.dragAndDrop.steps = %%-input-drag-steps-%%
+* since: v1.57
+
## async method: Frame.evalOnSelector
* since: v1.9
* discouraged: This method does not wait for the element to pass the actionability
diff --git a/docs/src/api/class-locator.md b/docs/src/api/class-locator.md
index a1f64a75b9..042c08bb99 100644
--- a/docs/src/api/class-locator.md
+++ b/docs/src/api/class-locator.md
@@ -875,6 +875,9 @@ Locator of the element to drag to.
### option: Locator.dragTo.targetPosition = %%-input-target-position-%%
* since: v1.18
+### option: Locator.dragTo.steps = %%-input-drag-steps-%%
+* since: v1.57
+
## async method: Locator.elementHandle
* since: v1.14
* discouraged: Always prefer using [Locator]s and web assertions over [ElementHandle]s because latter are inherently racy.
diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md
index ad4a89bdab..717cbf06cb 100644
--- a/docs/src/api/class-page.md
+++ b/docs/src/api/class-page.md
@@ -1102,6 +1102,9 @@ await Page.DragAndDropAsync("#source", "#target", new()
### option: Page.dragAndDrop.targetPosition = %%-input-target-position-%%
* since: v1.14
+### option: Page.dragAndDrop.steps = %%-input-drag-steps-%%
+* since: v1.57
+
## async method: Page.emulateMedia
* since: v1.8
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index e0141bec44..37f6665a93 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -108,6 +108,11 @@ element.
Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between Playwright's current cursor position and the provided destination. When set to 1, emits a single `mousemove` event at the destination location.
+## input-drag-steps
+- `steps` <[int]>
+
+Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup` of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+
## input-modifiers
- `modifiers` <[Array]<[KeyboardModifier]<"Alt"|"Control"|"ControlOrMeta"|"Meta"|"Shift">>>
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index d5383a95c5..b2d81ca90d 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -2496,6 +2496,12 @@ export interface Page {
y: number;
};
+ /**
+ * Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup`
+ * of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+ */
+ steps?: number;
+
/**
* When true, the call requires selector to resolve to a single element. If given selector resolves to more than one
* element, the call throws an exception.
@@ -6400,6 +6406,12 @@ export interface Frame {
y: number;
};
+ /**
+ * Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup`
+ * of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+ */
+ steps?: number;
+
/**
* When true, the call requires selector to resolve to a single element. If given selector resolves to more than one
* element, the call throws an exception.
@@ -13075,6 +13087,12 @@ export interface Locator {
y: number;
};
+ /**
+ * Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup`
+ * of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+ */
+ steps?: number;
+
/**
* Drops on the target element at this point relative to the top-left corner of the element's padding box. If not
* specified, some visible point of the element is used.
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index c3b46f560a..7c0622a3f9 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -1582,6 +1582,7 @@ scheme.FrameDragAndDropParams = tObject({
sourcePosition: tOptional(tType('Point')),
targetPosition: tOptional(tType('Point')),
strict: tOptional(tBoolean),
+ steps: tOptional(tInt),
});
scheme.FrameDragAndDropResult = tOptional(tObject({}));
scheme.FrameDblclickParams = tObject({
diff --git a/packages/playwright-core/src/server/frames.ts b/packages/playwright-core/src/server/frames.ts
index b89faf28fb..da1de2170d 100644
--- a/packages/playwright-core/src/server/frames.ts
+++ b/packages/playwright-core/src/server/frames.ts
@@ -1168,7 +1168,7 @@ export class Frame extends SdkObject {
// Note: do not perform locator handlers checkpoint to avoid moving the mouse in the middle of a drag operation.
dom.assertDone(await this._retryWithProgressIfNotConnected(progress, target, options.strict, false /* performActionPreChecks */, async handle => {
return handle._retryPointerAction(progress, 'move and up', false, async point => {
- await this._page.mouse.move(progress, point.x, point.y);
+ await this._page.mouse.move(progress, point.x, point.y, { steps: options.steps });
await this._page.mouse.up(progress);
}, {
...options,
diff --git a/packages/playwright-core/src/server/types.ts b/packages/playwright-core/src/server/types.ts
index 089ef21fdf..b189b92db9 100644
--- a/packages/playwright-core/src/server/types.ts
+++ b/packages/playwright-core/src/server/types.ts
@@ -113,6 +113,7 @@ export type PointerActionOptions = {
export type DragActionOptions = {
sourcePosition?: Point;
targetPosition?: Point;
+ steps?: number;
};
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index d5383a95c5..b2d81ca90d 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -2496,6 +2496,12 @@ export interface Page {
y: number;
};
+ /**
+ * Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup`
+ * of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+ */
+ steps?: number;
+
/**
* When true, the call requires selector to resolve to a single element. If given selector resolves to more than one
* element, the call throws an exception.
@@ -6400,6 +6406,12 @@ export interface Frame {
y: number;
};
+ /**
+ * Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup`
+ * of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+ */
+ steps?: number;
+
/**
* When true, the call requires selector to resolve to a single element. If given selector resolves to more than one
* element, the call throws an exception.
@@ -13075,6 +13087,12 @@ export interface Locator {
y: number;
};
+ /**
+ * Defaults to 1. Sends `n` interpolated `mousemove` events to represent travel between the `mousedown` and `mouseup`
+ * of the drag. When set to 1, emits a single `mousemove` event at the destination location.
+ */
+ steps?: number;
+
/**
* Drops on the target element at this point relative to the top-left corner of the element's padding box. If not
* specified, some visible point of the element is used.
diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts
index 66f4443ceb..96946db478 100644
--- a/packages/protocol/src/channels.d.ts
+++ b/packages/protocol/src/channels.d.ts
@@ -2789,6 +2789,7 @@ export type FrameDragAndDropParams = {
sourcePosition?: Point,
targetPosition?: Point,
strict?: boolean,
+ steps?: number,
};
export type FrameDragAndDropOptions = {
force?: boolean,
@@ -2796,6 +2797,7 @@ export type FrameDragAndDropOptions = {
sourcePosition?: Point,
targetPosition?: Point,
strict?: boolean,
+ steps?: number,
};
export type FrameDragAndDropResult = void;
export type FrameDblclickParams = {
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index 9e018052b4..786090a086 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -2224,6 +2224,7 @@ Frame:
sourcePosition: Point?
targetPosition: Point?
strict: boolean?
+ steps: int?
flags:
slowMo: true
snapshot: true
diff --git a/tests/page/page-drag.spec.ts b/tests/page/page-drag.spec.ts
index e7e3d17837..43e0f335be 100644
--- a/tests/page/page-drag.spec.ts
+++ b/tests/page/page-drag.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ElementHandle, Route } from 'playwright-core';
+import type { ElementHandle, Page, Route } from 'playwright-core';
import { test as it, expect } from './pageTest';
import { attachFrame } from '../config/utils';
@@ -293,6 +293,58 @@ it.describe('Drag and drop', () => {
expect(await page.$eval('#target', target => target.contains(document.querySelector('#source')))).toBe(true); // could not find source in target
});
+ [{
+ title: 'dragAndDrop',
+ drag: (page: Page, steps?: number) => page.dragAndDrop('#red', '#blue', { steps }),
+ }, {
+ title: 'dragTo',
+ drag: (page: Page, steps?: number) => page.locator('#red').dragTo(page.locator('#blue'), { steps }),
+ }].forEach(({ title, drag }) => {
+ it(`should ${title} with tweened mouse movement`, async ({ page }) => {
+ await page.setContent(`
+ <body style="margin: 0; padding: 0;">
+ <div style="width:100px;height:100px;background:red;" id="red"></div>
+ <div style="width:300px;height:100px;background:blue;" id="blue"></div>
+ </body>
+ `);
+ const eventsHandle = await page.evaluateHandle(() => {
+ const events = [];
+ document.addEventListener('mousedown', event => {
+ events.push({
+ type: 'mousedown',
+ x: event.pageX,
+ y: event.pageY,
+ });
+ });
+ document.addEventListener('mouseup', event => {
+ events.push({
+ type: 'mouseup',
+ x: event.pageX,
+ y: event.pageY,
+ });
+ });
+ document.addEventListener('mousemove', event => {
+ events.push({
+ type: 'mousemove',
+ x: event.pageX,
+ y: event.pageY,
+ });
+ });
+ return events;
+ });
+ await drag(page, 4);
+ await expect.poll(() => eventsHandle.jsonValue()).toEqual([
+ { type: 'mousemove', x: 50, y: 50 },
+ { type: 'mousedown', x: 50, y: 50 },
+ { type: 'mousemove', x: 75, y: 75 },
+ { type: 'mousemove', x: 100, y: 100 },
+ { type: 'mousemove', x: 125, y: 125 },
+ { type: 'mousemove', x: 150, y: 150 },
+ { type: 'mouseup', x: 150, y: 150 },
+ ]);
+ });
+ });
+
it('should allow specifying the position', async ({ page, server }) => {
await page.setContent(`
<div style="width:100px;height:100px;background:red;" id="red">