Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
chore(commands): laid out groundwork for state tracking
  • Loading branch information
Andarist committed Dec 27, 2021
commit 48a22b4be99cf73549f97a8da0a3c6d36fc2700a
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
40 changes: 32 additions & 8 deletions src/commands/mouseDown.ts → src/commands/realMouseDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Position,
} from "../getCypressElementCoordinates";
import { mouseButtonNumbers } from "../mouseButtonNumbers";
import { InternalState } from "../_internalState";

export interface realMouseDownOptions {
/** Pointer type for realMouseDown, if "pen" touch simulated */
Expand All @@ -26,6 +27,32 @@ export interface realMouseDownOptions {
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 */
export async function realMouseDown(
subject: JQuery,
Expand All @@ -43,15 +70,12 @@ export async function realMouseDown(
});

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

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

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

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

export interface realMouseUpOptions {
/** Pointer type for realMouseUp, if "pen" touch simulated */
Expand All @@ -25,6 +26,31 @@ export interface realMouseUpOptions {
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 */
export async function realMouseUp(
subject: JQuery,
Expand All @@ -42,15 +68,12 @@ export async function realMouseUp(
});

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

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

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

Expand Down
Loading