Skip to content

Commit be0b637

Browse files
committed
feat: add mousedown and mouseup events
1 parent 1fcdc80 commit be0b637

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

Diff for: examples/mouse-event-example.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { Chromeless } = require('chromeless')
2+
3+
async function run() {
4+
const chromeless = new Chromeless()
5+
6+
const screenshot = await chromeless
7+
.goto('https://www.google.com')
8+
.mousedown('input[name="btnI"]')
9+
.mouseup('input[name="btnI"]')
10+
.wait('.latest-doodle')
11+
.screenshot()
12+
13+
console.log(screenshot)
14+
15+
await chromeless.end()
16+
}
17+
18+
run().catch(console.error.bind(console))

Diff for: src/api.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,14 @@ export default class Chromeless<T extends any> implements Promise<T> {
124124
throw new Error('Not implemented yet')
125125
}
126126

127-
mousedown(): Chromeless<T> {
128-
throw new Error('Not implemented yet')
127+
mousedown(selector: string): Chromeless<T> {
128+
this.queue.enqueue({type: 'mousedown', selector})
129+
return this
129130
}
130131

131-
mouseup(): Chromeless<T> {
132-
throw new Error('Not implemented yet')
132+
mouseup(selector: string): Chromeless<T> {
133+
this.queue.enqueue({type: 'mouseup', selector})
134+
return this
133135
}
134136

135137
mouseover(): Chromeless<T> {

Diff for: src/chrome/local-runtime.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
press,
1616
clearCookies,
1717
getCookies,
18-
setCookies, getAllCookies, version,
18+
setCookies, getAllCookies, version, mousedown, mouseup
1919
} from '../util'
2020

2121
export default class LocalRuntime {
@@ -65,6 +65,10 @@ export default class LocalRuntime {
6565
return this.cookiesGetAll()
6666
case 'cookiesSet':
6767
return this.cookiesSet(command.nameOrCookies, command.value)
68+
case 'mousedown':
69+
return this.mousedown(command.selector)
70+
case 'mouseup':
71+
return this.mousup(command.selector)
6872
default:
6973
throw new Error(`No such command: ${JSON.stringify(command)}`)
7074
}
@@ -114,6 +118,38 @@ export default class LocalRuntime {
114118
return scrollTo(this.client, x, y)
115119
}
116120

121+
private async mousedown(selector: string): Promise<void> {
122+
if (this.chromlessOptions.implicitWait) {
123+
this.log(`mousedown(): Waiting for ${selector}`)
124+
await waitForNode(this.client, selector, this.chromlessOptions.waitTimeout)
125+
}
126+
127+
const exists = await nodeExists(this.client, selector)
128+
if (!exists) {
129+
throw new Error(`mousedown(): node for selector ${selector} doesn't exist`)
130+
}
131+
132+
const {scale} = this.chromlessOptions.viewport
133+
await mousedown(this.client, selector, scale)
134+
this.log(`Mousedown on ${selector}`)
135+
}
136+
137+
private async mousup(selector: string): Promise<void> {
138+
if (this.chromlessOptions.implicitWait) {
139+
this.log(`mouseup(): Waiting for ${selector}`)
140+
await waitForNode(this.client, selector, this.chromlessOptions.waitTimeout)
141+
}
142+
143+
const exists = await nodeExists(this.client, selector)
144+
if (!exists) {
145+
throw new Error(`mouseup(): node for selector ${selector} doesn't exist`)
146+
}
147+
148+
const {scale} = this.chromlessOptions.viewport
149+
await mouseup(this.client, selector, scale)
150+
this.log(`Mouseup on ${selector}`)
151+
}
152+
117153
async type(text: string, selector?: string): Promise<void> {
118154
if (selector) {
119155
if (this.chromlessOptions.implicitWait) {

Diff for: src/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ export type Command =
107107
type: 'cookiesGet'
108108
nameOrQuery?: string | CookieQuery
109109
}
110+
| {
111+
type: 'mousedown',
112+
selector: string
113+
}
114+
| {
115+
type: 'mouseup',
116+
selector: string
117+
}
110118

111119
export interface Cookie {
112120
url?: string

Diff for: src/util.ts

+34
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,40 @@ export async function setCookies(client: Client, cookies: Cookie[]): Promise<voi
271271
}
272272
}
273273

274+
export async function mousedown(client: Client, selector: string, scale: number) {
275+
const clientRect = await getClientRect(client, selector)
276+
const {Input} = client
277+
278+
const options = {
279+
x: Math.round((clientRect.left + clientRect.width / 2) * scale),
280+
y: Math.round((clientRect.top + clientRect.height / 2) * scale),
281+
button: 'left',
282+
clickCount: 1,
283+
}
284+
285+
await Input.dispatchMouseEvent({
286+
...options,
287+
type: 'mousePressed'
288+
})
289+
}
290+
291+
export async function mouseup(client: Client, selector: string, scale: number) {
292+
const clientRect = await getClientRect(client, selector)
293+
const {Input} = client
294+
295+
const options = {
296+
x: Math.round((clientRect.left + clientRect.width / 2) * scale),
297+
y: Math.round((clientRect.top + clientRect.height / 2) * scale),
298+
button: 'left',
299+
clickCount: 1,
300+
}
301+
302+
await Input.dispatchMouseEvent({
303+
...options,
304+
type: 'mouseReleased'
305+
})
306+
}
307+
274308
function getUrlFromCookie(cookie: Cookie) {
275309
const domain = cookie.domain.slice(1, cookie.domain.length)
276310
return `https://${domain}`

0 commit comments

Comments
 (0)