Skip to content

Commit

Permalink
feat!: add support for browser provider capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
haywirez committed Jul 12, 2023
1 parent a90d64f commit c9b15da
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 32 deletions.
22 changes: 22 additions & 0 deletions docs/guide/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ npx vitest --browser.name=chrome --browser.headless

In this case, Vitest will run in headless mode using the Chrome browser.

## Custom Provider Options

You may pass provider-specific options, such as [WebdriverIO custom capabilities](https://webdriver.io/docs/capabilities#custom-capabilities) like:

```ts
export default defineConfig({
test: {
browser: {
enabled: true,
headless: true,
},
providerOptions: {
webdriverio: {
'goog:chromeOptions': {
args: ['disable-gpu'],
},
},
},
},
})
```

## Limitations
### Thread Blocking Dialogs

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@vitest/coverage-istanbul": "workspace:*",
"@vitest/coverage-v8": "workspace:*",
"@vitest/ui": "workspace:*",
"@wdio/types": "^8.10.4",
"bumpp": "^9.1.1",
"esbuild": "^0.18.11",
"eslint": "^8.44.0",
Expand Down
9 changes: 6 additions & 3 deletions packages/vitest/src/node/browser/playwright.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Page } from 'playwright'
import type { LaunchOptions, Page } from 'playwright'
import type { BrowserProvider, BrowserProviderOptions } from '../../types/browser'
import { ensurePackageInstalled } from '../pkg'
import type { WorkspaceProject } from '../workspace'
Expand All @@ -9,22 +9,25 @@ export type PlaywrightBrowser = typeof playwrightBrowsers[number]

export interface PlaywrightProviderOptions extends BrowserProviderOptions {
browser: PlaywrightBrowser
providerOptions: { playwright: LaunchOptions }
}

export class PlaywrightBrowserProvider implements BrowserProvider {
public name = 'playwright'

private cachedBrowser: Page | null = null
private browser!: PlaywrightBrowser
private providerOptions?: LaunchOptions
private ctx!: WorkspaceProject

getSupportedBrowsers() {
return playwrightBrowsers
}

async initialize(ctx: WorkspaceProject, { browser }: PlaywrightProviderOptions) {
async initialize(ctx: WorkspaceProject, { browser, providerOptions }: PlaywrightProviderOptions) {
this.ctx = ctx
this.browser = browser
this.providerOptions = providerOptions?.playwright

const root = this.ctx.config.root

Expand All @@ -40,7 +43,7 @@ export class PlaywrightBrowserProvider implements BrowserProvider {

const playwright = await import('playwright')

const playwrightInstance = await playwright[this.browser].launch({ headless: options.headless })
const playwrightInstance = await playwright[this.browser].launch({ headless: options.headless, ...this.providerOptions })
this.cachedBrowser = await playwrightInstance.newPage()

this.cachedBrowser.on('close', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/vitest/src/node/browser/webdriver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Awaitable } from '@vitest/utils'
import type { Capabilities } from '@wdio/types'
import type { BrowserProvider, BrowserProviderOptions } from '../../types/browser'
import { ensurePackageInstalled } from '../pkg'
import type { WorkspaceProject } from '../workspace'
Expand All @@ -8,6 +9,7 @@ export type WebdriverBrowser = typeof webdriverBrowsers[number]

export interface WebdriverProviderOptions extends BrowserProviderOptions {
browser: WebdriverBrowser
providerOptions?: { webdriverio: Capabilities.Capabilities }
}

export class WebdriverBrowserProvider implements BrowserProvider {
Expand All @@ -16,15 +18,17 @@ export class WebdriverBrowserProvider implements BrowserProvider {
private cachedBrowser: WebdriverIO.Browser | null = null
private stopSafari: () => void = () => {}
private browser!: WebdriverBrowser
private providerOptions?: Capabilities.Capabilities
private ctx!: WorkspaceProject

getSupportedBrowsers() {
return webdriverBrowsers
}

async initialize(ctx: WorkspaceProject, { browser }: WebdriverProviderOptions) {
async initialize(ctx: WorkspaceProject, { browser, providerOptions }: WebdriverProviderOptions) {
this.ctx = ctx
this.browser = browser
this.providerOptions = providerOptions?.webdriverio

const root = this.ctx.config.root

Expand Down Expand Up @@ -59,6 +63,7 @@ export class WebdriverBrowserProvider implements BrowserProvider {
capabilities: {
'browserName': this.browser,
'wdio:devtoolsOptions': { headless: options.headless },
...this.providerOptions,
},
})

Expand Down
Loading

0 comments on commit c9b15da

Please sign in to comment.