11import { CommandShape } from '@seleniumhq/side-model'
2- import { getActiveCommand , getActiveTest , getActiveCommandIndex } from '@seleniumhq/side-api/dist/helpers/getActiveData'
3- import { LocatorFields , CoreSessionData , RecordNewCommandInput } from '@seleniumhq/side-api'
2+ import {
3+ getActiveCommand ,
4+ getActiveTest ,
5+ getActiveCommandIndex ,
6+ } from '@seleniumhq/side-api/dist/helpers/getActiveData'
7+ import {
8+ LocatorFields ,
9+ CoreSessionData ,
10+ RecordNewCommandInput ,
11+ } from '@seleniumhq/side-api'
412import { randomInt , randomUUID } from 'crypto'
513import { relative } from 'node:path'
614import BaseController from '../Base'
@@ -46,16 +54,17 @@ const getFrameTraversalCommands = (
4654const getLastActiveWindowHandleId = ( session : CoreSessionData ) : string => {
4755 const activeTest = getActiveTest ( session )
4856 const activeIndex = getActiveCommandIndex ( session )
49- console . log ( activeTest , activeIndex )
50- const commands = activeTest . commands
51- for ( let i = activeIndex ; i >= 0 ; i -- ) {
52- let item = commands [ i ]
53- if ( item . command == 'selectWindow' ) {
54- let target = item . target as string
55- return target . substring ( 'handle=${' . length , target . length - 1 )
56- }
57- if ( item . command == 'storeWindowHandle' ) {
58- return item . target as string
57+ if ( activeIndex > - 1 ) {
58+ const commands = activeTest . commands
59+ for ( let i = activeIndex ; i >= 0 ; i -- ) {
60+ let item = commands [ i ]
61+ if ( item . command == 'selectWindow' ) {
62+ let target = item . target as string
63+ return target . substring ( 'handle=${' . length , target . length - 1 )
64+ }
65+ if ( item . command == 'storeWindowHandle' ) {
66+ return item . target as string
67+ }
5968 }
6069 }
6170
@@ -72,14 +81,12 @@ export default class RecorderController extends BaseController {
7281 return null
7382 }
7483 const commands = [ ]
75- if (
76- getLastActiveWindowHandleId ( session ) != cmd . winHandleId
77- ) {
84+ if ( getLastActiveWindowHandleId ( session ) != cmd . winHandleId ) {
7885 const selectWindowCommand : CommandShape = {
7986 id : randomUUID ( ) ,
8087 command : 'selectWindow' ,
81- target : 'handle=${' + cmd . winHandleId + '}' ,
82- value : ''
88+ target : 'handle=${' + cmd . winHandleId + '}' ,
89+ value : '' ,
8390 }
8491 commands . push ( selectWindowCommand )
8592 }
@@ -90,19 +97,21 @@ export default class RecorderController extends BaseController {
9097 targets : Array . isArray ( cmd . target ) ? cmd . target : [ [ cmd . target , '' ] ] ,
9198 value : Array . isArray ( cmd . value ) ? cmd . value [ 0 ] [ 0 ] : cmd . value ,
9299 }
93- const windows = BrowserWindow . getAllWindows ( ) ;
94- const newWindowIDs = windows . map ( ( window ) => window . id ) ;
95- const opensWindow = this . windowIDs . length < newWindowIDs . length ;
100+ const windows = BrowserWindow . getAllWindows ( )
101+ const newWindowIDs = windows . map ( ( window ) => window . id )
102+ const opensWindow = this . windowIDs . length < newWindowIDs . length
96103 if ( opensWindow ) {
97104 mainCommand . opensWindow = true
98105 mainCommand . windowHandleName = `win${ randomInt ( 1 , 9999 ) } `
99106 }
100- this . windowIDs = windows . map ( ( window ) => window . id ) ;
107+ this . windowIDs = windows . map ( ( window ) => window . id )
101108
102- commands . push ( ...getFrameTraversalCommands (
103- session . state . recorder . activeFrame ,
104- cmd . frameLocation as string
105- ) )
109+ commands . push (
110+ ...getFrameTraversalCommands (
111+ session . state . recorder . activeFrame ,
112+ cmd . frameLocation as string
113+ )
114+ )
106115 commands . push ( mainCommand )
107116 return commands
108117 }
@@ -137,16 +146,18 @@ export default class RecorderController extends BaseController {
137146 const activeTest = getActiveTest ( session )
138147 const activeIndex = getActiveCommandIndex ( session )
139148
140- const commands = activeTest . commands
141- for ( let i = activeIndex ; i >= 0 ; i -- ) {
142- let item = commands [ i ]
143- if ( item . opensWindow && item . windowHandleName ) {
144- return item . windowHandleName
145- }
146-
147- if ( item . command == 'selectWindow' ) {
148- let target = item . target as string
149- return target . substring ( 'handle=${' . length , target . length - 1 )
149+ if ( activeIndex > - 1 ) {
150+ const commands = activeTest . commands
151+ for ( let i = activeIndex ; i >= 0 ; i -- ) {
152+ let item = commands [ i ]
153+ if ( item . opensWindow && item . windowHandleName ) {
154+ return item . windowHandleName
155+ }
156+
157+ if ( item . command == 'selectWindow' ) {
158+ let target = item . target as string
159+ return target . substring ( 'handle=${' . length , target . length - 1 )
160+ }
150161 }
151162 }
152163 return 'root'
@@ -192,7 +203,6 @@ export default class RecorderController extends BaseController {
192203 }
193204 let url = new URL ( currentCommand . target as string , state . project . url )
194205 playbackWindow . webContents . loadURL ( url . toString ( ) )
195-
196206 }
197207 playbackWindow . focus ( )
198208 return null
0 commit comments