@@ -12,6 +12,13 @@ function formatParams(params: Record<string, unknown>): string {
1212 . join ( ' ' ) ;
1313}
1414
15+ function indentJson ( value : unknown ) : string {
16+ return JSON . stringify ( value , null , 2 )
17+ . split ( '\n' )
18+ . map ( ( line , i ) => ( i > 0 ? ` ${ line } ` : line ) )
19+ . join ( '\n' ) ;
20+ }
21+
1522function generateStep ( step : RecordedStep ) : string {
1623 if ( step . tool === '__session_transition__' ) {
1724 const newId = ( step . params . newSessionId as string ) ?? 'unknown' ;
@@ -24,6 +31,50 @@ function generateStep(step: RecordedStep): string {
2431
2532 const p = step . params ;
2633 switch ( step . tool ) {
34+ case 'start_browser' : {
35+ const browserName = p . browser === 'edge' ? 'msedge' : String ( p . browser ?? 'chrome' ) ;
36+ const headless = p . headless !== false ;
37+ const width = ( p . windowWidth as number | undefined ) ?? 1920 ;
38+ const height = ( p . windowHeight as number | undefined ) ?? 1080 ;
39+ const args : string [ ] = [ `--window-size=${ width } ,${ height } ` ] ;
40+ if ( headless && browserName !== 'safari' ) {
41+ args . push ( '--headless=new' , '--disable-gpu' , '--disable-dev-shm-usage' ) ;
42+ }
43+ const caps : Record < string , unknown > = { browserName } ;
44+ if ( browserName === 'chrome' ) caps [ 'goog:chromeOptions' ] = { args } ;
45+ else if ( browserName === 'msedge' ) caps [ 'ms:edgeOptions' ] = { args } ;
46+ else if ( browserName === 'firefox' && headless ) caps [ 'moz:firefoxOptions' ] = { args : [ '-headless' ] } ;
47+ const extra = p . capabilities as Record < string , unknown > | undefined ;
48+ const merged = extra ? { ...caps , ...extra } : caps ;
49+ const nav = p . navigationUrl ? `\nawait browser.url('${ escapeStr ( p . navigationUrl ) } ');` : '' ;
50+ return `const browser = await remote({\n capabilities: ${ indentJson ( merged ) } \n});${ nav } ` ;
51+ }
52+ case 'start_app_session' : {
53+ const caps : Record < string , unknown > = {
54+ platformName : p . platform ,
55+ 'appium:deviceName' : p . deviceName ,
56+ ...( p . platformVersion !== undefined && { 'appium:platformVersion' : p . platformVersion } ) ,
57+ ...( p . automationName !== undefined && { 'appium:automationName' : p . automationName } ) ,
58+ ...( p . appPath !== undefined && { 'appium:app' : p . appPath } ) ,
59+ ...( p . udid !== undefined && { 'appium:udid' : p . udid } ) ,
60+ ...( p . noReset !== undefined && { 'appium:noReset' : p . noReset } ) ,
61+ ...( p . fullReset !== undefined && { 'appium:fullReset' : p . fullReset } ) ,
62+ ...( p . autoGrantPermissions !== undefined && { 'appium:autoGrantPermissions' : p . autoGrantPermissions } ) ,
63+ ...( p . autoAcceptAlerts !== undefined && { 'appium:autoAcceptAlerts' : p . autoAcceptAlerts } ) ,
64+ ...( p . autoDismissAlerts !== undefined && { 'appium:autoDismissAlerts' : p . autoDismissAlerts } ) ,
65+ ...( p . appWaitActivity !== undefined && { 'appium:appWaitActivity' : p . appWaitActivity } ) ,
66+ ...( p . newCommandTimeout !== undefined && { 'appium:newCommandTimeout' : p . newCommandTimeout } ) ,
67+ ...( ( p . capabilities as Record < string , unknown > | undefined ) ?? { } ) ,
68+ } ;
69+ const config : Record < string , unknown > = {
70+ protocol : 'http' ,
71+ hostname : p . appiumHost ?? 'localhost' ,
72+ port : p . appiumPort ?? 4723 ,
73+ path : p . appiumPath ?? '/' ,
74+ capabilities : caps ,
75+ } ;
76+ return `const browser = await remote(${ indentJson ( config ) } );` ;
77+ }
2778 case 'navigate' :
2879 return `await browser.url('${ escapeStr ( p . url ) } ');` ;
2980 case 'click_element' :
@@ -51,16 +102,7 @@ function generateStep(step: RecordedStep): string {
51102 }
52103}
53104
54- function buildHeader ( history : SessionHistory ) : string {
55- const caps = JSON . stringify ( history . capabilities , null , 2 )
56- . split ( '\n' )
57- . map ( ( line ) => ` ${ line } ` )
58- . join ( '\n' ) ;
59- return `import { remote } from 'webdriverio';\nconst browser = await remote(\n${ caps } \n);` ;
60- }
61-
62105export function generateCode ( history : SessionHistory ) : string {
63- const header = buildHeader ( history ) ;
64106 const steps = history . steps . map ( generateStep ) . join ( '\n' ) ;
65- return `${ header } \n\n${ steps } \n\nawait browser.deleteSession();` ;
107+ return `import { remote } from 'webdriverio'; \n\n${ steps } \n\nawait browser.deleteSession();` ;
66108}
0 commit comments