-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: extend Cypress.Keyboard.Keys and cy.press to support (almost) all keyboard keys #31496
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
Changes from 32 commits
efabb5a
88b8f76
c4af6b6
70ecad0
4a84ff3
2485043
935d766
6df79a8
bb031ba
e9abde8
bde74b4
eddde62
6109992
385ecc9
54f4f26
16a44f2
bad947c
9656b4c
f5fb925
8bf80af
32cc334
df6ef92
aa9a386
88d776c
0944c46
485e7d6
af4c50c
166e305
707adf0
869912a
f63b212
cb9d042
d43600b
d9085ff
47ed81f
311dc66
03a840e
3878191
58d7804
e413eda
764a438
fd8fd80
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,29 @@ | ||
declare namespace Cypress { | ||
type SupportedNamedKey = 'ArrowDown' | | ||
'ArrowLeft' | | ||
'ArrowRight' | | ||
'ArrowUp' | | ||
'End' | | ||
'Home' | | ||
'PageDown' | | ||
'PageUp' | | ||
'Enter' | | ||
'Tab' | | ||
'Backspace' | | ||
'Delete' | | ||
'Insert' | | ||
'F1' | | ||
'F2' | | ||
'F3' | | ||
'F4' | | ||
'F5' | | ||
'F6' | | ||
'F7' | | ||
'F8' | | ||
'F9' | | ||
'F10' | | ||
'F11' | | ||
'F12' | ||
|
||
type SupportedKey = SupportedNamedKey | string | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,53 @@ | ||
describe('src/cy/commands/actions/press', () => { | ||
it('dispatches the tab keypress to the AUT', () => { | ||
// Non-BiDi firefox is not supported | ||
if (Cypress.browser.family === 'firefox' && Cypress.browserMajorVersion() < 135) { | ||
return | ||
} | ||
// Non-BiDi firefox is not supported | ||
if (Cypress.browser.family === 'firefox' && Cypress.browserMajorVersion() < 135) { | ||
|
||
return | ||
} | ||
|
||
// TODO: Webkit is not supported. https://github.com/cypress-io/cypress/issues/31054 | ||
if (Cypress.isBrowser('webkit')) { | ||
return | ||
} | ||
// TODO: Webkit is not supported. https://github.com/cypress-io/cypress/issues/31054 | ||
if (Cypress.isBrowser('webkit')) { | ||
return | ||
} | ||
|
||
beforeEach(() => { | ||
cy.visit('/fixtures/input_events.html') | ||
}) | ||
|
||
const testKeyDownUp = (key) => { | ||
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. Should 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. It really doesn't matter here. |
||
it(`dispatches ${key} keypress to the AUT`, () => { | ||
cy.press(key) | ||
cy.get('#keydown').should('have.value', key) | ||
|
||
// in some browsers, F6 will cause the frame to lose focus, so the keyup will not be triggered | ||
if (key !== 'F6') { | ||
cy.get('#keyup').should('have.value', key) | ||
} | ||
|
||
// named keys are not dispatched as keypress events | ||
if (!Object.values(Cypress.Keyboard.Keys).includes(key)) { | ||
cy.get('#keypress').should('have.value', key) | ||
} | ||
}) | ||
} | ||
|
||
// // Numbers | ||
;['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].forEach(testKeyDownUp) | ||
|
||
// // Letters | ||
;['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'].forEach(testKeyDownUp) | ||
|
||
cy.press(Cypress.Keyboard.Keys.TAB) | ||
// // Special characters | ||
;['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', | ||
'+', '[', ']', '{', '}', '\\', '|', ';', ':', '\'', '"', ',', '.', | ||
'<', '>', '/', '?', '`', '~', ' ', '€', 'é'].forEach(testKeyDownUp) | ||
|
||
cacieprins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cy.get('#keydown').should('have.value', 'Tab') | ||
Object.values(Cypress.Keyboard.Keys).forEach(testKeyDownUp) | ||
|
||
cy.get('#keyup').should('have.value', 'Tab') | ||
it('dispatches the input event when an input is modified via cy.press', () => { | ||
cy.get('#input_source').focus() | ||
cy.press('a') | ||
cy.get('#input_source').should('have.value', 'a') | ||
cy.get('#input').should('have.value', 'a') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import type { $Cy } from '../../../cypress/cy' | ||
import type { StateFunc } from '../../../cypress/state' | ||
import type { KeyPressSupportedKeys, AutomationCommands } from '@packages/types' | ||
import { isSupportedKey, SupportedKey, AutomationCommands } from '@packages/types' | ||
import { defaults } from 'lodash' | ||
import { isSupportedKey } from '@packages/server/lib/automation/commands/key_press' | ||
import $errUtils from '../../../cypress/error_utils' | ||
import $utils from '../../../cypress/utils' | ||
|
||
export interface PressCommand { | ||
(key: KeyPressSupportedKeys, userOptions?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable>): void | ||
(key: SupportedKey | string, userOptions?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable>): void | ||
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. Is the 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. No, because |
||
} | ||
|
||
export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, cy: $Cy, state: StateFunc, config: any) { | ||
async function pressCommand (key: KeyPressSupportedKeys, userOptions?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable>) { | ||
async function pressCommand (key: SupportedKey | string, userOptions?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable>) { | ||
const options: Cypress.Loggable & Partial<Cypress.Timeoutable> = defaults({}, userOptions, { | ||
log: true, | ||
}) | ||
|
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.
likely need to bump the version to
15.1.0
in the changelog. I can do that in the release PR if needed