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

Commit

Permalink
add support to click specified coordinates relavte to an element (#426)
Browse files Browse the repository at this point in the history
* add support to click  specified coordinates relavte to an element

* code prettier
  • Loading branch information
skiloop authored and adieuadieu committed Jul 8, 2018
1 parent f0756d0 commit 9fa70c6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const chromeless = new Chromeless({
**Chrome methods**
- [`goto(url: string, timeout?: number)`](docs/api.md#api-goto)
- [`setUserAgent(useragent: string)`](docs/api.md#api-setUserAgent)
- [`click(selector: string)`](docs/api.md#api-click)
- [`click(selector: string, x?: number, y?: number)`](docs/api.md#api-click)
- [`wait(timeout: number)`](docs/api.md#api-wait-timeout)
- [`wait(selector: string)`](docs/api.md#api-wait-selector)
- [`wait(fn: (...args: any[]) => boolean, ...args: any[])`] - Not implemented yet
Expand Down
7 changes: 5 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Chromeless provides TypeScript typings.
### Chrome methods
- [`goto(url: string, timeout?: number)`](#api-goto)
- [`setUserAgent(useragent: string)`](#api-setuseragent)
- [`click(selector: string)`](#api-click)
- [`click(selector: string, x?: number, y?: number)`](#api-click)
- [`wait(timeout: number)`](#api-wait-timeout)
- [`wait(selector: string, timeout?: number)`](#api-wait-selector)
- [`wait(fn: (...args: any[]) => boolean, ...args: any[])`] - Not implemented yet
Expand Down Expand Up @@ -108,17 +108,20 @@ await chromeless.setUserAgent('Custom Chromeless UserAgent x.x.x')

<a name="api-click" />

### click(selector: string): Chromeless<T>
### click(selector: string, x?: number, y?: number): Chromeless<T>

Click on something in the DOM.

__Arguments__
- `selector` - DOM selector for element to click
- `x` - Offset from the left of the element, default width/2
- `y` - Offset from the top of the element, default height/2

__Example__

```js
await chromeless.click('#button')
await chromeless.click('#button', 20, 100)
```

---------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

click(selector: string): Chromeless<T> {
this.queue.enqueue({ type: 'click', selector })
click(selector: string, x?: number, y?: number): Chromeless<T> {
this.queue.enqueue({ type: 'click', selector, x, y })

return this
}
Expand Down
8 changes: 4 additions & 4 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class LocalRuntime {
case 'setUserAgent':
return this.setUserAgent(command.useragent)
case 'click':
return this.click(command.selector)
return this.click(command.selector, command.x, command.y)
case 'returnCode':
return this.returnCode(command.fn, ...command.args)
case 'returnExists':
Expand Down Expand Up @@ -187,7 +187,7 @@ export default class LocalRuntime {
this.log(`Waited for ${selector}`)
}

private async click(selector: string): Promise<void> {
private async click(selector: string, x?: number, y?: number): Promise<void> {
if (this.chromelessOptions.implicitWait) {
this.log(`click(): Waiting for ${selector}`)
await waitForNode(
Expand All @@ -206,8 +206,8 @@ export default class LocalRuntime {
if (this.chromelessOptions.scrollBeforeClick) {
await scrollToElement(this.client, selector)
}
await click(this.client, selector, scale)
this.log(`Clicked on ${selector}`)
await click(this.client, selector, scale, x, y)
this.log(`Clicked on ${selector} at (${x}, ${y})`)
}

private async returnCode<T>(fn: string, ...args: any[]): Promise<T> {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export type Command =
| {
type: 'click'
selector: string
x?: number
y?: number
}
| {
type: 'returnCode'
Expand Down
15 changes: 11 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,20 @@ export async function getClientRect(client, selector): Promise<ClientRect> {
return JSON.parse(result.result.value) as ClientRect
}

export async function click(client: Client, selector: string, scale: number) {
export async function click(
client: Client,
selector: string,
scale: number,
x?: number,
y?: number,
) {
const clientRect = await getClientRect(client, selector)
const { Input } = client

if (x === undefined) x = clientRect.width / 2
if (y === undefined) y = clientRect.height / 2
const options = {
x: Math.round((clientRect.left + clientRect.width / 2) * scale),
y: Math.round((clientRect.top + clientRect.height / 2) * scale),
x: Math.round((clientRect.left + x) * scale),
y: Math.round((clientRect.top + y) * scale),
button: 'left',
clickCount: 1,
}
Expand Down

0 comments on commit 9fa70c6

Please sign in to comment.