Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ Options:
- `Optional` **pointer**: \"mouse\" \| \"pen\"
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
- `Optional` **button**: "left" | "middle" | "right" | "back" | "forward" | "none"

## cy.realMouseUp

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

## Coordinates

Expand Down
26 changes: 26 additions & 0 deletions cypress/integration/mouse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ describe("cy.realMouseDown and cy.realMouseUp", () => {
.realMouseDown({ position: "bottomRight" })
.realMouseUp({ position: "bottomRight" });
});

describe("options.button", () => {
it("should allow to press down mouse using middle button", (done) => {
cy.get(".action-btn")
.then(($button) => {
$button.get(0).addEventListener("mousedown", (ev) => {
if (ev.button === 1) {
done();
}
});
})
.realMouseDown({ button: "middle" });
});

it("should allow to release mouse using middle button", (done) => {
cy.get(".action-btn")
.then(($button) => {
$button.get(0).addEventListener("mouseup", (ev) => {
if (ev.button === 1) {
done();
}
});
})
.realMouseUp({ button: "middle" });
});
});

describe("realMouseDown scroll behavior", () => {
function getScreenEdges() {
Expand Down
4 changes: 2 additions & 2 deletions cypress/integration/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ describe("cy.realType", () => {
cy.get("input[name=q]").focus()
});

it("types text into googles main search inptu", () => {
it("types text into an input", () => {
cy.realType("cypress can produce real events");

cy.get("input[name=q]").should(
"have.value",
"cypress can produce real events"
Expand Down
36 changes: 36 additions & 0 deletions src/_internalState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {mouseButtonNumbers} from './mouseButtonNumbers'
Copy link
Contributor Author

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


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,
}
46 changes: 27 additions & 19 deletions src/commands/realClick.ts
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" })
Expand Down Expand Up @@ -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++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();

Expand Down
30 changes: 24 additions & 6 deletions src/commands/realHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
ScrollBehaviorOptions,
getCypressElementCoordinates,
} from "../getCypressElementCoordinates";
import { InternalState } from "../_internalState";

export interface RealHoverOptions {
/**
* If set to `pen`, simulates touch based hover (via long press)
*/
pointer?: "mouse" | "pen";
/**
/**
* Position relative to the element where to hover the element.
* @example cy.realHover({ position: "topLeft" })
*/
Expand All @@ -22,6 +23,25 @@ export interface RealHoverOptions {
scrollBehavior?: ScrollBehaviorOptions;
}

export async function rawMouseMove({
x,
y,
pointer = "mouse",
}: {
x: number,
y: number,
pointer?: "mouse" | "pen",
}) {
await fireCdpCommand("Input.dispatchMouseEvent", {
type: "mouseMoved",
pointerType: pointer,
button: "none",
buttons: InternalState.getButtonsMask(),
x,
y,
});
}

/** @ignore this, update documentation for this function at index.d.ts */
export async function realHover(
subject: JQuery,
Expand All @@ -38,13 +58,11 @@ export async function realHover(
}),
});

await fireCdpCommand("Input.dispatchMouseEvent", {
await rawMouseMove({
...options,
x,
y,
type: "mouseMoved",
button: "none",
pointerType: options.pointer ?? "mouse",
});
})

log.snapshot().end();

Expand Down
46 changes: 38 additions & 8 deletions src/commands/mouseDown.ts → src/commands/realMouseDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -18,6 +20,37 @@ export interface realMouseDownOptions {
* @example cy.realMouseDown({ scrollBehavior: "top" });
*/
scrollBehavior?: ScrollBehaviorOptions;

/**
* @default "left"
*/
button?: keyof typeof mouseButtonNumbers;
}

export async function rawMouseDown({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extracted this so the "composite" realClick command can benefit from the state tracking

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 */
Expand All @@ -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();

Expand Down
44 changes: 36 additions & 8 deletions src/commands/mouseUp.ts → src/commands/realMouseUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
ScrollBehaviorOptions,
Position,
} from "../getCypressElementCoordinates";
import { mouseButtonNumbers } from "../mouseButtonNumbers";
import { InternalState } from "../_internalState";

export interface realMouseUpOptions {
/** Pointer type for realMouseUp, if "pen" touch simulated */
Expand All @@ -18,6 +20,35 @@ export interface realMouseUpOptions {
* @example cy.realMouseUp({ scrollBehavior: "top" });
*/
scrollBehavior?: ScrollBehaviorOptions;
/**
* @default "left"
*/
button?: keyof typeof mouseButtonNumbers;
}

export async function rawMouseUp({
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: "mouseReleased",
clickCount,
buttons: InternalState.getButtonsMask(),
pointerType: pointer,
button,
x,
y
});
}

/** @ignore this, update documentation for this function at index.d.ts */
Expand All @@ -37,15 +68,12 @@ export async function realMouseUp(
});

log.snapshot("before");
await fireCdpCommand("Input.dispatchMouseEvent", {
type: "mouseReleased",

await rawMouseUp({
...options,
x,
y,
clickCount: 1,
buttons: 1,
pointerType: options.pointer ?? "mouse",
button: "left",
});
y
})

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

Expand Down
Loading