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: Make cy.realPress {enter} to be functional for accessibility navigation #69

Merged
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
60 changes: 34 additions & 26 deletions cypress/integration/press.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,55 @@ describe("cy.realPress", () => {
cy.realPress("Tab");
cy.get("[aria-label='Search by voice']").should("be.focused");
});

it("Can use Enter for a11y navigation", () => {
cy.get("input[name=q]").focus();
cy.realType("something");
cy.realPress("Tab");

cy.get("input[name=q]").should("have.value", "something");
cy.realPress("Enter");
cy.get("input[name=q]").should("have.value", "");
});
});

context("shortcuts", () => {
beforeEach(() => {
cy.visit("https://wangchujiang.com/hotkeys/")
cy.get('[data-key=27]').realClick() // just activate the listener
})

cy.visit("https://wangchujiang.com/hotkeys/");
cy.get("[data-key=27]").realClick(); // just activate the listener
});

it("Can fire shortcuts", () => {

cy.realPress(["Control", "Shift", "R"]);
cy.realPress(["Alt", "Shift", "F5"]);
});

it("Fires correct js events", () => {
cy.document().then(document => {
document.addEventListener("keyup", e => {
expect(e.isTrusted).to.be.true
expect(e.shiftKey).to.be.true
expect(e.altKey).to.be.true
cy.document().then((document) => {
document.addEventListener("keyup", (e) => {
expect(e.isTrusted).to.be.true;
expect(e.shiftKey).to.be.true;
expect(e.altKey).to.be.true;

if (e.key === "Alt") {
expect(e.altKey).to.be.true
expect(e.code).to.eq("AltLeft")
expect(e.keyCode).to.eq(18)
if (e.key === "Alt") {
expect(e.altKey).to.be.true;
expect(e.code).to.eq("AltLeft");
expect(e.keyCode).to.eq(18);
}
if (e.key === "Shift") {
expect(e.altKey).to.be.true
expect(e.code).to.eq("ShiftLeft")
expect(e.keyCode).to.eq(16)
if (e.key === "Shift") {
expect(e.altKey).to.be.true;
expect(e.code).to.eq("ShiftLeft");
expect(e.keyCode).to.eq(16);
}
if (e.key === "F5") {
expect(e.altKey).to.be.true
expect(e.code).to.eq("F5")
expect(e.keyCode).to.eq(116)
if (e.key === "F5") {
expect(e.altKey).to.be.true;
expect(e.code).to.eq("F5");
expect(e.keyCode).to.eq(116);
}
})
})
});
});

cy.realPress(["Alt", "Shift", "F5"]);
})
});
});
});
9 changes: 8 additions & 1 deletion src/commands/realPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ export async function realPress(

for (const key of keyDefinitions) {
modifiers |= keyToModifierBitMap[key.key] ?? 0;

await fireCdpCommand("Input.dispatchKeyEvent", {
type: key.text ? "keyDown" : "rawKeyDown",
modifiers,
enter:
key.code === "Enter"
? {
type: "char",
unmodifiedText: "\r",
text: "\r",
}
: {},
...key,
});

Expand Down
1 change: 1 addition & 0 deletions src/keyCodeDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const keyCodeDefinitions = {
'Numpad5': {'keyCode': 12, 'shiftKeyCode': 101, 'key': 'Clear', 'code': 'Numpad5', 'shiftKey': '5', 'location': 3},
'NumpadEnter': {'keyCode': 13, 'code': 'NumpadEnter', 'key': 'Enter', 'text': '\r', 'location': 3},
'{enter}': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
'Enter': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
'\r': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
'\n': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
'ShiftLeft': {'keyCode': 16, 'code': 'ShiftLeft', 'key': 'Shift', 'location': 1},
Expand Down