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

feat: clearInput API #151

Merged
merged 3 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- `placeholder()` API method [#000](https://github.com/graphcool/chromeless/pull/000) @contributor
- `clear()` API method
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favour of calling this clearInput to be more explicit.


### Changed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const chromeless = new Chromeless({
- [`cookiesSet(cookies: Cookie[])`](docs/api.md#api-cookiesset-many)
- [`cookiesClear(name: string)`](docs/api.md#api-cookiesclear)
- [`cookiesClearAll()`](docs/api.md#api-cookiesclearall)
- [`clear(selector: string)`](docs/api.md#api-clear)

## Configuring Development Environment

Expand Down
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,17 @@ __Example__
```js
await chromeless.cookiesClearAll()
```
---------------------------------------

<a name="api-clear" />

### clear(selector: string): Chromeless<T>

Clear input text.


__Example__

```js
await chromeless.clear('#username')
```
5 changes: 5 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

clear(selector: string): Chromeless<T> {
this.queue.enqueue({type: 'clear', selector})
return this
}

async end(): Promise<T> {
const result = await this.lastReturnPromise
await this.queue.end()
Expand Down
19 changes: 19 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
mousedown,
mouseup,
focus,
clear
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing comma

} from '../util'

export default class LocalRuntime {
Expand Down Expand Up @@ -94,6 +95,8 @@ export default class LocalRuntime {
return this.mousup(command.selector)
case 'focus':
return this.focus(command.selector)
case 'clear':
return this.clear(command.selector)
default:
throw new Error(`No such command: ${JSON.stringify(command)}`)
}
Expand Down Expand Up @@ -359,6 +362,22 @@ export default class LocalRuntime {
}
}

async clear(selector: string): Promise<void> {
if (selector) {
if (this.chromelessOptions.implicitWait) {
this.log(`clear(): Waiting for ${selector}`)
await waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)
}

const exists = await nodeExists(this.client, selector)
if (!exists) {
throw new Error(`clear(): Node not found for selector: ${selector}`)
}
}
await clear(this.client, selector)
this.log(`${selector} cleared`)
}

private log(msg: string): void {
if (this.chromelessOptions.debug) {
console.log(msg)
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export type Command =
type: 'focus'
selector: string
}
| {
type: 'clear',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comma in type defs

selector: string
}

export interface Cookie {
url?: string
Expand Down
38 changes: 38 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,44 @@ export async function pdf(
return pdf.data
}

export async function clear(client: Client, selector: string): Promise<void> {
await wait(500)
await focus(client, selector)

const {Input} = client

const text = await getValue(client, selector)

const optionsDelete = {
nativeVirtualKeyCode: 46,
windowsVirtualKeyCode: 46,
}

const optionsBackspace = {
nativeVirtualKeyCode: 8,
windowsVirtualKeyCode: 8,
}

for (let i = 0; i < text.length; i++) {
await Input.dispatchKeyEvent({
...optionsDelete,
type: 'rawKeyDown',
})
Input.dispatchKeyEvent({
...optionsDelete,
type: 'keyUp',
})
await Input.dispatchKeyEvent({
...optionsBackspace,
type: 'rawKeyDown',
})
Input.dispatchKeyEvent({
...optionsBackspace,
type: 'keyUp',
})
}
}

export function getDebugOption(): boolean {
if (
process &&
Expand Down