Skip to content
Open
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ Scrolls the element into view. If already in view, no scroll occurs.
- `scrollDelay (number):` Time to wait after scrolling (when scrolling occurs). Default is `200`.
- `inViewportMargin (number):` Margin (in px) to add around the element when ensuring it is in the viewport. Default is `0`.

#### `scrollTo: (destination: Partial<Vector> | 'top' | 'bottom' | 'left' | 'right', options?: ScrollOptions) => Promise<void>`
#### `scrollTo: (destination: Partial<Vector> | 'top' | 'bottom' | 'left' | 'right' | ElementHandle, options?: ScrollOptions) => Promise<void>`

Scrolls to the specified destination point.

- **destination:** An object with `x` and `y` coordinates representing the target position. For example, `{ x: 500, y: 300 }`. Can also be `"top"` or `"bottom"`.
- **destination:** Can be any of:
- An object with `x` and `y` coordinates representing the target position. For example, `{ x: 500, y: 300 }`.
- `'top'` or `'bottom'` or `'left'` or `'right'`.
- An ElementHandle.
- **options (optional):** Additional options for scrolling. **Extends the `options` of the `scroll` function (below)**

#### `scroll: (delta: Partial<Vector>, options?: ScrollOptions) => Promise<void>`
Expand Down
20 changes: 15 additions & 5 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ export interface MoveToOptions extends PathOptions, Pick<MoveOptions, 'moveDelay
readonly moveDelay?: number
}

export type ScrollToDestination = Partial<Vector> | 'top' | 'bottom' | 'left' | 'right'

export type MouseButtonOptions = Pick<ClickOptions, 'button' | 'clickCount'>
Comment on lines 161 to 162

/**
Expand Down Expand Up @@ -913,7 +911,7 @@ export class GhostCursor {

/** Scrolls to the specified destination point. */
public async scrollTo (
destination: ScrollToDestination,
destination: Partial<Vector> | 'top' | 'bottom' | 'left' | 'right' | ElementHandle,
/** @default defaultOptions.scroll */
options?: ScrollOptions
): Promise<void> {
Expand All @@ -938,7 +936,7 @@ export class GhostCursor {
}
))

const to = ((): Partial<Vector> => {
const to: Partial<Vector> = await (async (): Promise<Partial<Vector>> => {
switch (destination) {
case 'top':
return { y: 0 }
Expand All @@ -948,8 +946,20 @@ export class GhostCursor {
return { x: 0 }
case 'right':
return { x: docWidth }
default:
default: {
const isElementHandle =
typeof destination === 'object' && 'evaluate' in destination

if (isElementHandle) {
const box = await destination.boundingBox()
if (box == null) {
throw new Error('no boundingBox')
}
return box
}

return destination
}
Comment on lines +949 to +962
}
})()

Expand Down
Loading