-
-
Notifications
You must be signed in to change notification settings - Fork 71
chore(commands): laid out groundwork for state tracking #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a6b3f6c
7ee4683
b05948e
48a22b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {mouseButtonNumbers} from './mouseButtonNumbers' | ||
|
||
const mouseButtons = new Set<keyof typeof mouseButtonNumbers>() | ||
|
||
function getButtonsMask(): number { | ||
let mask = 0; | ||
if (mouseButtons.has('left')) { | ||
mask |= 1; | ||
} | ||
if (mouseButtons.has('right')) { | ||
mask |= 2; | ||
} | ||
if (mouseButtons.has('middle')) { | ||
mask |= 4; | ||
} | ||
if (mouseButtons.has('back')) { | ||
mask |= 8; | ||
} | ||
if (mouseButtons.has('forward')) { | ||
mask |= 16; | ||
} | ||
return mask; | ||
} | ||
|
||
export const InternalState = { | ||
clear() { | ||
mouseButtons.clear() | ||
}, | ||
mouseButtonDown(button: keyof typeof mouseButtonNumbers) { | ||
mouseButtons.add(button) | ||
}, | ||
mouseButtonUp(button: keyof typeof mouseButtonNumbers) { | ||
mouseButtons.delete(button) | ||
}, | ||
getButtonsMask, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { fireCdpCommand } from "../fireCdpCommand"; | ||
import { | ||
getCypressElementCoordinates, | ||
ScrollBehaviorOptions, | ||
Position, | ||
} from "../getCypressElementCoordinates"; | ||
import { rawMouseDown } from './realMouseDown' | ||
import { rawMouseUp } from './realMouseUp' | ||
import { rawMouseMove } from './realHover' | ||
import { mouseButtonNumbers } from "../mouseButtonNumbers"; | ||
|
||
export interface RealClickOptions { | ||
/** Pointer type for realClick, if "pen" touch simulated */ | ||
pointer?: "mouse" | "pen"; | ||
/** The button on mouse that clicked. Simulates real browser behavior. */ | ||
button?: "none" | "left" | "right" | "middle" | "back" | "forward"; | ||
button?: keyof typeof mouseButtonNumbers; | ||
/** | ||
* Position of the click event relative to the element | ||
* @example cy.realClick({ position: "topLeft" }) | ||
|
@@ -65,25 +68,30 @@ export async function realClick( | |
}); | ||
|
||
log.snapshot("before"); | ||
await fireCdpCommand("Input.dispatchMouseEvent", { | ||
type: "mousePressed", | ||
x, | ||
y, | ||
clickCount: options.clickCount ?? 1, | ||
buttons: 1, | ||
pointerType: options.pointer ?? "mouse", | ||
button: options.button ?? "left", | ||
}); | ||
|
||
await fireCdpCommand("Input.dispatchMouseEvent", { | ||
type: "mouseReleased", | ||
await rawMouseMove({ | ||
...options, | ||
x, | ||
y, | ||
clickCount: options.clickCount ?? 1, | ||
buttons: 1, | ||
pointerType: options.pointer ?? "mouse", | ||
button: options.button ?? "left", | ||
}); | ||
y | ||
}) | ||
|
||
const { clickCount = 1 } = options | ||
|
||
for (let currentClick = 1; currentClick <= clickCount; currentClick++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this loop is a fix - gonna prepare a separate PR with this in a moment and when it lands I will rebase this PR on top of that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And there it is: #212 |
||
await rawMouseDown({ | ||
...options, | ||
x, | ||
y, | ||
clickCount: currentClick | ||
}) | ||
|
||
await rawMouseUp({ | ||
...options, | ||
x, | ||
y, | ||
clickCount: currentClick | ||
}) | ||
} | ||
|
||
log.snapshot("after").end(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import { | |
ScrollBehaviorOptions, | ||
Position, | ||
} from "../getCypressElementCoordinates"; | ||
import { mouseButtonNumbers } from "../mouseButtonNumbers"; | ||
import { InternalState } from "../_internalState"; | ||
|
||
export interface realMouseDownOptions { | ||
/** Pointer type for realMouseDown, if "pen" touch simulated */ | ||
|
@@ -18,6 +20,37 @@ export interface realMouseDownOptions { | |
* @example cy.realMouseDown({ scrollBehavior: "top" }); | ||
*/ | ||
scrollBehavior?: ScrollBehaviorOptions; | ||
|
||
/** | ||
* @default "left" | ||
*/ | ||
button?: keyof typeof mouseButtonNumbers; | ||
} | ||
|
||
export async function rawMouseDown({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've extracted this so the "composite" |
||
x, | ||
y, | ||
button = "left", | ||
pointer = "mouse", | ||
clickCount = 1, | ||
}: { | ||
x: number, | ||
y: number, | ||
button?: keyof typeof mouseButtonNumbers, | ||
pointer?: "mouse" | "pen", | ||
clickCount?: number | ||
}) { | ||
InternalState.mouseButtonUp(button) | ||
|
||
await fireCdpCommand("Input.dispatchMouseEvent", { | ||
type: "mousePressed", | ||
clickCount, | ||
buttons: InternalState.getButtonsMask(), | ||
pointerType: pointer, | ||
button, | ||
x, | ||
y, | ||
}); | ||
} | ||
|
||
/** @ignore this, update documentation for this function at index.d.ts */ | ||
|
@@ -37,15 +70,12 @@ export async function realMouseDown( | |
}); | ||
|
||
log.snapshot("before"); | ||
await fireCdpCommand("Input.dispatchMouseEvent", { | ||
type: "mousePressed", | ||
|
||
await rawMouseDown({ | ||
...options, | ||
x, | ||
y, | ||
clickCount: 1, | ||
buttons: 1, | ||
pointerType: options.pointer ?? "mouse", | ||
button: "left", | ||
}); | ||
y | ||
}) | ||
|
||
log.snapshot("after").end(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the main reason behind the PR - I'd like to make "state" (like button mask and pressed keys) to be tracked by the library, it's a chore to maintain this on the consumer's side and this would match how Playwright works