Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
➕ elementHandle: add press() (close #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Sep 8, 2018
1 parent 210591f commit 7a8a1e2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ elementHandle.focus(): Promise<void>
elementHandle.hover(): Promise<void>
```

#### `press`

```ts
elementHandle.press(key: string): Promise<void>
```

Where `key` is of the [possible keys](./src/keys.ts) or a single character.

#### `screenshot`

```ts
Expand Down
25 changes: 24 additions & 1 deletion src/api/ElementHandle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Marionette from '../Marionette'
import { pWriteFile, MOUSE_BUTTON } from '../utils'
import Page from './Page'
import {
TJSHandleId,
TClickOptions,
TMouseButton
} from './types'
import JSHandle from './JSHandle'
import { pWriteFile, MOUSE_BUTTON, hasKey } from '../utils'
import KEYS from '../keys'

class ElementHandle extends JSHandle {
private _page: Page
Expand Down Expand Up @@ -106,6 +107,28 @@ class ElementHandle extends JSHandle {
this._actionId = id
}

async press (key: string): Promise<void> {
if (hasKey(KEYS, key)) {
await this._send('WebDriver:ElementSendKeys', {
id: this._handleId.ELEMENT,
text: KEYS[key]
})

return
}

if (key.length === 1) {
await this._send('WebDriver:ElementSendKeys', {
id: this._handleId.ELEMENT,
text: key
})

return
}

throw new Error(`Unknown key: ${key}`)
}

async screenshot (options: { path?: string } = {}): Promise<Buffer> {
const result = await this._send('WebDriver:TakeScreenshot', {
id: this._handleId.ELEMENT,
Expand Down
69 changes: 69 additions & 0 deletions src/keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export default {
Null: '\ue000',
Cancel: '\ue001',
Abort: '\ue001',
Help: '\ue002',
Backspace: '\ue003',
Tab: '\ue004',
Clear: '\ue005',
Return: '\ue006',
Enter: '\ue007',
NumpadEnter: '\ue007',
Shift: '\ue008',
ShiftLeft: '\ue008',
ShiftRight: '\ue008',
ControlLeft: '\ue009',
ControlRight: '\ue009',
Alt: '\ue00a',
AltLeft: '\ue00a',
AltRight: '\ue00a',
Pause: '\ue00b',
Escape: '\ue00c',
Space: '\ue00d',
PageUp: '\ue00e',
PageDown: '\ue00f',
End: '\ue010',
Home: '\ue011',
ArrowLeft: '\ue012',
ArrowUp: '\ue013',
ArrowRight: '\ue014',
ArrowDown: '\ue015',
Insert: '\ue016',
Delete: '\ue017',
Semicolon: '\ue018',
Equal: '\ue019',
NumpadEqual: '\ue019',
Numpad0: '\ue01a',
Numpad1: '\ue01b',
Numpad2: '\ue01c',
Numpad3: '\ue01d',
Numpad4: '\ue01e',
Numpad5: '\ue01f',
Numpad6: '\ue020',
Numpad7: '\ue021',
Numpad8: '\ue022',
Numpad9: '\ue023',
NumpadMultiply: '\ue024',
NumpadAdd: '\ue025',
// TODO: ???
// SEPARATOR: '\ue026',
NumpadSubstract: '\ue027',
NumpadDecimal: '\ue028',
NumpadDivide: '\ue029',
F1: '\ue031',
F2: '\ue032',
F3: '\ue033',
F4: '\ue034',
F5: '\ue035',
F6: '\ue036',
F7: '\ue037',
F8: '\ue038',
F9: '\ue039',
F10: '\ue03a',
F11: '\ue03b',
F12: '\ue03c',
Meta: '\ue03d',
MetaLeft: '\ue03d',
// MetaRight has different code
Command: '\ue03d'
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export const mapEvaluateArgs = (args: TEvaluateArg[]) => args.map((arg) => {

return arg
})

// ESLint fails to parse this written as arrow function
function hasKey <T> (obj: T, key: any): key is keyof T {
return key in obj
}

export { hasKey }
42 changes: 42 additions & 0 deletions test/api/ElementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,48 @@ test('ElementHandle `hover()`', testWithFirefox(async (t) => {
)
}))

test('ElementHandle `press()`', testWithFirefox(async (t) => {
const browser = await foxr.connect()
const page = await browser.newPage()

await page.setContent('<input type="text" value="hhellloo"/>')

const target = await page.$('input')

if (target === null) {
t.fail('There should be element')
return
}

await target.press('Backspace')
await target.press('ArrowLeft')
await target.press('Backspace')
await target.press('ArrowLeft')
await target.press('ArrowLeft')
await target.press('Backspace')
await target.press('ArrowLeft')
await target.press('Delete')
await target.press('e')

t.equal(
await page.evaluate('document.querySelector("input").value'),
'hello',
'should press keys'
)

try {
// @ts-ignore
await target.press('Pepe')
t.fail()
} catch (err) {
t.equal(
err.message,
'Unknown key: Pepe',
'should throw if there is no such a key'
)
}
}))

test('ElementHandle `screenshot()`', testWithFirefox(async (t) => {
const writeFileSpy = createSpy(({ args }) => args[args.length - 1](null))

Expand Down

0 comments on commit 7a8a1e2

Please sign in to comment.