Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: connecting to electron browser #26471

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/server/lib/browsers/cdp_automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import debugModule from 'debug'
import { URL } from 'url'

import type { ResourceType, BrowserPreRequest, BrowserResponseReceived } from '@packages/proxy'
import type { WriteVideoFrame } from '@packages/types'
import type { CDPClient, WriteVideoFrame } from '@packages/types'
import type { Automation } from '../automation'
import { cookieMatches, CyCookie, CyCookieFilter } from '../automation/util'

Expand Down Expand Up @@ -154,10 +154,16 @@ const ffToStandardResourceTypeMap: { [ff: string]: ResourceType } = {
'webmanifest': 'manifest',
}

export class CdpAutomation {
export class CdpAutomation implements CDPClient {
on: OnFn
send: SendDebuggerCommand

private constructor (private sendDebuggerCommandFn: SendDebuggerCommand, private onFn: OnFn, private sendCloseCommandFn: SendCloseCommand, private automation: Automation) {
onFn('Network.requestWillBeSent', this.onNetworkRequestWillBeSent)
onFn('Network.responseReceived', this.onResponseReceived)

this.on = onFn
this.send = sendDebuggerCommandFn
}

async startVideoRecording (writeVideoFrame: WriteVideoFrame, screencastOpts) {
Expand Down
9 changes: 4 additions & 5 deletions packages/server/lib/browsers/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as errors from '../errors'
import type { Browser, BrowserInstance } from './types'
import type { BrowserWindow, WebContents } from 'electron'
import type { Automation } from '../automation'
import type { BrowserLaunchOpts, Preferences, RunModeVideoApi } from '@packages/types'
import type { BrowserLaunchOpts, Preferences, ProtocolManager, RunModeVideoApi } from '@packages/types'
import memory from './memory'

// TODO: unmix these two types
Expand Down Expand Up @@ -202,9 +202,7 @@ export = {
win.maximize()
}

const launched = await this._launch(win, url, automation, preferences, options.videoApi)

automation.use(await _getAutomation(win, preferences, automation))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed since the automation is already set in _launch

const launched = await this._launch(win, url, automation, preferences, options.videoApi, options.protocolManager)

return launched
},
Expand Down Expand Up @@ -235,7 +233,7 @@ export = {
return this._launch(win, url, automation, electronOptions)
},

async _launch (win: BrowserWindow, url: string, automation: Automation, options: ElectronOpts, videoApi?: RunModeVideoApi) {
async _launch (win: BrowserWindow, url: string, automation: Automation, options: ElectronOpts, videoApi?: RunModeVideoApi, protocolManager?: ProtocolManager) {
if (options.show) {
menu.set({ withInternalDevTools: true })
}
Expand Down Expand Up @@ -283,6 +281,7 @@ export = {
automation.use(cdpAutomation)

await Promise.all([
protocolManager?.connectToBrowser(cdpAutomation),
videoApi && recordVideo(cdpAutomation, videoApi),
this._handleDownloads(win, options.downloadsFolder, automation),
])
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Command<T extends keyof Commands> = Commands[T]
type Events = ProtocolMapping.Events
type Event<T extends keyof Events> = Events[T]

interface CDPClient {
export interface CDPClient {
send<T extends Extract<keyof Commands, string>> (command: T, params?: Command<T>['paramsType'][0]): Promise<Command<T>['returnType']>
on<T extends Extract<keyof Events, string>> (eventName: T, cb: (event: Event<T>[0]) => void): void
}
Expand Down