Skip to content

Commit

Permalink
feat: add x & y options for mouseDown and moustUp events (#486)
Browse files Browse the repository at this point in the history
* Update mouseDown.ts

Add X & Y coordinates to mouseDown

* Update src/commands/mouseDown.ts

* Update mouseUp as well

---------

Co-authored-by: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com>
  • Loading branch information
adamistheanswer and dmtrKovalenko committed Jul 17, 2023
1 parent 769273d commit 1b65608
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/commands/mouseDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ export interface realMouseDownOptions {
* @default "left"
*/
button?: keyof typeof mouseButtonNumbers;

/** X coordinate to click, relative to the Element. Overrides `position`.
* @example
* cy.get("canvas").realMouseDown({ x: 100, y: 115 })
* cy.get("body").realMouseDown({ x: 11, y: 12 }) // global click by coordinates
*/
x?: number;
/** Y coordinate to click, relative to the Element. Overrides `position`.
* @example
* cy.get("canvas").realMouseDown({ x: 100, y: 115 })
* cy.get("body").realMouseDown({ x: 11, y: 12 }) // global click by coordinates
*/
y?: number;
/**
* Indicates whether the shift key was pressed or not when an event occurred
* @example cy.realMouseDown({ shiftKey: true });
Expand All @@ -37,7 +50,14 @@ export async function realMouseDown(
subject: JQuery,
options: realMouseDownOptions = {}
) {
const { x, y } = getCypressElementCoordinates(subject, options.position, options.scrollBehavior);
const position =
options.x && options.y ? { x: options.x, y: options.y } : options.position;

const { x, y } = getCypressElementCoordinates(
subject,
position,
options.scrollBehavior
);

const log = Cypress.log({
$el: subject,
Expand Down
21 changes: 20 additions & 1 deletion src/commands/mouseUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ export interface realMouseUpOptions {
* @default "left"
*/
button?: keyof typeof mouseButtonNumbers;
/** X coordinate to click, relative to the Element. Overrides `position`.
* @example
* cy.get("canvas").realMouseUp({ x: 100, y: 115 })
* cy.get("body").realMouseUp({ x: 11, y: 12 }) // global click by coordinates
*/
x?: number;
/** Y coordinate to click, relative to the Element. Overrides `position`.
* @example
* cy.get("canvas").realMouseUp({ x: 100, y: 115 })
* cy.get("body").realMouseUp({ x: 11, y: 12 }) // global click by coordinates
*/
y?: number;
/**
* Indicates whether the shift key was pressed or not when an event occurred
* @example cy.realMouseUp({ shiftKey: true });
Expand All @@ -36,7 +48,14 @@ export async function realMouseUp(
subject: JQuery,
options: realMouseUpOptions = {}
) {
const { x, y } = getCypressElementCoordinates(subject, options.position, options.scrollBehavior);
const position =
options.x && options.y ? { x: options.x, y: options.y } : options.position;

const { x, y } = getCypressElementCoordinates(
subject,
position,
options.scrollBehavior
);

const log = Cypress.log({
$el: subject,
Expand Down
8 changes: 3 additions & 5 deletions src/commands/realClick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ export async function realClick(
subject: JQuery,
options: RealClickOptions = {}
) {
// prettier-ignore
const position = options.x && options.y
? { x: options.x, y: options.y }
: options.position;
const position =
options.x && options.y ? { x: options.x, y: options.y } : options.position;

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

log.snapshot("before");

const { clickCount = 1 } = options
const { clickCount = 1 } = options;

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

0 comments on commit 1b65608

Please sign in to comment.