@@ -78,6 +78,53 @@ describe('engine-adapter', () => {
7878 const count = await adapter . count ( 'button' ) ;
7979 assert . strictEqual ( count , 5 ) ;
8080 } ) ;
81+
82+ describe ( 'evaluateOnPage' , ( ) => {
83+ it ( 'should handle zero arguments' , async ( ) => {
84+ page = createMockPlaywrightPage ( ) ;
85+ adapter = new PlaywrightAdapter ( page ) ;
86+ const result = await adapter . evaluateOnPage ( ( ) => 42 ) ;
87+ assert . strictEqual ( result , 42 ) ;
88+ } ) ;
89+
90+ it ( 'should handle single argument' , async ( ) => {
91+ page = createMockPlaywrightPage ( ) ;
92+ adapter = new PlaywrightAdapter ( page ) ;
93+ const result = await adapter . evaluateOnPage ( ( x ) => x * 2 , [ 5 ] ) ;
94+ assert . strictEqual ( result , 10 ) ;
95+ } ) ;
96+
97+ it ( 'should handle multiple arguments by spreading them' , async ( ) => {
98+ page = createMockPlaywrightPage ( ) ;
99+ adapter = new PlaywrightAdapter ( page ) ;
100+ // This is the bug case - multiple args should be spread, not passed as array
101+ const result = await adapter . evaluateOnPage ( ( a , b ) => a + b , [ 3 , 7 ] ) ;
102+ assert . strictEqual ( result , 10 ) ;
103+ } ) ;
104+
105+ it ( 'should handle selector + array arguments (real-world bug case)' , async ( ) => {
106+ page = createMockPlaywrightPage ( ) ;
107+ adapter = new PlaywrightAdapter ( page ) ;
108+ // This reproduces the original bug: selector and processedIds
109+ const result = await adapter . evaluateOnPage (
110+ ( selector , processedIds ) =>
111+ `Selector: ${ selector } , Count: ${ processedIds . length } ` ,
112+ [ '[data-qa="test"]' , [ 'id1' , 'id2' ] ]
113+ ) ;
114+ assert . ok ( result . includes ( 'Selector: [data-qa="test"]' ) ) ;
115+ assert . ok ( result . includes ( 'Count: 2' ) ) ;
116+ } ) ;
117+
118+ it ( 'should handle multiple arguments of different types' , async ( ) => {
119+ page = createMockPlaywrightPage ( ) ;
120+ adapter = new PlaywrightAdapter ( page ) ;
121+ const result = await adapter . evaluateOnPage (
122+ ( str , num , obj ) => `${ str } -${ num } -${ obj . key } ` ,
123+ [ 'hello' , 42 , { key : 'world' } ]
124+ ) ;
125+ assert . strictEqual ( result , 'hello-42-world' ) ;
126+ } ) ;
127+ } ) ;
81128 } ) ;
82129
83130 describe ( 'PuppeteerAdapter' , ( ) => {
@@ -130,6 +177,42 @@ describe('engine-adapter', () => {
130177 const count = await adapter . count ( 'button' ) ;
131178 assert . strictEqual ( count , 5 ) ;
132179 } ) ;
180+
181+ describe ( 'evaluateOnPage' , ( ) => {
182+ it ( 'should handle zero arguments' , async ( ) => {
183+ page = createMockPuppeteerPage ( ) ;
184+ adapter = new PuppeteerAdapter ( page ) ;
185+ const result = await adapter . evaluateOnPage ( ( ) => 42 ) ;
186+ assert . strictEqual ( result , 42 ) ;
187+ } ) ;
188+
189+ it ( 'should handle single argument' , async ( ) => {
190+ page = createMockPuppeteerPage ( ) ;
191+ adapter = new PuppeteerAdapter ( page ) ;
192+ const result = await adapter . evaluateOnPage ( ( x ) => x * 2 , [ 5 ] ) ;
193+ assert . strictEqual ( result , 10 ) ;
194+ } ) ;
195+
196+ it ( 'should handle multiple arguments by spreading them' , async ( ) => {
197+ page = createMockPuppeteerPage ( ) ;
198+ adapter = new PuppeteerAdapter ( page ) ;
199+ // Puppeteer natively spreads args - ensure same behavior as Playwright fix
200+ const result = await adapter . evaluateOnPage ( ( a , b ) => a + b , [ 3 , 7 ] ) ;
201+ assert . strictEqual ( result , 10 ) ;
202+ } ) ;
203+
204+ it ( 'should handle selector + array arguments (parity with PlaywrightAdapter)' , async ( ) => {
205+ page = createMockPuppeteerPage ( ) ;
206+ adapter = new PuppeteerAdapter ( page ) ;
207+ const result = await adapter . evaluateOnPage (
208+ ( selector , processedIds ) =>
209+ `Selector: ${ selector } , Count: ${ processedIds . length } ` ,
210+ [ '[data-qa="test"]' , [ 'id1' , 'id2' ] ]
211+ ) ;
212+ assert . ok ( result . includes ( 'Selector: [data-qa="test"]' ) ) ;
213+ assert . ok ( result . includes ( 'Count: 2' ) ) ;
214+ } ) ;
215+ } ) ;
133216 } ) ;
134217
135218 describe ( 'createEngineAdapter' , ( ) => {
0 commit comments