From 7a51bff47f6436fc29d0df7eb74f12f69102ca5b Mon Sep 17 00:00:00 2001
From: jrandolf <101637635+jrandolf@users.noreply.github.com>
Date: Wed, 27 Apr 2022 23:00:07 +0200
Subject: [PATCH] feat: add `back` and `forward` mouse buttons (#8284)
---
docs/api.md | 12 +++++------
src/common/Input.ts | 2 +-
src/common/JSHandle.ts | 4 +++-
test/assets/input/scrollable.html | 16 ++++++++++++++-
test/click.spec.ts | 27 +++++++++++++++++++++++++
utils/doclint/check_public_api/index.js | 6 +++---
6 files changed, 55 insertions(+), 12 deletions(-)
diff --git a/docs/api.md b/docs/api.md
index 6e04af65..513f05d6 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -1466,7 +1466,7 @@ Get the browser context that the page belongs to.
- `selector` <[string]> A [selector] to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked.
- `options` <[Object]>
- - `button` <"left"|"right"|"middle"> Defaults to `left`.
+ - `button` <"left"|"right"|"middle"|"back"|"forward"> Defaults to `left`.
- `clickCount` <[number]> defaults to 1. See [UIEvent.detail].
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. The Promise will be rejected if there is no element matching `selector`.
@@ -3508,7 +3508,7 @@ await browser
- `x` <[number]>
- `y` <[number]>
- `options` <[Object]>
- - `button` <"left"|"right"|"middle"> Defaults to `left`.
+ - `button` <"left"|"right"|"middle"|"back"|"forward"> Defaults to `left`.
- `clickCount` <[number]> defaults to 1. See [UIEvent.detail].
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- returns: <[Promise]>
@@ -3518,7 +3518,7 @@ Shortcut for [`mouse.move`](#mousemovex-y-options), [`mouse.down`](#mousedownopt
#### mouse.down([options])
- `options` <[Object]>
- - `button` <"left"|"right"|"middle"> Defaults to `left`.
+ - `button` <"left"|"right"|"middle"|"back"|"forward"> Defaults to `left`.
- `clickCount` <[number]> defaults to 1. See [UIEvent.detail].
- returns: <[Promise]>
@@ -3593,7 +3593,7 @@ Dispatches a `mousemove` event.
#### mouse.up([options])
- `options` <[Object]>
- - `button` <"left"|"right"|"middle"> Defaults to `left`.
+ - `button` <"left"|"right"|"middle"|"back"|"forward"> Defaults to `left`.
- `clickCount` <[number]> defaults to 1. See [UIEvent.detail].
- returns: <[Promise]>
@@ -3891,7 +3891,7 @@ Adds a `` tag into the page with the desired URL or a ` A [selector] to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked.
- `options` <[Object]>
- - `button` <"left"|"right"|"middle"> Defaults to `left`.
+ - `button` <"left"|"right"|"middle"|"back"|"forward"> Defaults to `left`.
- `clickCount` <[number]> defaults to 1. See [UIEvent.detail].
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully clicked. The Promise will be rejected if there is no element matching `selector`.
@@ -4654,7 +4654,7 @@ This method returns boxes of the element, or `null` if the element is not visibl
#### elementHandle.click([options])
- `options` <[Object]>
- - `button` <"left"|"right"|"middle"> Defaults to `left`.
+ - `button` <"left"|"right"|"middle"|"back"|"forward"> Defaults to `left`.
- `clickCount` <[number]> defaults to 1. See [UIEvent.detail].
- `delay` <[number]> Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0.
- `offset` <[Object]> Offset in pixels relative to the top-left corner of the border box of the element.
diff --git a/src/common/Input.ts b/src/common/Input.ts
index e3506cf3..03d02a57 100644
--- a/src/common/Input.ts
+++ b/src/common/Input.ts
@@ -280,7 +280,7 @@ export class Keyboard {
/**
* @public
*/
-export type MouseButton = 'left' | 'right' | 'middle';
+export type MouseButton = 'left' | 'right' | 'middle' | 'back' | 'forward';
/**
* @public
diff --git a/src/common/JSHandle.ts b/src/common/JSHandle.ts
index 9faf863f..9e7ec6d5 100644
--- a/src/common/JSHandle.ts
+++ b/src/common/JSHandle.ts
@@ -32,6 +32,8 @@ import {
UnwrapPromiseLike,
} from './EvalTypes.js';
import { isNode } from '../environment.js';
+import { MouseButton } from './Input.js';
+
/**
* @public
*/
@@ -1177,7 +1179,7 @@ export interface ClickOptions {
/**
* @defaultValue 'left'
*/
- button?: 'left' | 'right' | 'middle';
+ button?: MouseButton;
/**
* @defaultValue 1
*/
diff --git a/test/assets/input/scrollable.html b/test/assets/input/scrollable.html
index 885d3739..75757824 100644
--- a/test/assets/input/scrollable.html
+++ b/test/assets/input/scrollable.html
@@ -12,12 +12,26 @@
button.id = 'button-' + i;
button.onclick = () => button.textContent = 'clicked';
button.oncontextmenu = event => {
+ if (![2].includes(event.button)) {
+ return;
+ }
event.preventDefault();
button.textContent = 'context menu';
}
+ button.onmouseup = event => {
+ if (![1,3,4].includes(event.button)) {
+ return;
+ }
+ event.preventDefault();
+ button.textContent = {
+ 3: 'back click',
+ 4: 'forward click',
+ 1: 'aux click',
+ }[event.button];
+ }
document.body.appendChild(button);
document.body.appendChild(document.createElement('br'));
}