Skip to content
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

feat: Add shiftKey modifier to keyboard events #514

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions cypress/e2e/modifiers.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
describe("Events behavior with shiftKey modifier applied", () => {
beforeEach(() => {
cy.visit("./cypress/fixtures/modifiers-test.html");
});

it("detects shift key modifier on click", () => {
cy.get("#action-button").realClick({ shiftKey: true });
cy.contains("Shift key was pressed");
});

it("detects shift key modifier on hover", () => {
cy.get("#mouse-move-div").realHover({ shiftKey: true });
cy.contains("Shift key was pressed");
});

it("detects shift key modifier on mousedown", () => {
cy.get("#mouse-down-div").realMouseDown({ shiftKey: true });
cy.contains("Shift key was pressed");
});

it("detects shift key modifier on mousemove", () => {
cy.get("#mouse-move-div").realMouseMove(100, 50, { shiftKey: true });
cy.contains("Shift key was pressed");
});
});
40 changes: 40 additions & 0 deletions cypress/fixtures/modifiers-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<body>
<button id="action-button">Click on me with shift pressed</button>
<hr />
<div id="mouse-move-div">Hover/mousemove over me with shift pressed</div>
<hr />
<div id="mouse-down-div">Initiate mouse down with shift pressed</div>
<hr />

<p id="result"></p>
</body>
<script>
const resultBox = document.getElementById("result");

function commonHandler(e) {
resultBox.innerHTML = "";
if (e.shiftKey) resultBox.innerHTML = "Shift key was pressed";
}

document
.getElementById("action-button")
.addEventListener("click", commonHandler);

document
.getElementById("mouse-move-div")
.addEventListener("mousemove", commonHandler);

document
.getElementById("mouse-down-div")
.addEventListener("mousedown", commonHandler);
</script>
<style>
div {
width: 100%;
height: 100px;
background-color: aqua;
}
</style>
</html>
13 changes: 12 additions & 1 deletion src/commands/mouseDown.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 { keyToModifierBitMap } from "../keyToModifierBitMap";

export interface realMouseDownOptions {
/** Pointer type for realMouseDown, if "pen" touch simulated */
Expand All @@ -24,14 +25,23 @@ export interface realMouseDownOptions {
* @default "left"
*/
button?: keyof typeof mouseButtonNumbers;
/**
* Indicates whether the shift key was pressed or not when an event occurred
* @example cy.realMouseDown({ shiftKey: true });
*/
shiftKey?: boolean;
}

/** @ignore this, update documentation for this function at index.d.ts */
export async function realMouseDown(
subject: JQuery,
options: realMouseDownOptions = {}
) {
const { x, y } = getCypressElementCoordinates(subject, options.position, options.scrollBehavior);
const { x, y } = getCypressElementCoordinates(
subject,
options.position,
options.scrollBehavior
);

const log = Cypress.log({
$el: subject,
Expand All @@ -51,6 +61,7 @@ export async function realMouseDown(
buttons: mouseButtonNumbers[options.button ?? "left"],
pointerType: options.pointer ?? "mouse",
button: options.button ?? "left",
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
});

log.snapshot("after").end();
Expand Down
15 changes: 11 additions & 4 deletions src/commands/mouseMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Position,
ScrollBehaviorOptions,
} from "../getCypressElementCoordinates";
import { keyToModifierBitMap } from "../keyToModifierBitMap";

export interface RealMouseMoveOptions {
/**
Expand All @@ -14,19 +15,24 @@ export interface RealMouseMoveOptions {
position?: Position;
/**
* Controls how the page is scrolled to bring the subject into view, if needed.
* @example cy.realClick({ scrollBehavior: "top" });
* @example cy.realMouseMove({ scrollBehavior: "top" });
*/
scrollBehavior?: ScrollBehaviorOptions;
/**
* Indicates whether the shift key was pressed or not when an event occurred
* @example cy.realMouseMove({ shiftKey: true });
*/
shiftKey?: boolean;
}

/** @ignore this, update documentation for this function at index.d.ts */
export async function realMouseMove(
subject: JQuery,
x: number ,
x: number,
y: number,
options: RealMouseMoveOptions = {}
) {
const basePosition= getCypressElementCoordinates(
const basePosition = getCypressElementCoordinates(
subject,
options.position ?? "topLeft",
options.scrollBehavior
Expand All @@ -46,9 +52,10 @@ export async function realMouseMove(
type: "mouseMoved",
x: x * basePosition.frameScale + basePosition.x,
y: y * basePosition.frameScale + basePosition.y,
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
});

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

return subject;
}
}
10 changes: 9 additions & 1 deletion src/commands/realClick.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 { keyToModifierBitMap } from "../keyToModifierBitMap";

export interface RealClickOptions {
/** Pointer type for realClick, if "pen" touch simulated */
Expand Down Expand Up @@ -38,6 +39,11 @@ export interface RealClickOptions {
* @example cy.realClick({ clickCount: 2 });
*/
clickCount?: number;
/**
* Indicates whether the shift key was pressed or not when an event occurred
* @example cy.realClick({ shiftKey: true });
*/
shiftKey?: boolean;
}

/** @ignore this, update documentation for this function at index.d.ts */
Expand Down Expand Up @@ -67,7 +73,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 All @@ -78,6 +84,7 @@ export async function realClick(
buttons: mouseButtonNumbers[options.button ?? "left"],
pointerType: options.pointer ?? "mouse",
button: options.button ?? "left",
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
});

await fireCdpCommand("Input.dispatchMouseEvent", {
Expand All @@ -88,6 +95,7 @@ export async function realClick(
buttons: mouseButtonNumbers[options.button ?? "left"],
pointerType: options.pointer ?? "mouse",
button: options.button ?? "left",
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
});
}

Expand Down
15 changes: 13 additions & 2 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 { keyToModifierBitMap } from "../keyToModifierBitMap";

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 @@ -20,14 +21,23 @@ export interface RealHoverOptions {
* @example cy.realHover({ scrollBehavior: "top" });
*/
scrollBehavior?: ScrollBehaviorOptions;
/**
* Indicates whether the shift key was pressed or not when an event occurred
* @example cy.realHover({ shiftKey: true });
*/
shiftKey?: boolean;
}

/** @ignore this, update documentation for this function at index.d.ts */
export async function realHover(
subject: JQuery,
options: RealHoverOptions = {}
) {
const { x, y } = getCypressElementCoordinates(subject, options.position, options.scrollBehavior);
const { x, y } = getCypressElementCoordinates(
subject,
options.position,
options.scrollBehavior
);

const log = Cypress.log({
$el: subject,
Expand All @@ -44,6 +54,7 @@ export async function realHover(
type: "mouseMoved",
button: "none",
pointerType: options.pointer ?? "mouse",
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
});

log.snapshot().end();
Expand Down
8 changes: 1 addition & 7 deletions src/commands/realPress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fireCdpCommand } from "../fireCdpCommand";
import { keyCodeDefinitions } from "../keyCodeDefinitions";
import { keyToModifierBitMap } from "../keyToModifierBitMap";

export interface RealPressOptions {
/**
Expand Down Expand Up @@ -34,13 +35,6 @@ function getKeyDefinition(key: keyof typeof keyCodeDefinitions) {
};
}

const keyToModifierBitMap: Record<string, number> = {
Alt: 1,
Control: 2,
Meta: 4,
Shift: 8,
};

type Key = keyof typeof keyCodeDefinitions;
// unfortunately passing a string like Shift+P is not possible cause typescript template literals can not handle such giant union
type KeyOrShortcut = Key | Array<Key>;
Expand Down
6 changes: 6 additions & 0 deletions src/keyToModifierBitMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const keyToModifierBitMap: Record<string, number> = {
Alt: 1,
Control: 2,
Meta: 4,
Shift: 8,
};