diff --git a/packages/playwright-core/src/server/chromium/crInput.ts b/packages/playwright-core/src/server/chromium/crInput.ts index 0e1d0bfe07..7cb361b4d1 100644 --- a/packages/playwright-core/src/server/chromium/crInput.ts +++ b/packages/playwright-core/src/server/chromium/crInput.ts @@ -22,7 +22,7 @@ import { macEditingCommands } from '../macEditingCommands'; import { isString } from '../../utils/utils'; import { DragManager } from './crDragDrop'; import { CRPage } from './crPage'; -import { toModifiersMask } from './crProtocolHelper'; +import { toButtonsMask, toModifiersMask } from './crProtocolHelper'; export class RawKeyboardImpl implements input.RawKeyboard { constructor( @@ -101,6 +101,7 @@ export class RawMouseImpl implements input.RawMouse { await this._client.send('Input.dispatchMouseEvent', { type: 'mouseMoved', button, + buttons: toButtonsMask(buttons), x, y, modifiers: toModifiersMask(modifiers) @@ -120,6 +121,7 @@ export class RawMouseImpl implements input.RawMouse { await this._client.send('Input.dispatchMouseEvent', { type: 'mousePressed', button, + buttons: toButtonsMask(buttons), x, y, modifiers: toModifiersMask(modifiers), @@ -135,6 +137,7 @@ export class RawMouseImpl implements input.RawMouse { await this._client.send('Input.dispatchMouseEvent', { type: 'mouseReleased', button, + buttons: toButtonsMask(buttons), x, y, modifiers: toModifiersMask(modifiers), diff --git a/packages/playwright-core/src/server/chromium/crProtocolHelper.ts b/packages/playwright-core/src/server/chromium/crProtocolHelper.ts index 42822dcc11..a5361ec713 100644 --- a/packages/playwright-core/src/server/chromium/crProtocolHelper.ts +++ b/packages/playwright-core/src/server/chromium/crProtocolHelper.ts @@ -102,3 +102,14 @@ export function toModifiersMask(modifiers: Set): number mask |= 8; return mask; } + +export function toButtonsMask(buttons: Set): number { + let mask = 0; + if (buttons.has('left')) + mask |= 1; + if (buttons.has('right')) + mask |= 2; + if (buttons.has('middle')) + mask |= 4; + return mask; +} diff --git a/tests/page/page-mouse.spec.ts b/tests/page/page-mouse.spec.ts index 8398e4d780..6b0bc5c172 100644 --- a/tests/page/page-mouse.spec.ts +++ b/tests/page/page-mouse.spec.ts @@ -110,6 +110,40 @@ it('should pointerdown the div with a custom button', async ({ page, server, bro expect(event.pointerId).toBe(browserName === 'firefox' ? 0 : 1); }); +it('should report correct buttons property', async ({ page }) => { + await page.evaluate(() => { + (window as any).__EVENTS = []; + const handler = event => { + (window as any).__EVENTS.push({ + type: event.type, + button: event.button, + buttons: event.buttons, + }); + }; + window.addEventListener('mousedown', handler, false); + window.addEventListener('mouseup', handler, false); + }); + await page.mouse.move(50, 60); + await page.mouse.down({ + button: 'middle', + }); + await page.mouse.down({ + button: 'left', + }); + await page.mouse.up({ + button: 'middle', + }); + await page.mouse.up({ + button: 'left', + }); + expect(await page.evaluate(() => (window as any).__EVENTS)).toEqual([ + { type: 'mousedown', button: 1, buttons: 4 }, + { type: 'mousedown', button: 0, buttons: 5 }, + { type: 'mouseup', button: 1, buttons: 1 }, + { type: 'mouseup', button: 0, buttons: 0 }, + ]); +}); + it('should select the text with mouse', async ({ page, server }) => { await page.goto(server.PREFIX + '/input/textarea.html'); await page.focus('textarea');