@@ -35,10 +35,14 @@ function makeHistory(steps: Partial<RecordedStep>[]): SessionHistory {
3535}
3636
3737describe ( 'generateCode - header' , ( ) => {
38- it ( 'wraps output in remote() setup' , ( ) => {
38+ it ( 'wraps output in remote() setup with try/finally and deleteSession ' , ( ) => {
3939 const code = generateCode ( makeHistory ( [ ] ) ) ;
4040 expect ( code ) . toContain ( "import { remote } from 'webdriverio';" ) ;
4141 expect ( code ) . toContain ( 'browserName' ) ;
42+ expect ( code ) . toContain ( 'try {' ) ;
43+ expect ( code ) . toContain ( '} finally {' ) ;
44+ expect ( code ) . toContain ( ' await browser.deleteSession();' ) ;
45+ expect ( code ) . toContain ( '}' ) ;
4246 } ) ;
4347
4448 it ( 'generates start_session (browser) using history.capabilities' , ( ) => {
@@ -177,6 +181,31 @@ describe('generateCode - tool mappings', () => {
177181 expect ( code ) . toContain ( "await browser.execute('mobile: swipe', { direction: 'up' });" ) ;
178182 } ) ;
179183
184+ it ( 'execute_script → browser.execute with single-quoted script string' , ( ) => {
185+ const code = generateCode ( makeHistory ( [ {
186+ tool : 'execute_script' ,
187+ params : { script : 'return document.title' } ,
188+ } ] ) ) ;
189+ expect ( code ) . toContain ( "await browser.execute('return document.title');" ) ;
190+ } ) ;
191+
192+ it ( 'execute_script escapes backslashes in script string' , ( ) => {
193+ const code = generateCode ( makeHistory ( [ {
194+ tool : 'execute_script' ,
195+ params : { script : "return document.querySelector('[data-\\\\x]')" } ,
196+ } ] ) ) ;
197+ expect ( code ) . toContain ( "await browser.execute('return document.querySelector(\\'[data-\\\\\\\\x]\\')');" ) ;
198+ } ) ;
199+
200+ it ( 'execute_script with args → browser.execute with args array' , ( ) => {
201+ const code = generateCode ( makeHistory ( [ {
202+ tool : 'execute_script' ,
203+ params : { script : 'arguments[0].click()' , args : [ '#btn' ] } ,
204+ } ] ) ) ;
205+ expect ( code ) . toContain ( "await browser.execute('arguments[0].click()'," ) ;
206+ expect ( code ) . toContain ( '"#btn"' ) ;
207+ } ) ;
208+
180209 it ( 'drag_and_drop (selector form) → $().dragAndDrop($())' , ( ) => {
181210 const code = generateCode ( makeHistory ( [ {
182211 tool : 'drag_and_drop' ,
0 commit comments