-
-
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!: move browser providers to @vitest/browser package (#4364)
- Loading branch information
1 parent
2af2ba7
commit 5cdeb55
Showing
29 changed files
with
500 additions
and
666 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { BrowserProvider } from 'vitest/nide' | ||
|
||
declare var webdriverio: BrowserProvider | ||
declare var playwright: BrowserProvider | ||
|
||
export { webdriverio, playwright } |
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,8 @@ | ||
import type { Browser, LaunchOptions } from 'playwright' | ||
|
||
declare module 'vitest/node' { | ||
interface BrowserProviderOptions { | ||
launch?: LaunchOptions | ||
page?: Parameters<Browser['newPage']>[0] | ||
} | ||
} |
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,5 @@ | ||
import type { RemoteOptions } from 'webdriverio' | ||
|
||
declare module 'vitest/node' { | ||
interface BrowserProviderOptions extends RemoteOptions {} | ||
} |
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,5 @@ | ||
import { PlaywrightBrowserProvider } from './playwright' | ||
import { WebdriverBrowserProvider } from './webdriver' | ||
|
||
export const webdriverio = WebdriverBrowserProvider | ||
export const playwright = PlaywrightBrowserProvider |
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,80 @@ | ||
import type { Browser, LaunchOptions, Page } from 'playwright' | ||
import type { BrowserProvider, BrowserProviderInitializationOptions, WorkspaceProject } from 'vitest/node' | ||
import { ensurePackageInstalled } from 'vitest/node' | ||
|
||
type Awaitable<T> = T | PromiseLike<T> | ||
|
||
export const playwrightBrowsers = ['firefox', 'webkit', 'chromium'] as const | ||
export type PlaywrightBrowser = typeof playwrightBrowsers[number] | ||
|
||
export interface PlaywrightProviderOptions extends BrowserProviderInitializationOptions { | ||
browser: PlaywrightBrowser | ||
} | ||
|
||
export class PlaywrightBrowserProvider implements BrowserProvider { | ||
public name = 'playwright' | ||
|
||
private cachedBrowser: Browser | null = null | ||
private cachedPage: Page | null = null | ||
private browser!: PlaywrightBrowser | ||
private ctx!: WorkspaceProject | ||
|
||
private options?: { | ||
launch?: LaunchOptions | ||
page?: Parameters<Browser['newPage']>[0] | ||
} | ||
|
||
getSupportedBrowsers() { | ||
return playwrightBrowsers | ||
} | ||
|
||
async initialize(ctx: WorkspaceProject, { browser, options }: PlaywrightProviderOptions) { | ||
this.ctx = ctx | ||
this.browser = browser | ||
this.options = options as any | ||
|
||
const root = this.ctx.config.root | ||
|
||
if (!await ensurePackageInstalled('playwright', root)) | ||
throw new Error('Cannot find "playwright" package. Please install it manually.') | ||
} | ||
|
||
private async openBrowserPage() { | ||
if (this.cachedPage) | ||
return this.cachedPage | ||
|
||
const options = this.ctx.config.browser | ||
|
||
const playwright = await import('playwright') | ||
|
||
const browser = await playwright[this.browser].launch({ | ||
...this.options?.launch, | ||
headless: options.headless, | ||
}) | ||
this.cachedBrowser = browser | ||
this.cachedPage = await browser.newPage(this.options?.page) | ||
|
||
this.cachedPage.on('close', () => { | ||
browser.close() | ||
}) | ||
|
||
return this.cachedPage | ||
} | ||
|
||
catchError(cb: (error: Error) => Awaitable<void>) { | ||
this.cachedPage?.on('pageerror', cb) | ||
return () => { | ||
this.cachedPage?.off('pageerror', cb) | ||
} | ||
} | ||
|
||
async openPage(url: string) { | ||
const browserPage = await this.openBrowserPage() | ||
await browserPage.goto(url) | ||
} | ||
|
||
async close() { | ||
await this.cachedPage?.close() | ||
await this.cachedBrowser?.close() | ||
} | ||
} |
Oops, something went wrong.