Skip to content

Commit cad18f1

Browse files
committed
feat(mouse button): Allow to specify button option for realMouseDown and realMouseUp
1 parent a6b3f6c commit cad18f1

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ Options:
271271
- `Optional` **pointer**: \"mouse\" \| \"pen\"
272272
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
273273
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
274+
- `Optional` **button**: "left" | "middle" | "right" | "back" | "forward" | "none"
274275

275276
## cy.realMouseUp
276277

@@ -292,6 +293,7 @@ Options:
292293
- `Optional` **pointer**: \"mouse\" \| \"pen\"
293294
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
294295
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
296+
- `Optional` **button**: "left" | "middle" | "right" | "back" | "forward" | "none"
295297

296298
## Coordinates
297299

cypress/integration/mouse.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ describe("cy.realMouseDown and cy.realMouseUp", () => {
3636
.realMouseDown({ position: "bottomRight" })
3737
.realMouseUp({ position: "bottomRight" });
3838
});
39+
40+
describe("options.button", () => {
41+
it("should allow to press down mouse using middle button", (done) => {
42+
cy.get(".action-btn")
43+
.then(($button) => {
44+
$button.get(0).addEventListener("mousedown", (ev) => {
45+
if (ev.button === 1) {
46+
done();
47+
}
48+
});
49+
})
50+
.realMouseDown({ button: "middle" });
51+
});
52+
53+
it("should allow to release mouse using middle button", (done) => {
54+
cy.get(".action-btn")
55+
.then(($button) => {
56+
$button.get(0).addEventListener("mouseup", (ev) => {
57+
if (ev.button === 1) {
58+
done();
59+
}
60+
});
61+
})
62+
.realMouseUp({ button: "middle" });
63+
});
64+
});
3965

4066
describe("realMouseDown scroll behavior", () => {
4167
function getScreenEdges() {

src/commands/mouseDown.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ScrollBehaviorOptions,
55
Position,
66
} from "../getCypressElementCoordinates";
7+
import { mouseButtonNumbers } from "../mouseButtonNumbers";
78

89
export interface realMouseDownOptions {
910
/** Pointer type for realMouseDown, if "pen" touch simulated */
@@ -18,6 +19,11 @@ export interface realMouseDownOptions {
1819
* @example cy.realMouseDown({ scrollBehavior: "top" });
1920
*/
2021
scrollBehavior?: ScrollBehaviorOptions;
22+
23+
/**
24+
* @default "left"
25+
*/
26+
button?: keyof typeof mouseButtonNumbers;
2127
}
2228

2329
/** @ignore this, update documentation for this function at index.d.ts */
@@ -42,9 +48,9 @@ export async function realMouseDown(
4248
x,
4349
y,
4450
clickCount: 1,
45-
buttons: 1,
51+
buttons: mouseButtonNumbers[options.button ?? "left"],
4652
pointerType: options.pointer ?? "mouse",
47-
button: "left",
53+
button: options.button ?? "left",
4854
});
4955

5056
log.snapshot("after").end();

src/commands/mouseUp.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ScrollBehaviorOptions,
55
Position,
66
} from "../getCypressElementCoordinates";
7+
import { mouseButtonNumbers } from "../mouseButtonNumbers";
78

89
export interface realMouseUpOptions {
910
/** Pointer type for realMouseUp, if "pen" touch simulated */
@@ -18,6 +19,10 @@ export interface realMouseUpOptions {
1819
* @example cy.realMouseUp({ scrollBehavior: "top" });
1920
*/
2021
scrollBehavior?: ScrollBehaviorOptions;
22+
/**
23+
* @default "left"
24+
*/
25+
button?: keyof typeof mouseButtonNumbers;
2126
}
2227

2328
/** @ignore this, update documentation for this function at index.d.ts */
@@ -42,9 +47,9 @@ export async function realMouseUp(
4247
x,
4348
y,
4449
clickCount: 1,
45-
buttons: 1,
50+
buttons: mouseButtonNumbers[options.button ?? "left"],
4651
pointerType: options.pointer ?? "mouse",
47-
button: "left",
52+
button: options.button ?? "left",
4853
});
4954

5055
log.snapshot("after").end();

src/mouseButtonNumbers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const mouseButtonNumbers = {
2+
"none": 0,
3+
"left": 1,
4+
"right": 2,
5+
"middle": 4,
6+
"back": 8,
7+
"forward": 16,
8+
}

0 commit comments

Comments
 (0)