Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Commit 34ee149

Browse files
authored
Merge pull request #151 from criticalbh/feature/clear
feat: clear API
2 parents 6923e72 + 1c43e52 commit 34ee149

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Added
1111
- `placeholder()` API method [#000](https://github.com/graphcool/chromeless/pull/000) @contributor
12+
- `clearInput()` API method
1213

1314
### Changed
1415

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ const chromeless = new Chromeless({
169169
- [`cookiesSet(cookies: Cookie[])`](docs/api.md#api-cookiesset-many)
170170
- [`cookiesClear(name: string)`](docs/api.md#api-cookiesclear)
171171
- [`cookiesClearAll()`](docs/api.md#api-cookiesclearall)
172+
- [`clearInput(selector: string)`](docs/api.md#api-clearInput)
172173

173174
## Configuring Development Environment
174175

docs/api.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,17 @@ __Example__
609609
```js
610610
await chromeless.cookiesClearAll()
611611
```
612+
---------------------------------------
613+
614+
<a name="api-clearInput" />
615+
616+
### clearInput(selector: string): Chromeless<T>
617+
618+
Clear input text.
619+
620+
621+
__Example__
622+
623+
```js
624+
await chromeless.clearInput('#username')
625+
```

src/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ export default class Chromeless<T extends any> implements Promise<T> {
276276
return this
277277
}
278278

279+
clearInput(selector: string): Chromeless<T> {
280+
this.queue.enqueue({type: 'clearInput', selector})
281+
return this
282+
}
283+
279284
async end(): Promise<T> {
280285
const result = await this.lastReturnPromise
281286
await this.queue.end()

src/chrome/local-runtime.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
mousedown,
3333
mouseup,
3434
focus,
35+
clearInput,
3536
} from '../util'
3637

3738
export default class LocalRuntime {
@@ -97,6 +98,8 @@ export default class LocalRuntime {
9798
return this.mousup(command.selector)
9899
case 'focus':
99100
return this.focus(command.selector)
101+
case 'clearInput':
102+
return this.clearInput(command.selector)
100103
default:
101104
throw new Error(`No such command: ${JSON.stringify(command)}`)
102105
}
@@ -362,6 +365,22 @@ export default class LocalRuntime {
362365
}
363366
}
364367

368+
async clearInput(selector: string): Promise<void> {
369+
if (selector) {
370+
if (this.chromelessOptions.implicitWait) {
371+
this.log(`clearInput(): Waiting for ${selector}`)
372+
await waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)
373+
}
374+
375+
const exists = await nodeExists(this.client, selector)
376+
if (!exists) {
377+
throw new Error(`clearInput(): Node not found for selector: ${selector}`)
378+
}
379+
}
380+
await clearInput(this.client, selector)
381+
this.log(`${selector} cleared`)
382+
}
383+
365384
private log(msg: string): void {
366385
if (this.chromelessOptions.debug) {
367386
console.log(msg)

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export type Command =
154154
type: 'focus'
155155
selector: string
156156
}
157+
| {
158+
type: 'clearInput'
159+
selector: string
160+
}
157161

158162
export interface Cookie {
159163
url?: string

src/util.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,44 @@ export async function pdf(
422422
return pdf.data
423423
}
424424

425+
export async function clearInput(client: Client, selector: string): Promise<void> {
426+
await wait(500)
427+
await focus(client, selector)
428+
429+
const {Input} = client
430+
431+
const text = await getValue(client, selector)
432+
433+
const optionsDelete = {
434+
nativeVirtualKeyCode: 46,
435+
windowsVirtualKeyCode: 46,
436+
}
437+
438+
const optionsBackspace = {
439+
nativeVirtualKeyCode: 8,
440+
windowsVirtualKeyCode: 8,
441+
}
442+
443+
for (let i = 0; i < text.length; i++) {
444+
await Input.dispatchKeyEvent({
445+
...optionsDelete,
446+
type: 'rawKeyDown',
447+
})
448+
Input.dispatchKeyEvent({
449+
...optionsDelete,
450+
type: 'keyUp',
451+
})
452+
await Input.dispatchKeyEvent({
453+
...optionsBackspace,
454+
type: 'rawKeyDown',
455+
})
456+
Input.dispatchKeyEvent({
457+
...optionsBackspace,
458+
type: 'keyUp',
459+
})
460+
}
461+
}
462+
425463
export function getDebugOption(): boolean {
426464
if (
427465
process &&

0 commit comments

Comments
 (0)