@web/test-runner-commands
Advanced tools
Comparing version 0.4.5 to 0.5.0
# @web/test-runner-commands | ||
## 0.5.0 | ||
### Minor Changes | ||
- c3ead4fa: Added support for holding and releasing keys to the sendKeys command. The command now supports two additional actions `down` and `up` which hold down, or release a key. This effectively allows to hold down modifier keys while pressing other keys, which allows key combinations such as `Shift+Tab`. | ||
@example | ||
```ts | ||
await sendKeys({ | ||
down: 'Shift', | ||
}); | ||
await sendKeys({ | ||
press: 'Tab', | ||
}); | ||
await sendKeys({ | ||
up: 'Shift', | ||
}); | ||
``` | ||
## 0.4.5 | ||
@@ -4,0 +24,0 @@ |
import { TestRunnerPlugin } from '@web/test-runner-core'; | ||
export declare type SendKeysPayload = { | ||
declare type TypePayload = { | ||
type: string; | ||
press?: undefined; | ||
} | { | ||
type?: undefined; | ||
}; | ||
declare type PressPayload = { | ||
press: string; | ||
}; | ||
declare type DownPayload = { | ||
down: string; | ||
}; | ||
declare type UpPayload = { | ||
up: string; | ||
}; | ||
export declare type SendKeysPayload = TypePayload | PressPayload | DownPayload | UpPayload; | ||
export declare function sendKeysPlugin(): TestRunnerPlugin<SendKeysPayload>; | ||
export {}; | ||
//# sourceMappingURL=sendKeysPlugin.d.ts.map |
@@ -8,14 +8,28 @@ "use strict"; | ||
function isSendKeysPayload(payload) { | ||
const validOptions = ['type', 'press', 'down', 'up']; | ||
if (!isObject(payload)) | ||
throw new Error('You must provide a `SendKeysPayload` object'); | ||
if (!!payload.type && !!payload.press) | ||
throw new Error('You must provide ONLY one of the `type` or `press` properties for pass to the browser runner.'); | ||
if (!payload.type && !payload.press) | ||
throw new Error('You mist provide one of the `type` or `press` properties for pass to the browser runner.'); | ||
const payloadKeys = Object.keys(payload).filter(key => key !== 'press' && key !== 'type'); | ||
if (payloadKeys.length) { | ||
throw new Error('Unknown options `' + payloadKeys.join(', ') + '` present.'); | ||
const numberOfValidOptions = Object.keys(payload).filter(key => validOptions.includes(key)).length; | ||
const unknownOptions = Object.keys(payload).filter(key => !validOptions.includes(key)); | ||
if (numberOfValidOptions > 1) | ||
throw new Error(`You must provide ONLY one of the following properties to pass to the browser runner: ${validOptions.join(', ')}.`); | ||
if (numberOfValidOptions === 0) | ||
throw new Error(`You must provide one of the following properties to pass to the browser runner: ${validOptions.join(', ')}.`); | ||
if (unknownOptions.length > 0) { | ||
throw new Error('Unknown options `' + unknownOptions.join(', ') + '` present.'); | ||
} | ||
return true; | ||
} | ||
function isTypePayload(payload) { | ||
return 'type' in payload; | ||
} | ||
function isPressPayload(payload) { | ||
return 'press' in payload; | ||
} | ||
function isDownPayload(payload) { | ||
return 'down' in payload; | ||
} | ||
function isUpPayload(payload) { | ||
return 'up' in payload; | ||
} | ||
function sendKeysPlugin() { | ||
@@ -32,10 +46,18 @@ return { | ||
const page = session.browser.getPage(session.id); | ||
if (payload.type) { | ||
if (isTypePayload(payload)) { | ||
await page.keyboard.type(payload.type); | ||
return true; | ||
} | ||
else if (payload.press) { | ||
else if (isPressPayload(payload)) { | ||
await page.keyboard.press(payload.press); | ||
return true; | ||
} | ||
else if (isDownPayload(payload)) { | ||
await page.keyboard.down(payload.down); | ||
return true; | ||
} | ||
else if (isUpPayload(payload)) { | ||
await page.keyboard.up(payload.up); | ||
return true; | ||
} | ||
} | ||
@@ -45,10 +67,18 @@ // handle specific behavior for puppeteer | ||
const page = session.browser.getPage(session.id); | ||
if (payload.type) { | ||
if (isTypePayload(payload)) { | ||
await page.keyboard.type(payload.type); | ||
return true; | ||
} | ||
else if (payload.press) { | ||
else if (isPressPayload(payload)) { | ||
await page.keyboard.press(payload.press); | ||
return true; | ||
} | ||
else if (isDownPayload(payload)) { | ||
await page.keyboard.down(payload.down); | ||
return true; | ||
} | ||
else if (isUpPayload(payload)) { | ||
await page.keyboard.up(payload.up); | ||
return true; | ||
} | ||
} | ||
@@ -55,0 +85,0 @@ // you might not be able to support all browser launchers |
{ | ||
"name": "@web/test-runner-commands", | ||
"version": "0.4.5", | ||
"version": "0.5.0", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
@@ -5,6 +5,9 @@ import { TestRunnerPlugin } from '@web/test-runner-core'; | ||
export type SendKeysPayload = | ||
| { type: string; press?: undefined } | ||
| { type?: undefined; press: string }; | ||
type TypePayload = { type: string }; | ||
type PressPayload = { press: string }; | ||
type DownPayload = { down: string }; | ||
type UpPayload = { up: string }; | ||
export type SendKeysPayload = TypePayload | PressPayload | DownPayload | UpPayload; | ||
function isObject(payload: unknown): payload is Record<string, unknown> { | ||
@@ -15,14 +18,25 @@ return payload != null && typeof payload === 'object'; | ||
function isSendKeysPayload(payload: unknown): boolean { | ||
const validOptions = ['type', 'press', 'down', 'up']; | ||
if (!isObject(payload)) throw new Error('You must provide a `SendKeysPayload` object'); | ||
if (!!payload.type && !!payload.press) | ||
const numberOfValidOptions = Object.keys(payload).filter(key => | ||
validOptions.includes(key), | ||
).length; | ||
const unknownOptions = Object.keys(payload).filter(key => !validOptions.includes(key)); | ||
if (numberOfValidOptions > 1) | ||
throw new Error( | ||
'You must provide ONLY one of the `type` or `press` properties for pass to the browser runner.', | ||
`You must provide ONLY one of the following properties to pass to the browser runner: ${validOptions.join( | ||
', ', | ||
)}.`, | ||
); | ||
if (!payload.type && !payload.press) | ||
if (numberOfValidOptions === 0) | ||
throw new Error( | ||
'You mist provide one of the `type` or `press` properties for pass to the browser runner.', | ||
`You must provide one of the following properties to pass to the browser runner: ${validOptions.join( | ||
', ', | ||
)}.`, | ||
); | ||
const payloadKeys = Object.keys(payload).filter(key => key !== 'press' && key !== 'type'); | ||
if (payloadKeys.length) { | ||
throw new Error('Unknown options `' + payloadKeys.join(', ') + '` present.'); | ||
if (unknownOptions.length > 0) { | ||
throw new Error('Unknown options `' + unknownOptions.join(', ') + '` present.'); | ||
} | ||
@@ -32,2 +46,18 @@ return true; | ||
function isTypePayload(payload: SendKeysPayload): payload is TypePayload { | ||
return 'type' in payload; | ||
} | ||
function isPressPayload(payload: SendKeysPayload): payload is PressPayload { | ||
return 'press' in payload; | ||
} | ||
function isDownPayload(payload: SendKeysPayload): payload is DownPayload { | ||
return 'down' in payload; | ||
} | ||
function isUpPayload(payload: SendKeysPayload): payload is UpPayload { | ||
return 'up' in payload; | ||
} | ||
export function sendKeysPlugin(): TestRunnerPlugin<SendKeysPayload> { | ||
@@ -44,8 +74,14 @@ return { | ||
const page = (session.browser as PlaywrightLauncher).getPage(session.id); | ||
if (payload.type) { | ||
if (isTypePayload(payload)) { | ||
await page.keyboard.type(payload.type); | ||
return true; | ||
} else if (payload.press) { | ||
} else if (isPressPayload(payload)) { | ||
await page.keyboard.press(payload.press); | ||
return true; | ||
} else if (isDownPayload(payload)) { | ||
await page.keyboard.down(payload.down); | ||
return true; | ||
} else if (isUpPayload(payload)) { | ||
await page.keyboard.up(payload.up); | ||
return true; | ||
} | ||
@@ -57,8 +93,14 @@ } | ||
const page = (session.browser as ChromeLauncher).getPage(session.id); | ||
if (payload.type) { | ||
if (isTypePayload(payload)) { | ||
await page.keyboard.type(payload.type); | ||
return true; | ||
} else if (payload.press) { | ||
} else if (isPressPayload(payload)) { | ||
await page.keyboard.press(payload.press as puppeteerCore.KeyInput); | ||
return true; | ||
} else if (isDownPayload(payload)) { | ||
await page.keyboard.down(payload.down as puppeteerCore.KeyInput); | ||
return true; | ||
} else if (isUpPayload(payload)) { | ||
await page.keyboard.up(payload.up as puppeteerCore.KeyInput); | ||
return true; | ||
} | ||
@@ -65,0 +107,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
62895
1027