-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: cy.press() #31398
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
Merged
Merged
feat: cy.press() #31398
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
c3f4f85
wip - cy.press() command
cacieprins 5abbb60
cy command for dispatching key press/down/up events
cacieprins 6f5da33
unit tests and failure cases for cy.press()
cacieprins 4748f31
Cypress.Keyboard.Keys definition; fix command log message
cacieprins f0f5dc0
add keys to the internal keyboard type
cacieprins 98063fc
auto-focus in cdp
cacieprins 7ec4c8b
ensure aut iframe is focused before dispatching key events in bidi br…
cacieprins c8b61e8
update tests for cdp focus
cacieprins 6600009
fixed tests for bidi
cacieprins 30bfc52
lint
cacieprins a12e15f
Merge branch 'develop' into cacie/feat/cy-press-command
cacieprins 2e84355
fix type ref in .d.ts
cacieprins a33bcef
linting
cacieprins a1f9a27
skip press() driver test in ff below v135
cacieprins 1ca097c
Merge branch 'develop' into cacie/feat/cy-press-command
jennifer-shehane 3637665
try all contexts for frame before failing due to missing/invalid cont…
cacieprins 7744213
ensure error is error before accessing props
cacieprins 2636de5
Merge branch 'develop' into cacie/feat/cy-press-command
cacieprins fe3a435
skip press driver test in webkit
cacieprins 8956584
changelog
cacieprins 39ffe62
debug automation middleware invocation for firefox flake
cacieprins 9d60c4d
debug
cacieprins 1b183e5
cache update
cacieprins dc42d30
use bidi automation middleware from connectToNewSpec rather than cons…
cacieprins 14d8822
more comprehensive logging
cacieprins a13c69d
debug socket base, additional debug in automation
cacieprins da743c3
install firefox automation middleware on setup as well as connectToNe…
cacieprins 98b12c7
Merge branch 'develop' into cacie/feat/cy-press-command
cacieprins 9af2e37
unit tests for firefox-utils
cacieprins a9ac681
proper calledWith
cacieprins e01d3df
Merge branch 'develop' into cacie/feat/cy-press-command
cacieprins c30265b
Merge branch 'develop' into cacie/feat/cy-press-command
jennifer-shehane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -578,99 +578,7 @@ declare namespace Cypress { | |
*/ | ||
stop(): void | ||
|
||
Commands: { | ||
/** | ||
* Add a custom command | ||
* @see https://on.cypress.io/api/commands | ||
*/ | ||
add<T extends keyof Chainable>(name: T, fn: CommandFn<T>): void | ||
|
||
/** | ||
* Add a custom parent command | ||
* @see https://on.cypress.io/api/commands#Parent-Commands | ||
*/ | ||
add<T extends keyof Chainable>(name: T, options: CommandOptions & { prevSubject: false }, fn: CommandFn<T>): void | ||
|
||
/** | ||
* Add a custom child command | ||
* @see https://on.cypress.io/api/commands#Child-Commands | ||
*/ | ||
add<T extends keyof Chainable, S = any>(name: T, options: CommandOptions & { prevSubject: true }, fn: CommandFnWithSubject<T, S>): void | ||
|
||
/** | ||
* Add a custom child or dual command | ||
* @see https://on.cypress.io/api/commands#Validations | ||
*/ | ||
add<T extends keyof Chainable, S extends PrevSubject>( | ||
name: T, options: CommandOptions & { prevSubject: S | ['optional'] }, fn: CommandFnWithSubject<T, PrevSubjectMap[S]>, | ||
): void | ||
|
||
/** | ||
* Add a custom command that allows multiple types as the prevSubject | ||
* @see https://on.cypress.io/api/commands#Validations#Allow-Multiple-Types | ||
*/ | ||
add<T extends keyof Chainable, S extends PrevSubject>( | ||
name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject<T, PrevSubjectMap<void>[S]>, | ||
): void | ||
|
||
/** | ||
* Add one or more custom commands | ||
* @see https://on.cypress.io/api/commands | ||
*/ | ||
addAll<T extends keyof Chainable>(fns: CommandFns): void | ||
|
||
/** | ||
* Add one or more custom parent commands | ||
* @see https://on.cypress.io/api/commands#Parent-Commands | ||
*/ | ||
addAll<T extends keyof Chainable>(options: CommandOptions & { prevSubject: false }, fns: CommandFns): void | ||
|
||
/** | ||
* Add one or more custom child commands | ||
* @see https://on.cypress.io/api/commands#Child-Commands | ||
*/ | ||
addAll<T extends keyof Chainable, S = any>(options: CommandOptions & { prevSubject: true }, fns: CommandFnsWithSubject<S>): void | ||
|
||
/** | ||
* Add one or more custom commands that validate their prevSubject | ||
* @see https://on.cypress.io/api/commands#Validations | ||
*/ | ||
addAll<T extends keyof Chainable, S extends PrevSubject>( | ||
options: CommandOptions & { prevSubject: S | ['optional'] }, fns: CommandFnsWithSubject<PrevSubjectMap[S]>, | ||
): void | ||
|
||
/** | ||
* Add one or more custom commands that allow multiple types as their prevSubject | ||
* @see https://on.cypress.io/api/commands#Allow-Multiple-Types | ||
*/ | ||
addAll<T extends keyof Chainable, S extends PrevSubject>( | ||
options: CommandOptions & { prevSubject: S[] }, fns: CommandFnsWithSubject<PrevSubjectMap<void>[S]>, | ||
): void | ||
|
||
/** | ||
* Overwrite an existing Cypress command with a new implementation | ||
* @see https://on.cypress.io/api/commands#Overwrite-Existing-Commands | ||
*/ | ||
overwrite<T extends keyof Chainable>(name: T, fn: CommandFnWithOriginalFn<T>): void | ||
|
||
/** | ||
* Overwrite an existing Cypress command with a new implementation | ||
* @see https://on.cypress.io/api/commands#Overwrite-Existing-Commands | ||
*/ | ||
overwrite<T extends keyof Chainable, S extends PrevSubject>(name: T, fn: CommandFnWithOriginalFnAndSubject<T, PrevSubjectMap[S]>): void | ||
|
||
/** | ||
* Add a custom query | ||
* @see https://on.cypress.io/api/custom-queries | ||
*/ | ||
addQuery<T extends keyof Chainable>(name: T, fn: QueryFn<T>): void | ||
|
||
/** | ||
* Overwrite an existing Cypress query with a new implementation | ||
* @see https://on.cypress.io/api/custom-queries | ||
*/ | ||
overwriteQuery<T extends keyof Chainable>(name: T, fn: QueryFnWithOriginalFn<T>): void | ||
} | ||
Commands: Commands | ||
|
||
/** | ||
* @see https://on.cypress.io/cookies | ||
|
@@ -829,6 +737,100 @@ declare namespace Cypress { | |
onSpecWindow: (window: Window, specList: string[] | Array<() => Promise<void>>) => void | ||
} | ||
|
||
interface Commands { | ||
/** | ||
* Add a custom command | ||
* @see https://on.cypress.io/api/commands | ||
*/ | ||
add<T extends keyof Chainable>(name: T, fn: CommandFn<T>): void | ||
|
||
/** | ||
* Add a custom parent command | ||
* @see https://on.cypress.io/api/commands#Parent-Commands | ||
*/ | ||
add<T extends keyof Chainable>(name: T, options: CommandOptions & { prevSubject: false }, fn: CommandFn<T>): void | ||
|
||
/** | ||
* Add a custom child command | ||
* @see https://on.cypress.io/api/commands#Child-Commands | ||
*/ | ||
add<T extends keyof Chainable, S = any>(name: T, options: CommandOptions & { prevSubject: true }, fn: CommandFnWithSubject<T, S>): void | ||
|
||
/** | ||
* Add a custom child or dual command | ||
* @see https://on.cypress.io/api/commands#Validations | ||
*/ | ||
add<T extends keyof Chainable, S extends PrevSubject>( | ||
name: T, options: CommandOptions & { prevSubject: S | ['optional'] }, fn: CommandFnWithSubject<T, PrevSubjectMap[S]>, | ||
): void | ||
|
||
/** | ||
* Add a custom command that allows multiple types as the prevSubject | ||
* @see https://on.cypress.io/api/commands#Validations#Allow-Multiple-Types | ||
*/ | ||
add<T extends keyof Chainable, S extends PrevSubject>( | ||
name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject<T, PrevSubjectMap<void>[S]>, | ||
): void | ||
|
||
/** | ||
* Add one or more custom commands | ||
* @see https://on.cypress.io/api/commands | ||
*/ | ||
addAll<T extends keyof Chainable>(fns: CommandFns): void | ||
|
||
/** | ||
* Add one or more custom parent commands | ||
* @see https://on.cypress.io/api/commands#Parent-Commands | ||
*/ | ||
addAll<T extends keyof Chainable>(options: CommandOptions & { prevSubject: false }, fns: CommandFns): void | ||
|
||
/** | ||
* Add one or more custom child commands | ||
* @see https://on.cypress.io/api/commands#Child-Commands | ||
*/ | ||
addAll<T extends keyof Chainable, S = any>(options: CommandOptions & { prevSubject: true }, fns: CommandFnsWithSubject<S>): void | ||
|
||
/** | ||
* Add one or more custom commands that validate their prevSubject | ||
* @see https://on.cypress.io/api/commands#Validations | ||
*/ | ||
addAll<T extends keyof Chainable, S extends PrevSubject>( | ||
options: CommandOptions & { prevSubject: S | ['optional'] }, fns: CommandFnsWithSubject<PrevSubjectMap[S]>, | ||
): void | ||
|
||
/** | ||
* Add one or more custom commands that allow multiple types as their prevSubject | ||
* @see https://on.cypress.io/api/commands#Allow-Multiple-Types | ||
*/ | ||
addAll<T extends keyof Chainable, S extends PrevSubject>( | ||
options: CommandOptions & { prevSubject: S[] }, fns: CommandFnsWithSubject<PrevSubjectMap<void>[S]>, | ||
): void | ||
|
||
/** | ||
* Overwrite an existing Cypress command with a new implementation | ||
* @see https://on.cypress.io/api/commands#Overwrite-Existing-Commands | ||
*/ | ||
overwrite<T extends keyof Chainable>(name: T, fn: CommandFnWithOriginalFn<T>): void | ||
|
||
/** | ||
* Overwrite an existing Cypress command with a new implementation | ||
* @see https://on.cypress.io/api/commands#Overwrite-Existing-Commands | ||
*/ | ||
overwrite<T extends keyof Chainable, S extends PrevSubject>(name: T, fn: CommandFnWithOriginalFnAndSubject<T, PrevSubjectMap[S]>): void | ||
|
||
/** | ||
* Add a custom query | ||
* @see https://on.cypress.io/api/custom-queries | ||
*/ | ||
addQuery<T extends keyof Chainable>(name: T, fn: QueryFn<T>): void | ||
|
||
/** | ||
* Overwrite an existing Cypress query with a new implementation | ||
* @see https://on.cypress.io/api/custom-queries | ||
*/ | ||
overwriteQuery<T extends keyof Chainable>(name: T, fn: QueryFnWithOriginalFn<T>): void | ||
} | ||
|
||
type CanReturnChainable = void | Chainable | Promise<unknown> | ||
type ThenReturn<S, R> = | ||
R extends void ? Chainable<S> : | ||
|
@@ -1742,6 +1744,16 @@ declare namespace Cypress { | |
*/ | ||
pause(options?: Partial<Loggable>): Chainable<Subject> | ||
|
||
/** | ||
* Send a native sequence of keyboard events: keydown & press, followed by keyup, for the provided key. | ||
* Supported keys index the Cypress.Keyboard.Keys record. | ||
* | ||
* @example | ||
* cy.press(Cypress.Keyboard.Keys.TAB) // dispatches a keydown and press event to the browser, followed by a keyup event. | ||
* @see https://on.cypress.io/press | ||
*/ | ||
press(key: 'Tab' /* todo */, options?: Partial<Loggable & Timeoutable>): void | ||
|
||
|
||
/** | ||
* Get the immediately preceding sibling of each element in a set of the elements. | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,11 @@ | ||
describe('__placeholder__/commands/actions/press', () => { | ||
describe('src/cy/commands/actions/press', () => { | ||
it('dispatches the tab keypress to the AUT', () => { | ||
cy.visit('/fixtures/input_events.html') | ||
cy.get('#focus').focus() | ||
cy.press('Tab') | ||
|
||
cy.get('#focus').focus().then(async () => { | ||
try { | ||
await Cypress.automation('key:press', { key: 'Tab' }) | ||
} catch (e) { | ||
if (e.message && (e.message as string).includes('key:press')) { | ||
cy.log(e.message) | ||
cy.get('#keyup').should('have.value', 'Tab') | ||
|
||
return | ||
} | ||
|
||
throw e | ||
} | ||
|
||
cy.get('#keyup').should('have.value', 'Tab') | ||
|
||
cy.get('#keydown').should('have.value', 'Tab') | ||
}) | ||
cy.get('#keydown').should('have.value', 'Tab') | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { $Cy } from '../../../cypress/cy' | ||
import type { StateFunc } from '../../../cypress/state' | ||
import type { KeyPressSupportedKeys, AutomationCommands } from '@packages/types' | ||
import { defaults } from 'lodash' | ||
import { isSupportedKey } from '@packages/server/lib/automation/commands/key_press' | ||
import $errUtils from '../../../cypress/error_utils' | ||
|
||
export interface PressCommand { | ||
(key: KeyPressSupportedKeys, userOptions?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable>): void | ||
} | ||
|
||
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>) { | ||
const options: Cypress.Loggable & Partial<Cypress.Timeoutable> = defaults({}, userOptions, { | ||
log: true, | ||
}) | ||
|
||
const log = Cypress.log({ | ||
timeout: options.timeout, | ||
hidden: options.log === false, | ||
message: options, | ||
consoleProps () { | ||
return { | ||
'Key': key, | ||
} | ||
}, | ||
}) | ||
|
||
if (!isSupportedKey(key)) { | ||
$errUtils.throwErrByPath('press.invalid_key', { | ||
onFail: log, | ||
args: { key }, | ||
}) | ||
|
||
// throwErrByPath always throws, but there's no way to indicate that | ||
// code beyond this point is unreachable to typescript / linters | ||
return | ||
} | ||
|
||
if (Cypress.browser.family === 'webkit') { | ||
$errUtils.throwErrByPath('press.unsupported_browser', { | ||
onFail: log, | ||
args: { | ||
family: Cypress.browser.family, | ||
}, | ||
}) | ||
|
||
return | ||
} | ||
|
||
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'] = { | ||
key, | ||
} | ||
|
||
await Cypress.automation('key:press', args) | ||
} catch (err) { | ||
$errUtils.throwErr(err, { onFail: log }) | ||
} | ||
} | ||
|
||
return Commands.add('press', pressCommand) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.