1
- import type { Page } from 'puppeteer'
1
+ import type { ElementHandle , Page } from 'puppeteer'
2
2
import { type ClickOptions , createCursor , GhostCursor } from '../spoof'
3
3
import { join } from 'path'
4
- import { promises as fs } from 'fs'
4
+ import { readFileSync } from 'fs'
5
5
import installMouseHelper from '../mouse-helper'
6
6
7
7
declare const page : Page
@@ -18,63 +18,76 @@ const cursorDefaultOptions = {
18
18
inViewportMargin : 50
19
19
} as const satisfies ClickOptions
20
20
21
+ declare global {
22
+ // eslint-disable-next-line no-var
23
+ var boxWasClicked : boolean
24
+ }
25
+
21
26
describe ( 'Mouse movements' , ( ) => {
27
+ const html = readFileSync ( join ( __dirname , 'custom-page.html' ) , 'utf8' )
28
+
22
29
beforeAll ( async ( ) => {
23
30
await installMouseHelper ( page )
24
- const html = await fs . readFile ( join ( __dirname , 'custom-page.html' ) , 'utf8' )
31
+ } )
32
+
33
+ beforeEach ( async ( ) => {
25
34
await page . goto ( 'data:text/html,' + encodeURIComponent ( html ) , {
26
35
waitUntil : 'networkidle2'
27
36
} )
28
- } )
29
37
30
- beforeEach ( ( ) => {
31
38
cursor = createCursor ( page , undefined , undefined , {
32
39
move : cursorDefaultOptions ,
33
40
click : cursorDefaultOptions ,
34
41
moveTo : cursorDefaultOptions
35
42
} )
36
43
} )
37
44
45
+ const testClick = async ( clickSelector : string ) : Promise < void > => {
46
+ expect ( await page . evaluate ( ( ) => window . boxWasClicked ) ) . toEqual ( false )
47
+ await cursor . click ( clickSelector )
48
+ expect ( await page . evaluate ( ( ) => window . boxWasClicked ) ) . toEqual ( true )
49
+ }
50
+
38
51
it ( 'Should click on the element without throwing an error (CSS selector)' , async ( ) => {
39
- await cursor . click ( '#box1' )
52
+ await testClick ( '#box1' )
40
53
} )
41
54
42
55
it ( 'Should click on the element without throwing an error (XPath selector)' , async ( ) => {
43
- await cursor . click ( '//*[@id="box1"]' )
56
+ await testClick ( '//*[@id="box1"]' )
44
57
} )
45
58
46
59
it ( 'Should scroll to elements correctly' , async ( ) => {
47
60
const getScrollPosition = async ( ) : Promise < { top : number , left : number } > => await page . evaluate ( ( ) => (
48
61
{ top : window . scrollY , left : window . scrollX }
49
62
) )
50
63
51
- const box1 = await page . waitForSelector ( '#box1' )
52
- if ( box1 == null ) throw new Error ( ' box not found' )
53
- const box2 = await page . waitForSelector ( '#box2' )
54
- if ( box2 == null ) throw new Error ( 'box not found' )
55
- const box3 = await page . waitForSelector ( '#box3' )
56
- if ( box3 == null ) throw new Error ( 'box not found' )
64
+ const boxes = await Promise . all ( [ 1 , 2 , 3 ] . map ( async ( number : number ) : Promise < ElementHandle < HTMLElement > > => {
65
+ const selector = `# box${ number } `
66
+ const box = await page . waitForSelector ( selector ) as ElementHandle < HTMLElement > | null
67
+ if ( box == null ) throw new Error ( ` ${ selector } not found` )
68
+ return box
69
+ } ) )
57
70
58
71
expect ( await getScrollPosition ( ) ) . toEqual ( { top : 0 , left : 0 } )
59
72
60
- expect ( await box1 . isIntersectingViewport ( ) ) . toBeTruthy ( )
61
- await cursor . click ( box1 )
73
+ expect ( await boxes [ 0 ] . isIntersectingViewport ( ) ) . toBeTruthy ( )
74
+ await cursor . click ( boxes [ 0 ] )
62
75
expect ( await getScrollPosition ( ) ) . toEqual ( { top : 0 , left : 0 } )
63
- expect ( await box1 . isIntersectingViewport ( ) ) . toBeTruthy ( )
76
+ expect ( await boxes [ 0 ] . isIntersectingViewport ( ) ) . toBeTruthy ( )
64
77
65
- expect ( await box2 . isIntersectingViewport ( ) ) . toBeFalsy ( )
66
- await cursor . move ( box2 )
78
+ expect ( await boxes [ 1 ] . isIntersectingViewport ( ) ) . toBeFalsy ( )
79
+ await cursor . move ( boxes [ 1 ] )
67
80
expect ( await getScrollPosition ( ) ) . toEqual ( { top : 2500 , left : 0 } )
68
- expect ( await box2 . isIntersectingViewport ( ) ) . toBeTruthy ( )
81
+ expect ( await boxes [ 1 ] . isIntersectingViewport ( ) ) . toBeTruthy ( )
69
82
70
- expect ( await box3 . isIntersectingViewport ( ) ) . toBeFalsy ( )
71
- await cursor . move ( box3 )
83
+ expect ( await boxes [ 2 ] . isIntersectingViewport ( ) ) . toBeFalsy ( )
84
+ await cursor . move ( boxes [ 2 ] )
72
85
expect ( await getScrollPosition ( ) ) . toEqual ( { top : 4450 , left : 2250 } )
73
- expect ( await box3 . isIntersectingViewport ( ) ) . toBeTruthy ( )
86
+ expect ( await boxes [ 2 ] . isIntersectingViewport ( ) ) . toBeTruthy ( )
74
87
75
- expect ( await box1 . isIntersectingViewport ( ) ) . toBeFalsy ( )
76
- await cursor . click ( box1 )
77
- expect ( await box1 . isIntersectingViewport ( ) ) . toBeTruthy ( )
88
+ expect ( await boxes [ 0 ] . isIntersectingViewport ( ) ) . toBeFalsy ( )
89
+ await cursor . click ( boxes [ 0 ] )
90
+ expect ( await boxes [ 0 ] . isIntersectingViewport ( ) ) . toBeTruthy ( )
78
91
} )
79
92
} )
80
93
0 commit comments