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

feat: selectFile and selectFiles API #170

Merged
merged 4 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `placeholder()` API method [#000](https://github.com/graphcool/chromeless/pull/000) @contributor
- `clearInput()` API method [#151](https://github.com/graphcool/chromeless/pull/151), [#133](https://github.com/graphcool/chromeless/issues/133) @criticalbh
- `setViewport()` API method [#115](https://github.com/graphcool/chromeless/pull/115) @joeyvandijk
- `selectFiles(selector: string, files: string[]) and selectFile(selector: string, file: string)` API methods @criticalbh

### Changed
- **Breaking:** We renamed `cookiesClear()` to `deleteCookies()`, `cookiesClearAll()` to `clearCookies()` and according to semver should bump the version to 2.0.0, however, just-this-time, we're not going to. [#123](https://github.com/graphcool/chromeless/pull/123) @joeyvandijk
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ const chromeless = new Chromeless({
- [`deleteCookies(name: string)`](docs/api.md#api-deletecookies)
- [`clearCookies()`](docs/api.md#api-clearcookies)
- [`clearInput(selector: string)`](docs/api.md#api-clearInput)
- [`selectFiles(selector: string, files: string[])`](docs/api.md#api-selectFiles)
- [`selectFile(selector: string, file: string)`](docs/api.md#api-selectFile)

## Configuring Development Environment

Expand Down
28 changes: 28 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,31 @@ __Example__
```js
await chromeless.clearInput('#username')
```
---------------------------------------

<a name="api-selectFile" />

### selectFile(selector: string, file: string): Chromeless<T>

Select file for selected file input.


__Example__

```js
await chromeless.selectFile('.uploader', '/User/Me/Documents/img.jpg')
```
---------------------------------------

<a name="api-selectFiles" />

### selectFile(selector: string, files: string[]): Chromeless<T>

Select files for selected file input.


__Example__

```js
await chromeless.selectFiles('.multi-uploader', ['/User/Me/Documents/img.jpg', '/User/Me/Documents/img2.jpg'])
```
11 changes: 10 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,20 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

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

selectFile(selector: string, file: string): Chromeless<T> {
return this.selectFiles(selector, [file])
}

selectFiles(selector: string, files: string[]): Chromeless<T> {
this.queue.enqueue({type: 'selectFiles', selector, files})
return this
}

async end(): Promise<T> {
const result = await this.lastReturnPromise
await this.queue.end()
Expand Down
18 changes: 18 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
mouseup,
focus,
clearInput,
selectFiles,
} from '../util'

export default class LocalRuntime {
Expand Down Expand Up @@ -103,6 +104,8 @@ export default class LocalRuntime {
return this.focus(command.selector)
case 'clearInput':
return this.clearInput(command.selector)
case 'selectFiles':
return this.selectFiles(command.selector, command.files)
default:
throw new Error(`No such command: ${JSON.stringify(command)}`)
}
Expand Down Expand Up @@ -401,6 +404,21 @@ export default class LocalRuntime {
this.log(`${selector} cleared`)
}

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

const exists = await nodeExists(this.client, selector)
if (!exists) {
throw new Error(`selectFiles(): node for selector ${selector} doesn't exist`)
}

await selectFiles(this.client, selector, files)
this.log(`Selected files ${files}`)
}

private log(msg: string): void {
if (this.chromelessOptions.debug) {
console.log(msg)
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ export type Command =
type: 'clearInput'
selector: string
}
| {
type: 'selectFiles',
selector: string,
files: string[]
}

export interface Cookie {
url?: string
Expand Down
7 changes: 7 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,13 @@ export async function clearInput(client: Client, selector: string): Promise<void
}
}

export async function selectFiles(client: Client, selector: string, files: string[]): Promise<string> {
const {DOM} = client
const dom = await DOM.getDocument()
const node = await DOM.querySelector({nodeId: dom.root.nodeId, selector: selector})
return await DOM.setFileInputFiles({files: files, nodeId: node.nodeId})
}

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