-
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 all 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,18 @@ | ||
declare namespace Cypress { | ||
type SupportedNamedKey = 'ArrowDown' | | ||
'ArrowLeft' | | ||
'ArrowRight' | | ||
'ArrowUp' | | ||
'End' | | ||
'Home' | | ||
'PageDown' | | ||
'PageUp' | | ||
'Space' | | ||
'Enter' | | ||
'Tab' | | ||
'Backspace' | | ||
'Delete' | | ||
'Insert' | ||
|
||
type SupportedKey = SupportedNamedKey | string | number | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,49 @@ | ||
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 | ||
} | ||
|
||
// 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') | ||
}) | ||
|
||
cy.press(Cypress.Keyboard.Keys.TAB) | ||
|
||
cy.get('#keydown').should('have.value', 'Tab') | ||
it('fires the click event on the button when the named key is sent', () => { | ||
cy.get('#button').focus() | ||
cy.get('#button').should('be.focused') | ||
cy.press(Cypress.Keyboard.Keys.SPACE) | ||
cy.get('#checkbox').should('be.checked') | ||
}) | ||
|
||
cy.get('#keyup').should('have.value', 'Tab') | ||
it('fires the click event on the button when a space is sent', () => { | ||
cy.get('#button').focus() | ||
cy.get('#button').should('be.focused') | ||
cy.press(' ') | ||
cy.get('#checkbox').should('be.checked') | ||
}) | ||
|
||
const testKeyDownUp = (key) => { | ||
it(`dispatches ${key} keypress to the AUT`, () => { | ||
cy.press(key) | ||
// spacebar is a special case - it's both a named key and a single character, | ||
// but when we dispatch the named key (via codepoint in bidi, via `Space` in CDP) | ||
// we get the space character, not the name of the key. | ||
cy.get('#keydown').should('have.value', key === 'Space' ? ' ' : key) | ||
}) | ||
} | ||
|
||
Object.values(Cypress.Keyboard.Keys).forEach(testKeyDownUp) | ||
|
||
// sets truncated for speed | ||
|
||
cacieprins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// // Numbers | ||
;['0', '1'].forEach(testKeyDownUp) | ||
|
||
;[0, 1].forEach(testKeyDownUp) | ||
|
||
// // Letters | ||
;['a', 'z'].forEach(testKeyDownUp) | ||
|
||
// // Special characters | ||
;['!', ' ', '€', 'é'].forEach(testKeyDownUp) | ||
}) |
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 | number, userOptions?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable>) { | ||
const options: Cypress.Loggable & Partial<Cypress.Timeoutable> = defaults({}, userOptions, { | ||
log: true, | ||
}) | ||
|
@@ -50,17 +49,6 @@ export default function (Commands: Cypress.Commands, Cypress: Cypress.Cypress, c | |
return null | ||
} | ||
|
||
if (Cypress.browser.name === 'firefox' && Number(Cypress.browser.majorVersion) < 135) { | ||
$errUtils.throwErrByPath('press.unsupported_browser_version', { | ||
onFail: log, | ||
args: { | ||
browser: Cypress.browser.name, | ||
version: Cypress.browser.majorVersion, | ||
minimumVersion: 135, | ||
}, | ||
}) | ||
} | ||
|
||
try { | ||
const command: 'key:press' = 'key:press' | ||
const args: AutomationCommands[typeof command]['dataType'] = { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Should
key
be typed?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.
It really doesn't matter here.