-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FI-1498 feat:
pressKey
can receive selector
- Loading branch information
Showing
2 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,20 +1,73 @@ | ||
import {LogEventType} from '../constants/internal'; | ||
import {getPlaywrightPage} from '../useContext'; | ||
import {E2edError} from '../utils/error'; | ||
import {log} from '../utils/log'; | ||
import {getDescriptionFromSelector} from '../utils/selectors'; | ||
|
||
import type {Keyboard} from '@playwright/test'; | ||
import type {KeyboardPressKey, Selector} from '../types/internal'; | ||
|
||
import type {KeyboardPressKey} from '../types/internal'; | ||
type Options = Readonly<{delay?: number; timeout?: number}>; | ||
|
||
type Options = Parameters<Keyboard['press']>[1]; | ||
type PressKey = (( | ||
this: void, | ||
selector: Selector, | ||
key: KeyboardPressKey, | ||
options?: Options, | ||
) => Promise<void>) & | ||
((this: void, key: KeyboardPressKey, options?: Options) => Promise<void>); | ||
|
||
/** | ||
* Presses the specified keyboard keys. | ||
*/ | ||
export const pressKey = async (key: KeyboardPressKey, options: Options = {}): Promise<void> => { | ||
log(`Press keyboard key: "${key}"`, options, LogEventType.InternalAction); | ||
export const pressKey: PressKey = async ( | ||
keyOrSelector: KeyboardPressKey | Selector, | ||
keyOrOptions?: KeyboardPressKey | Options, | ||
maybeOptions?: Options, | ||
): Promise<void> => { | ||
let key: KeyboardPressKey; | ||
let selector: Selector | undefined; | ||
let options: Options; | ||
|
||
if (typeof keyOrSelector === 'string') { | ||
key = keyOrSelector; | ||
|
||
if (typeof keyOrOptions === 'string') { | ||
throw new E2edError('keyOrOptions is string', { | ||
keyOrOptions, | ||
keyOrSelector, | ||
maybeOptions, | ||
}); | ||
} | ||
|
||
options = keyOrOptions ?? {}; | ||
} else { | ||
selector = keyOrSelector; | ||
|
||
if (typeof keyOrOptions !== 'string') { | ||
throw new E2edError('keyOrOptions is not string', { | ||
keyOrOptions, | ||
keyOrSelector, | ||
maybeOptions, | ||
}); | ||
} | ||
|
||
key = keyOrOptions; | ||
|
||
options = maybeOptions ?? {}; | ||
} | ||
|
||
const withDescription = | ||
selector !== undefined | ||
? ` on element with description ${getDescriptionFromSelector(selector)}` | ||
: ''; | ||
|
||
log(`Press keyboard key${withDescription}: "${key}"`, options, LogEventType.InternalAction); | ||
|
||
const page = getPlaywrightPage(); | ||
|
||
await page.keyboard.press(key, options); | ||
if (selector !== undefined) { | ||
await selector.getPlaywrightLocator().press(key, options); | ||
} else { | ||
await page.keyboard.press(key, options); | ||
} | ||
}; |
This file contains 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