From b65fd03585efe05b3f7aee40e5f9f46d1a3496fd Mon Sep 17 00:00:00 2001 From: Domas Date: Thu, 22 Mar 2018 23:42:02 +0200 Subject: [PATCH] Implement mouseover; refs @ronanyeah for code --- src/api.ts | 9 +++++---- src/chrome/local-runtime.ts | 26 ++++++++++++++++++++++++++ src/types.ts | 4 ++++ src/util.ts | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/api.ts b/src/api.ts index 840a977e..79853be9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -165,13 +165,14 @@ export default class Chromeless implements Promise { return this } - mouseup(selector: string): Chromeless { - this.queue.enqueue({ type: 'mouseup', selector }) + mouseover(selector: string): Chromeless { + this.queue.enqueue({ type: 'mouseover', selector }) return this } - mouseover(): Chromeless { - throw new Error('Not implemented yet') + mouseup(selector: string): Chromeless { + this.queue.enqueue({ type: 'mouseup', selector }) + return this } scrollTo(x: number, y: number): Chromeless { diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index af1f7392..b9808510 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -33,6 +33,7 @@ import { getAllCookies, version, mousedown, + mouseover, mouseup, focus, clearInput, @@ -113,6 +114,8 @@ export default class LocalRuntime { return this.setCookies(command.nameOrCookies, command.value) case 'mousedown': return this.mousedown(command.selector) + case 'mouseover': + return this.mouseover(command.selector) case 'mouseup': return this.mouseup(command.selector) case 'focus': @@ -245,6 +248,29 @@ export default class LocalRuntime { this.log(`Mousedown on ${selector}`) } + private async mouseover(selector: string): Promise { + if (this.chromelessOptions.implicitWait) { + this.log(`mouseover(): Waiting for ${selector}`) + await waitForNode( + this.client, + selector, + this.chromelessOptions.waitTimeout, + ) + } + + const exists = await nodeExists(this.client, selector) + if (!exists) { + throw new Error( + `mouseover(): node for selector ${selector} doesn't exist`, + ) + } + + const { scale } = this.chromelessOptions.viewport + await mouseover(this.client, selector, scale) + this.log(`Mouseover on ${selector}`) + } + + private async mouseup(selector: string): Promise { if (this.chromelessOptions.implicitWait) { this.log(`mouseup(): Waiting for ${selector}`) diff --git a/src/types.ts b/src/types.ts index 5f5c7f97..b7d070d0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -176,6 +176,10 @@ export type Command = type: 'mousedown' selector: string } + | { + type: 'mouseover' + selector: string + } | { type: 'mouseup' selector: string diff --git a/src/util.ts b/src/util.ts index f46f9eb0..9395f060 100644 --- a/src/util.ts +++ b/src/util.ts @@ -400,6 +400,25 @@ export async function mousedown( }) } +export async function mouseover( + client: Client, + selector: string, + scale: number, +) { + const clientRect = await getClientRect(client, selector) + const { Input } = client + + const options = { + x: Math.round((clientRect.left + clientRect.width / 2) * scale), + y: Math.round((clientRect.top + clientRect.height / 2) * scale), + } + + await Input.dispatchMouseEvent({ + ...options, + type: 'mouseMoved', + }) +} + export async function mouseup(client: Client, selector: string, scale: number) { const clientRect = await getClientRect(client, selector) const { Input } = client