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 all 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 @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- `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
- `setFileInput(selector: string, files: string | string[]) API method [#170](https://github.com/graphcool/chromeless/pull/170/) @criticalbh
- `clearCache()` API method [#122](https://github.com/graphcool/chromeless/pull/122) @joeyvandijk
- `scrollToElement()` command and `scrollBeforeClick` constructor option [#15](https://github.com/graphcool/chromeless/issues/15), [#167](https://github.com/graphcool/chromeless/pull/167) @janza
- Mocha E2E tests [example](examples/mocha-chai-test-example.js) [#164](https://github.com/graphcool/chromeless/pull/164) @FabioAntunes
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ 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)
- [`setFileInput(selector: string, files: string | string[])`](docs/api.md#api-set-file-input)

## 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 @@ -666,3 +666,17 @@ __Example__
```js
await chromeless.clearInput('#username')
```
---------------------------------------

<a name="api-set-file-input" />

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

Set file(s) for selected file input.


__Example__

```js
await chromeless.setFileInput('.uploader', '/User/Me/Documents/img.jpg')
```
13 changes: 12 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ChromeRemote from './chrome/remote'
import Queue from './queue'
import { ChromelessOptions, Cookie, CookieQuery, PdfOptions, DeviceMetrics } from './types'
import { getDebugOption } from './util'
import { isArray } from 'util'

export default class Chromeless<T extends any> implements Promise<T> {
private queue: Queue
Expand Down Expand Up @@ -297,11 +298,21 @@ 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
}

setFileInput(selector: string, files: string): Chromeless<T>
setFileInput(selector: string, files: string[]): Chromeless<T>
setFileInput(selector: string, files: string | string[]): Chromeless<T> {
if (!isArray(files)) {
files = [files]
}
this.queue.enqueue({type: 'setFileInput', 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 @@ -37,6 +37,7 @@ import {
mouseup,
focus,
clearInput,
setFileInput,
} from '../util'

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

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

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

await setFileInput(this.client, selector, files)
this.log(`setFileInput() 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 @@ -171,6 +171,11 @@ export type Command =
type: 'clearInput'
selector: string
}
| {
type: 'setFileInput',
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 @@ -479,6 +479,13 @@ export async function clearInput(client: Client, selector: string): Promise<void
}
}

export async function setFileInput(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