| 103 | } |
| 104 | |
| 105 | export class PlaywrightBrowserProvider implements BrowserProvider { |
| 106 | public name = 'playwright' as const |
| 107 | public supportsParallelism = true |
| 108 | |
| 109 | public browser: Browser | null = null |
| 110 | public persistentContext: BrowserContext | null = null |
| 111 | |
| 112 | public contexts: Map<string, BrowserContext> = new Map() |
| 113 | public pages: Map<string, Page> = new Map() |
| 114 | public mocker: BrowserModuleMocker |
| 115 | public browserName: PlaywrightBrowser |
| 116 | |
| 117 | private browserPromise: Promise<Browser> | null = null |
| 118 | private closing = false |
| 119 | |
| 120 | public tracingContexts: Set<string> = new Set() |
| 121 | public pendingTraces: Map<string, string> = new Map() |
| 122 | |
| 123 | public initScripts: string[] = [ |
| 124 | resolve(distRoot, 'locators.js'), |
| 125 | ] |
| 126 | |
| 127 | constructor( |
| 128 | private project: TestProject, |
| 129 | private options: PlaywrightProviderOptions, |
| 130 | ) { |
| 131 | this.browserName = project.config.browser.name as PlaywrightBrowser |
| 132 | this.mocker = this.createMocker() |
| 133 | |
| 134 | for (const [name, command] of Object.entries(commands)) { |
| 135 | project.browser!.registerCommand(name as any, command as BrowserCommand) |
| 136 | } |
| 137 | |
| 138 | // make sure the traces are finished if the test hangs |
| 139 | process.on('SIGTERM', this.onSIGTERM) |
| 140 | } |
| 141 | |
| 142 | private onSIGTERM = () => { |
| 143 | if (!this.browser) { |
| 144 | return |
| 145 | } |
| 146 | const promises = [] |
| 147 | for (const [trace, contextId] of this.pendingTraces.entries()) { |
| 148 | promises.push((() => { |
| 149 | const context = this.contexts.get(contextId) |
| 150 | return context?.tracing.stopChunk({ path: trace }) |
| 151 | })()) |
| 152 | } |
| 153 | return Promise.allSettled(promises) |
| 154 | } |
| 155 | |
| 156 | private async openBrowser(openBrowserOptions: { parallel: boolean }) { |
| 157 | await this._throwIfClosing() |
| 158 | |
| 159 | if (this.browserPromise) { |
| 160 | debug?.('[%s] the browser is resolving, reusing the promise', this.browserName) |
| 161 | return this.browserPromise |
| 162 | } |