Skip to content

Commit b5c0a55

Browse files
committed
use cdp scroll
1 parent b78fa71 commit b5c0a55

File tree

4 files changed

+195
-109
lines changed

4 files changed

+195
-109
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,32 @@ Moves the mouse to the specified destination point.
144144
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `0`. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
145145
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.
146146

147-
#### `scrollIntoView(selector: string | ElementHandle, options?: ScrollOptions) => Promise<void>`
147+
#### `scrollIntoView(selector: string | ElementHandle, options?: ScrollIntoViewOptions) => Promise<void>`
148148

149149
Scrolls the element into view. If already in view, no scroll occurs.
150150

151151
- **selector:** CSS selector or ElementHandle to identify the target element.
152-
- **options (optional):** Additional options for scrolling. **Extends the `options` of the `getElement` function (below)**
152+
- **options (optional):** Additional options for scrolling. **Extends the `options` of the `getElement` and `scroll` functions (below)**
153153
- `scrollSpeed (number):` Scroll speed (when scrolling occurs). 0 to 100. 100 is instant. Default is `100`.
154154
- `scrollDelay (number):` Time to wait after scrolling (when scrolling occurs). Default is `200`.
155155
- `inViewportMargin (number):` Margin (in px) to add around the element when ensuring it is in the viewport. Default is `0`.
156156

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

159159
Scrolls to the specified destination point.
160160

161161
- **destination:** An object with `x` and `y` coordinates representing the target position. For example, `{ x: 500, y: 300 }`. Can also be `"top"` or `"bottom"`.
162+
- **options (optional):** Additional options for scrolling. **Extends the `options` of the `scroll` function (below)**
163+
164+
#### `scroll: (delta: Partial<Vector>, options?: ScrollOptions) => Promise<void>`
165+
166+
Scrolls the page the distance set by `delta`.
167+
168+
- **delta:** An object with `x` and `y` coordinates representing the distance to scroll from the current position.
162169
- **options (optional):** Additional options for scrolling.
163-
- `behavior (ScrollBehavior):` Directly passed to `window.scrollTo`.
164-
- `scrollDelay (number):` Time to wait after scrolling. Default is `200`.
170+
- `scrollSpeed (number):` Scroll speed (when scrolling occurs). 0 to 100. 100 is instant. Default is `100`.
171+
- `scrollDelay (number):` Time to wait after scrolling (when scrolling occurs). Default is `200`.
172+
165173
#### `getElement(selector: string | ElementHandle, options?: GetElementOptions) => Promise<void>`
166174

167175
Gets the element via a selector. Can use an XPath.

src/__debug__/browser-debug.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const cursorDefaultOptions = {
1616
waitForClick: 10,
1717
scrollDelay: 100,
1818
scrollSpeed: 40,
19-
inViewportMargin: 50
19+
inViewportMargin: 50,
20+
waitForSelector: 200
2021
} as const satisfies ClickOptions
2122

2223
puppeteer.launch({ headless: false }).then(async (browser) => {
@@ -26,8 +27,10 @@ puppeteer.launch({ headless: false }).then(async (browser) => {
2627

2728
const cursor = createCursor(page, undefined, undefined, {
2829
move: cursorDefaultOptions,
30+
moveTo: cursorDefaultOptions,
2931
click: cursorDefaultOptions,
30-
moveTo: cursorDefaultOptions
32+
scroll: cursorDefaultOptions,
33+
getElement: cursorDefaultOptions
3134
})
3235

3336
const html = await fs.readFile(join(__dirname, 'custom-page.html'), 'utf8')
@@ -44,6 +47,14 @@ puppeteer.launch({ headless: false }).then(async (browser) => {
4447
await cursor.click('#box3')
4548

4649
await cursor.click('#box1')
50+
51+
// await cursor.scrollTo('right')
52+
53+
// await cursor.scrollTo('left')
54+
55+
// await cursor.scrollTo('bottom')
56+
57+
// await cursor.scrollTo('top')
4758
}
4859

4960
await performActions()

src/__test__/spoof.spec.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ describe('Mouse movements', () => {
4848
expect(await page.evaluate(() => window.boxWasClicked)).toEqual(true)
4949
}
5050

51+
const getScrollPosition = async (): Promise<{ top: number, left: number }> => await page.evaluate(() => (
52+
{ top: window.scrollY, left: window.scrollX }
53+
))
54+
5155
it('Should click on the element without throwing an error (CSS selector)', async () => {
5256
await testClick('#box1')
5357
})
@@ -57,10 +61,6 @@ describe('Mouse movements', () => {
5761
})
5862

5963
it('Should scroll to elements correctly', async () => {
60-
const getScrollPosition = async (): Promise<{ top: number, left: number }> => await page.evaluate(() => (
61-
{ top: window.scrollY, left: window.scrollX }
62-
))
63-
6464
const boxes = await Promise.all([1, 2, 3].map(async (number: number): Promise<ElementHandle<HTMLElement>> => {
6565
const selector = `#box${number}`
6666
const box = await page.waitForSelector(selector) as ElementHandle<HTMLElement> | null
@@ -89,6 +89,25 @@ describe('Mouse movements', () => {
8989
await cursor.click(boxes[0])
9090
expect(await boxes[0].isIntersectingViewport()).toBeTruthy()
9191
})
92+
93+
it('Should scroll to position correctly', async () => {
94+
expect(await getScrollPosition()).toEqual({ top: 0, left: 0 })
95+
96+
await cursor.scrollTo('bottom')
97+
expect(await getScrollPosition()).toEqual({ top: 4450, left: 0 })
98+
99+
await cursor.scrollTo('right')
100+
expect(await getScrollPosition()).toEqual({ top: 4450, left: 2250 })
101+
102+
await cursor.scrollTo('top')
103+
expect(await getScrollPosition()).toEqual({ top: 0, left: 2250 })
104+
105+
await cursor.scrollTo('left')
106+
expect(await getScrollPosition()).toEqual({ top: 0, left: 0 })
107+
108+
await cursor.scrollTo({ y: 200, x: 400 })
109+
expect(await getScrollPosition()).toEqual({ top: 200, left: 400 })
110+
})
92111
})
93112

94113
jest.setTimeout(15_000)

0 commit comments

Comments
 (0)