-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: playwright as browser provider (#3079)
Co-authored-by: Vladimir Sheremet <[email protected]>
- Loading branch information
1 parent
280ad1e
commit 9dc6929
Showing
22 changed files
with
239 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { Page } from 'playwright' | ||
import type { BrowserProvider, BrowserProviderOptions } from '../../types/browser' | ||
import { ensurePackageInstalled } from '../pkg' | ||
import type { Vitest } from '../core' | ||
|
||
export const playwrightBrowsers = ['firefox', 'webkit', 'chromium'] as const | ||
export type PlaywrightBrowser = typeof playwrightBrowsers[number] | ||
|
||
export interface PlaywrightProviderOptions extends BrowserProviderOptions { | ||
browser: PlaywrightBrowser | ||
} | ||
|
||
export class PlaywrightBrowserProvider implements BrowserProvider { | ||
public name = 'playwright' | ||
|
||
private cachedBrowser: Page | null = null | ||
private browser!: PlaywrightBrowser | ||
private ctx!: Vitest | ||
|
||
getSupportedBrowsers() { | ||
return playwrightBrowsers | ||
} | ||
|
||
async initialize(ctx: Vitest, { browser }: PlaywrightProviderOptions) { | ||
this.ctx = ctx | ||
this.browser = browser | ||
|
||
const root = this.ctx.config.root | ||
|
||
if (!await ensurePackageInstalled('playwright', root)) | ||
throw new Error('Cannot find "playwright" package. Please install it manually.') | ||
} | ||
|
||
async openBrowser() { | ||
if (this.cachedBrowser) | ||
return this.cachedBrowser | ||
|
||
const options = this.ctx.config.browser | ||
|
||
const playwright = await import('playwright') | ||
|
||
const playwrightInstance = await playwright[this.browser].launch({ headless: options.headless }) | ||
this.cachedBrowser = await playwrightInstance.newPage() | ||
|
||
this.cachedBrowser.on('close', () => { | ||
playwrightInstance.close() | ||
}) | ||
|
||
return this.cachedBrowser | ||
} | ||
|
||
async openPage(url: string) { | ||
const browserInstance = await this.openBrowser() | ||
await browserInstance.goto(url) | ||
} | ||
|
||
async close() { | ||
await this.cachedBrowser?.close() | ||
// TODO: right now process can only exit with timeout, if we use browser | ||
// needs investigating | ||
process.exit() | ||
} | ||
} |
Oops, something went wrong.