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

add support to click specified coordinates relavte to an element #426

Merged
merged 2 commits into from
Jul 8, 2018
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
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)`](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
9 changes: 6 additions & 3 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)`](#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 @@ -107,17 +107,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 Expand Up @@ -173,7 +176,7 @@ __Arguments__
__Example__

```js
await chromeless.wait(() => {
await chromeless.wait(() => {
return new Promise((resolve, reject) => {
// do something async, setTimeout...
resolve();
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 @@ -74,7 +74,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 @@ -180,7 +180,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 @@ -199,8 +199,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 @@ -86,6 +86,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 @@ -154,13 +154,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