@@ -9,6 +9,18 @@ import {
99 ScreenshotResult ,
1010 ConsoleLog ,
1111 ViewportSize ,
12+ NavigationOptions ,
13+ ClickOptions ,
14+ TypeOptions ,
15+ HoverOptions ,
16+ ScrollDirection ,
17+ ScrollOptions ,
18+ ResizeOptions ,
19+ ScreenshotOptions ,
20+ ScriptOptions ,
21+ WaitOptions ,
22+ LogOptions ,
23+ BrowserEvent ,
1224} from "../../interfaces"
1325
1426/**
@@ -24,15 +36,18 @@ export interface ApiBrowserOptions {
2436 * by delegating to the underlying CLI browser implementation
2537 */
2638export class ApiBrowserSession implements IBrowserSession {
27- private sessionId : string
39+ public readonly id : string
40+ public isActive = false
2841 private options : ApiBrowserOptions
29- private isConnected = false
3042 private currentUrl ?: string
3143 private viewport : ViewportSize = { width : 1280 , height : 720 }
44+ private consoleLogs : ConsoleLog [ ] = [ ]
45+ private eventListeners : Map < BrowserEvent , ( ( data : any ) => void ) [ ] > = new Map ( )
3246
3347 constructor ( sessionId : string , options : ApiBrowserOptions = { } ) {
34- this . sessionId = sessionId
48+ this . id = sessionId
3549 this . options = options
50+ this . isActive = true
3651 }
3752
3853 private log ( message : string ) : void {
@@ -41,113 +56,158 @@ export class ApiBrowserSession implements IBrowserSession {
4156 }
4257 }
4358
44- async navigate ( url : string ) : Promise < BrowserActionResult > {
59+ async navigateToUrl ( url : string , options ?: NavigationOptions ) : Promise < BrowserActionResult > {
4560 this . log ( `Navigate to: ${ url } ` )
4661 this . currentUrl = url
4762
4863 // In API context, this would delegate to actual browser implementation
4964 return {
50- success : true ,
51- message : `Navigated to ${ url } ` ,
52- timestamp : new Date ( ) ,
65+ currentUrl : url ,
66+ screenshot : "mock-screenshot-base64" ,
67+ logs : "Navigation completed" ,
5368 }
5469 }
5570
56- async click ( selector : string ) : Promise < BrowserActionResult > {
57- this . log ( `Click: ${ selector } ` )
71+ async click ( coordinate : string , options ?: ClickOptions ) : Promise < BrowserActionResult > {
72+ this . log ( `Click: ${ coordinate } ` )
5873
5974 return {
60- success : true ,
61- message : `Clicked ${ selector } ` ,
62- timestamp : new Date ( ) ,
75+ currentUrl : this . currentUrl ,
76+ screenshot : "mock-screenshot-base64" ,
77+ logs : `Clicked at ${ coordinate } ` ,
6378 }
6479 }
6580
66- async type ( selector : string , text : string ) : Promise < BrowserActionResult > {
67- this . log ( `Type in ${ selector } : ${ text } ` )
81+ async type ( text : string , options ?: TypeOptions ) : Promise < BrowserActionResult > {
82+ this . log ( `Type: ${ text } ` )
6883
6984 return {
70- success : true ,
71- message : `Typed " ${ text } " in ${ selector } ` ,
72- timestamp : new Date ( ) ,
85+ currentUrl : this . currentUrl ,
86+ screenshot : "mock-screenshot-base64" ,
87+ logs : `Typed: ${ text } ` ,
7388 }
7489 }
7590
76- async waitForSelector ( selector : string , timeout ?: number ) : Promise < BrowserActionResult > {
77- this . log ( `Wait for selector : ${ selector } ` )
91+ async hover ( coordinate : string , options ?: HoverOptions ) : Promise < BrowserActionResult > {
92+ this . log ( `Hover : ${ coordinate } ` )
7893
7994 return {
80- success : true ,
81- message : `Found selector ${ selector } ` ,
82- timestamp : new Date ( ) ,
95+ currentUrl : this . currentUrl ,
96+ screenshot : "mock-screenshot-base64" ,
97+ logs : `Hovered at ${ coordinate } ` ,
98+ currentMousePosition : coordinate ,
8399 }
84100 }
85101
86- async screenshot ( ) : Promise < ScreenshotResult > {
87- this . log ( `Taking screenshot ` )
102+ async scroll ( direction : ScrollDirection , options ?: ScrollOptions ) : Promise < BrowserActionResult > {
103+ this . log ( `Scroll: ${ direction } ` )
88104
89- // In API context, this would return actual screenshot data
90105 return {
91- success : true ,
92- data : Buffer . from ( "mock-screenshot-data" ) ,
93- format : "png" ,
94- timestamp : new Date ( ) ,
106+ currentUrl : this . currentUrl ,
107+ screenshot : "mock-screenshot-base64" ,
108+ logs : `Scrolled ${ direction } ` ,
95109 }
96110 }
97111
98- async getContent ( ) : Promise < string > {
99- this . log ( `Getting page content` )
112+ async resize ( size : string , options ?: ResizeOptions ) : Promise < BrowserActionResult > {
113+ this . log ( `Resize: ${ size } ` )
114+ const [ width , height ] = size . split ( "," ) . map ( ( s ) => parseInt ( s . trim ( ) ) )
115+ this . viewport = { width, height }
100116
101- // In API context, this would return actual page content
102- return "<html><body>Mock page content</body></html>"
117+ return {
118+ currentUrl : this . currentUrl ,
119+ screenshot : "mock-screenshot-base64" ,
120+ logs : `Resized to ${ size } ` ,
121+ }
103122 }
104123
105- async executeScript ( script : string ) : Promise < any > {
124+ async screenshot ( options ?: ScreenshotOptions ) : Promise < ScreenshotResult > {
125+ this . log ( `Taking screenshot` )
126+
127+ // In API context, this would return actual screenshot data
128+ return {
129+ data : "mock-screenshot-base64-data" ,
130+ format : options ?. format || "png" ,
131+ width : this . viewport . width ,
132+ height : this . viewport . height ,
133+ }
134+ }
135+
136+ async executeScript ( script : string , options ?: ScriptOptions ) : Promise < any > {
106137 this . log ( `Execute script: ${ script } ` )
107138
108139 // In API context, this would execute actual JavaScript
109140 return { result : "mock-script-result" }
110141 }
111142
112- async close ( ) : Promise < void > {
113- this . log ( `Closing browser session` )
114- this . isConnected = false
143+ async waitForElement ( selector : string , options ?: WaitOptions ) : Promise < boolean > {
144+ this . log ( `Wait for element: ${ selector } ` )
145+ // In API context, this would wait for actual element
146+ return true
115147 }
116148
117- getSessionId ( ) : string {
118- return this . sessionId
149+ async waitForNavigation ( options ?: WaitOptions ) : Promise < boolean > {
150+ this . log ( `Wait for navigation` )
151+ // In API context, this would wait for actual navigation
152+ return true
119153 }
120154
121- isActive ( ) : boolean {
122- return this . isConnected
155+ async getCurrentUrl ( ) : Promise < string > {
156+ return this . currentUrl || "about:blank"
123157 }
124158
125- getCurrentUrl ( ) : string | undefined {
126- return this . currentUrl
159+ async getTitle ( ) : Promise < string > {
160+ this . log ( `Getting page title` )
161+ return "Mock Page Title"
127162 }
128163
129- getViewport ( ) : ViewportSize {
130- return this . viewport
164+ async getContent ( ) : Promise < string > {
165+ this . log ( `Getting page content` )
166+ // In API context, this would return actual page content
167+ return "<html><body>Mock page content</body></html>"
131168 }
132169
133- async setViewport ( viewport : ViewportSize ) : Promise < void > {
134- this . log ( `Set viewport: ${ viewport . width } x ${ viewport . height } ` )
135- this . viewport = viewport
170+ async getConsoleLogs ( options ?: LogOptions ) : Promise < ConsoleLog [ ] > {
171+ this . log ( `Getting console logs ` )
172+ return this . consoleLogs
136173 }
137174
138- onConsole ( callback : ( log : ConsoleLog ) => void ) : void {
139- this . log ( `Console listener registered ` )
140- // In API context, this would listen for actual console events
175+ async clearConsoleLogs ( ) : Promise < void > {
176+ this . log ( `Clearing console logs ` )
177+ this . consoleLogs = [ ]
141178 }
142179
143- onRequest ( callback : ( url : string , method : string ) => void ) : void {
144- this . log ( `Request listener registered` )
145- // In API context, this would listen for actual network requests
180+ async setViewport ( width : number , height : number ) : Promise < void > {
181+ this . log ( `Set viewport: ${ width } x${ height } ` )
182+ this . viewport = { width, height }
183+ }
184+
185+ async getViewport ( ) : Promise < ViewportSize > {
186+ return this . viewport
146187 }
147188
148- onResponse ( callback : ( url : string , status : number ) => void ) : void {
149- this . log ( `Response listener registered` )
150- // In API context, this would listen for actual network responses
189+ async close ( ) : Promise < void > {
190+ this . log ( `Closing browser session` )
191+ this . isActive = false
192+ }
193+
194+ on ( event : BrowserEvent , callback : ( data : any ) => void ) : void {
195+ this . log ( `Event listener added: ${ event } ` )
196+ if ( ! this . eventListeners . has ( event ) ) {
197+ this . eventListeners . set ( event , [ ] )
198+ }
199+ this . eventListeners . get ( event ) ! . push ( callback )
200+ }
201+
202+ off ( event : BrowserEvent , callback : ( data : any ) => void ) : void {
203+ this . log ( `Event listener removed: ${ event } ` )
204+ const listeners = this . eventListeners . get ( event )
205+ if ( listeners ) {
206+ const index = listeners . indexOf ( callback )
207+ if ( index > - 1 ) {
208+ listeners . splice ( index , 1 )
209+ }
210+ }
151211 }
152212}
153213
@@ -194,18 +254,30 @@ export class ApiBrowser implements IBrowser {
194254 return session
195255 }
196256
197- async getInstalledBrowsers ( ) : Promise < BrowserType [ ] > {
198- this . log ( `Getting installed browsers` )
257+ async getAvailableBrowsers ( ) : Promise < BrowserType [ ] > {
258+ this . log ( `Getting available browsers` )
199259
200260 // In API context, this would detect actual installed browsers
201- return [ "chromium" , "firefox" , "webkit" ]
261+ return [ "chromium" , "firefox" , "chrome" ] as BrowserType [ ]
262+ }
263+
264+ async isBrowserInstalled ( browserType : BrowserType ) : Promise < boolean > {
265+ this . log ( `Checking if browser is installed: ${ browserType } ` )
266+ // In API context, this would check actual browser installation
267+ return true
202268 }
203269
204- async installBrowser ( browser : BrowserType , options ?: BrowserInstallOptions ) : Promise < boolean > {
205- this . log ( `Installing browser: ${ browser } ` )
270+ async getBrowserExecutablePath ( browserType : BrowserType ) : Promise < string | undefined > {
271+ this . log ( `Getting browser executable path: ${ browserType } ` )
272+ // In API context, this would return actual browser path
273+ return "/mock/browser/path"
274+ }
275+
276+ async installBrowser ( browserType : BrowserType , options ?: BrowserInstallOptions ) : Promise < void > {
277+ this . log ( `Installing browser: ${ browserType } ` )
206278
207279 // In API context, this would perform actual browser installation
208- return true
280+ // No return value as per interface
209281 }
210282
211283 async getBrowserVersion ( browser : BrowserType ) : Promise < string | null > {
@@ -233,7 +305,7 @@ export class ApiBrowser implements IBrowser {
233305 }
234306
235307 async getActiveSessions ( ) : Promise < IBrowserSession [ ] > {
236- return Array . from ( this . sessions . values ( ) ) . filter ( ( session ) => session . isActive ( ) )
308+ return Array . from ( this . sessions . values ( ) ) . filter ( ( session ) => session . isActive )
237309 }
238310
239311 async closeSession ( sessionId : string ) : Promise < void > {
@@ -255,7 +327,7 @@ export class ApiBrowser implements IBrowser {
255327 }
256328
257329 getDefaultBrowser ( ) : BrowserType {
258- return "chromium"
330+ return "chromium" as BrowserType
259331 }
260332
261333 isHeadlessSupported ( ) : boolean {
0 commit comments