Skip to content

Commit 709da89

Browse files
feat: add x & y options for mouseDown and moustUp events (#486)
* Update mouseDown.ts Add X & Y coordinates to mouseDown * Update src/commands/mouseDown.ts * Update mouseUp as well --------- Co-authored-by: Dmitriy Kovalenko <[email protected]>
1 parent 98625af commit 709da89

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/commands/mouseDown.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ export interface realMouseDownOptions {
2525
* @default "left"
2626
*/
2727
button?: keyof typeof mouseButtonNumbers;
28+
29+
/** X coordinate to click, relative to the Element. Overrides `position`.
30+
* @example
31+
* cy.get("canvas").realMouseDown({ x: 100, y: 115 })
32+
* cy.get("body").realMouseDown({ x: 11, y: 12 }) // global click by coordinates
33+
*/
34+
x?: number;
35+
/** Y coordinate to click, relative to the Element. Overrides `position`.
36+
* @example
37+
* cy.get("canvas").realMouseDown({ x: 100, y: 115 })
38+
* cy.get("body").realMouseDown({ x: 11, y: 12 }) // global click by coordinates
39+
*/
40+
y?: number;
2841
/**
2942
* Indicates whether the shift key was pressed or not when an event occurred
3043
* @example cy.realMouseDown({ shiftKey: true });
@@ -37,7 +50,14 @@ export async function realMouseDown(
3750
subject: JQuery,
3851
options: realMouseDownOptions = {}
3952
) {
40-
const { x, y } = getCypressElementCoordinates(subject, options.position, options.scrollBehavior);
53+
const position =
54+
options.x && options.y ? { x: options.x, y: options.y } : options.position;
55+
56+
const { x, y } = getCypressElementCoordinates(
57+
subject,
58+
position,
59+
options.scrollBehavior
60+
);
4161

4262
const log = Cypress.log({
4363
$el: subject,

src/commands/mouseUp.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ export interface realMouseUpOptions {
2424
* @default "left"
2525
*/
2626
button?: keyof typeof mouseButtonNumbers;
27+
/** X coordinate to click, relative to the Element. Overrides `position`.
28+
* @example
29+
* cy.get("canvas").realMouseUp({ x: 100, y: 115 })
30+
* cy.get("body").realMouseUp({ x: 11, y: 12 }) // global click by coordinates
31+
*/
32+
x?: number;
33+
/** Y coordinate to click, relative to the Element. Overrides `position`.
34+
* @example
35+
* cy.get("canvas").realMouseUp({ x: 100, y: 115 })
36+
* cy.get("body").realMouseUp({ x: 11, y: 12 }) // global click by coordinates
37+
*/
38+
y?: number;
2739
/**
2840
* Indicates whether the shift key was pressed or not when an event occurred
2941
* @example cy.realMouseUp({ shiftKey: true });
@@ -36,7 +48,14 @@ export async function realMouseUp(
3648
subject: JQuery,
3749
options: realMouseUpOptions = {}
3850
) {
39-
const { x, y } = getCypressElementCoordinates(subject, options.position, options.scrollBehavior);
51+
const position =
52+
options.x && options.y ? { x: options.x, y: options.y } : options.position;
53+
54+
const { x, y } = getCypressElementCoordinates(
55+
subject,
56+
position,
57+
options.scrollBehavior
58+
);
4059

4160
const log = Cypress.log({
4261
$el: subject,

src/commands/realClick.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ export async function realClick(
5151
subject: JQuery,
5252
options: RealClickOptions = {}
5353
) {
54-
// prettier-ignore
55-
const position = options.x && options.y
56-
? { x: options.x, y: options.y }
57-
: options.position;
54+
const position =
55+
options.x && options.y ? { x: options.x, y: options.y } : options.position;
5856

5957
const { x, y } = getCypressElementCoordinates(
6058
subject,
@@ -73,7 +71,7 @@ export async function realClick(
7371

7472
log.snapshot("before");
7573

76-
const { clickCount = 1 } = options
74+
const { clickCount = 1 } = options;
7775

7876
for (let currentClick = 1; currentClick <= clickCount; currentClick++) {
7977
await fireCdpCommand("Input.dispatchMouseEvent", {

0 commit comments

Comments
 (0)