forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriverio.ts
More file actions
337 lines (287 loc) · 10.7 KB
/
webdriverio.ts
File metadata and controls
337 lines (287 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import type { CustomComparatorsRegistry } from '@vitest/browser'
import type { Capabilities } from '@wdio/types'
import type {
ScreenshotComparatorRegistry,
ScreenshotMatcherOptions,
SelectorOptions,
} from 'vitest/browser'
import type {
BrowserCommand,
BrowserProvider,
BrowserProviderOption,
CDPSession,
TestProject,
} from 'vitest/node'
import type { ClickOptions, DragAndDropOptions, MoveToOptions, remote } from 'webdriverio'
import { defineBrowserProvider } from '@vitest/browser'
import { resolve } from 'pathe'
import { createDebugger } from 'vitest/node'
import commands from './commands'
import { distRoot } from './constants'
const debug = createDebugger('vitest:browser:wdio')
const webdriverBrowsers = ['firefox', 'chrome', 'edge', 'safari'] as const
type WebdriverBrowser = (typeof webdriverBrowsers)[number]
export interface WebdriverProviderOptions extends Partial<
Parameters<typeof remote>[0]
> {}
export function webdriverio(options: WebdriverProviderOptions = {}): BrowserProviderOption<WebdriverProviderOptions> {
return defineBrowserProvider({
name: 'webdriverio',
supportedBrowser: webdriverBrowsers,
options,
providerFactory(project) {
return new WebdriverBrowserProvider(project, options)
},
})
}
export class WebdriverBrowserProvider implements BrowserProvider {
public name = 'webdriverio' as const
public supportsParallelism: boolean = false
public browser: WebdriverIO.Browser | null = null
private browserName!: WebdriverBrowser
private project!: TestProject
private options?: WebdriverProviderOptions
private closing = false
private iframeSwitched = false
private topLevelContext: string | undefined
public initScripts: string[] = [
resolve(distRoot, 'locators.js'),
]
getSupportedBrowsers(): readonly string[] {
return webdriverBrowsers
}
constructor(
project: TestProject,
options: WebdriverProviderOptions,
) {
// increase shutdown timeout because WDIO takes some extra time to kill the driver
if (!project.vitest.state._data.timeoutIncreased) {
project.vitest.state._data.timeoutIncreased = true
project.vitest.config.teardownTimeout += 10_000
}
this.closing = false
this.project = project
this.browserName = project.config.browser.name as WebdriverBrowser
this.options = options
for (const [name, command] of Object.entries(commands)) {
project.browser!.registerCommand(name as any, command as BrowserCommand)
}
}
isIframeSwitched(): boolean {
return this.iframeSwitched
}
async switchToTestFrame(): Promise<void> {
const browser = this.browser!
// support wdio@9
if (browser.switchFrame) {
await browser.switchFrame(browser.$('iframe[data-vitest]'))
}
else {
const iframe = await browser.findElement(
'css selector',
'iframe[data-vitest]',
)
await browser.switchToFrame(iframe)
}
this.iframeSwitched = true
}
async switchToMainFrame(): Promise<void> {
const page = this.browser!
if (page.switchFrame) {
await page.switchFrame(null)
}
else {
await page.switchToParentFrame()
}
this.iframeSwitched = false
}
async setViewport(options: { width: number; height: number }): Promise<void> {
if (this.topLevelContext == null || !this.browser) {
throw new Error(`The browser has no open pages.`)
}
if (this.browser.isBidi) {
await this.browser.send({
method: 'browsingContext.setViewport',
params: {
context: this.topLevelContext,
devicePixelRatio: 1,
viewport: options,
},
})
}
else {
await this.browser.setWindowSize(options.width, options.height)
}
}
getCommandsContext(): {
browser: WebdriverIO.Browser | null
} {
return {
browser: this.browser,
}
}
async openBrowser(): Promise<WebdriverIO.Browser> {
await this._throwIfClosing('opening the browser')
if (this.browser) {
debug?.('[%s] the browser is already opened, reusing it', this.browserName)
return this.browser
}
const options = this.project.config.browser
if (this.browserName === 'safari') {
if (options.headless) {
throw new Error(
'You\'ve enabled headless mode for Safari but it doesn\'t currently support it.',
)
}
}
const { remote } = await import('webdriverio')
const remoteOptions: Capabilities.WebdriverIOConfig = {
logLevel: 'silent',
...this.options,
capabilities: this.buildCapabilities(),
}
debug?.('[%s] opening the browser with options: %O', this.browserName, remoteOptions)
// TODO: close everything, if browser is closed from the outside
this.browser = await remote(remoteOptions)
await this._throwIfClosing()
return this.browser
}
private buildCapabilities() {
const capabilities: Capabilities.WebdriverIOConfig['capabilities'] = {
...this.options?.capabilities,
browserName: this.browserName,
}
const headlessMap = {
chrome: ['goog:chromeOptions', ['headless', 'disable-gpu']],
firefox: ['moz:firefoxOptions', ['-headless']],
edge: ['ms:edgeOptions', ['--headless']],
} as const
const options = this.project.config.browser
const browser = this.browserName
if (browser !== 'safari' && options.headless) {
const [key, args] = headlessMap[browser]
const currentValues = (this.options?.capabilities as any)?.[key] || {}
const newArgs = [...(currentValues.args || []), ...args]
capabilities[key] = { ...currentValues, args: newArgs as any }
}
// start Vitest UI maximized only on supported browsers
if (options.ui && (browser === 'chrome' || browser === 'edge')) {
const key = browser === 'chrome'
? 'goog:chromeOptions'
: 'ms:edgeOptions'
const args = capabilities[key]?.args || []
if (!args.includes('--start-maximized') && !args.includes('--start-fullscreen')) {
args.push('--start-maximized')
}
capabilities[key] ??= {}
capabilities[key]!.args = args
}
const inspector = this.project.vitest.config.inspector
if (inspector.enabled && (browser === 'chrome' || browser === 'edge')) {
const key = browser === 'chrome'
? 'goog:chromeOptions'
: 'ms:edgeOptions'
const args = capabilities[key]?.args || []
// NodeJS equivalent defaults: https://nodejs.org/en/learn/getting-started/debugging#enable-inspector
const port = inspector.port || 9229
const host = inspector.host || '127.0.0.1'
args.push(`--remote-debugging-port=${port}`)
if (host !== 'localhost' && host !== '127.0.0.1' && host !== '::1') {
this.project.vitest.logger.warn(`Custom inspector host "${host}" will be ignored. Chrome only allows remote debugging on localhost.`)
}
this.project.vitest.logger.log(`Debugger listening on ws://127.0.0.1:${port}`)
capabilities[key] ??= {}
capabilities[key]!.args = args
}
return capabilities
}
async openPage(sessionId: string, url: string): Promise<void> {
await this._throwIfClosing('creating the browser')
debug?.('[%s][%s] creating the browser page for %s', sessionId, this.browserName, url)
const browserInstance = await this.openBrowser()
debug?.('[%s][%s] browser page is created, opening %s', sessionId, this.browserName, url)
await browserInstance.url(url)
this.topLevelContext = await browserInstance.getWindowHandle()
await this._throwIfClosing('opening the url')
}
private async _throwIfClosing(action?: string) {
if (this.closing) {
debug?.(`[%s] provider was closed, cannot perform the action${action ? ` ${action}` : ''}`, this.browserName)
await (this.browser?.sessionId ? this.browser?.deleteSession?.() : null)
throw new Error(`[vitest] The provider was closed.`)
}
}
async close(): Promise<void> {
debug?.('[%s] closing provider', this.browserName)
this.closing = true
const browser = this.browser
const sessionId = browser?.sessionId
if (!browser || !sessionId) {
return
}
// https://github.com/webdriverio/webdriverio/blob/ab1a2e82b13a9c7d0e275ae87e7357e1b047d8d3/packages/wdio-runner/src/index.ts#L486
await browser.deleteSession()
browser.sessionId = undefined as unknown as string
this.browser = null
}
async getCDPSession(_sessionId: string): Promise<CDPSession> {
return {
send: (method, params) => {
if (!this.browser) {
throw new Error(`The environment was torn down.`)
}
return this.browser.sendCommandAndGetResult(method, params ?? {}).catch((error) => {
return Promise.reject(new Error(`Failed to execute "${method}" command.`, { cause: error }))
})
},
on: () => {
throw new Error(`webdriverio provider doesn't support cdp.on()`)
},
once: () => {
throw new Error(`webdriverio provider doesn't support cdp.once()`)
},
off: () => {
throw new Error(`webdriverio provider doesn't support cdp.off()`)
},
}
}
}
declare module 'vitest/browser' {
export interface UserEventClickOptions extends Partial<ClickOptions>, SelectorOptions {}
export interface UserEventHoverOptions extends MoveToOptions, SelectorOptions {}
export interface UserEventDragAndDropOptions extends DragAndDropOptions {
sourceX?: number
sourceY?: number
targetX?: number
targetY?: number
}
export interface UserEventFillOptions extends SelectorOptions {}
export interface UserEventSelectOptions extends SelectorOptions {}
export interface UserEventClearOptions extends SelectorOptions {}
export interface UserEventDoubleClickOptions extends SelectorOptions {}
export interface UserEventTripleClickOptions extends SelectorOptions {}
export interface UserEventWheelBaseOptions extends SelectorOptions {}
export interface LocatorScreenshotOptions extends SelectorOptions {}
}
interface WebdriverCDPSession {
send: (method: string, params?: Record<string, unknown>) => Promise<unknown>
on: (event: string, listener: (...args: unknown[]) => void) => void
once: (event: string, listener: (...args: unknown[]) => void) => void
off: (event: string, listener: (...args: unknown[]) => void) => void
}
declare module 'vitest/node' {
export interface BrowserCommandContext {
browser: WebdriverIO.Browser
}
export interface _BrowserNames {
webdriverio: WebdriverBrowser
}
export interface ToMatchScreenshotOptions
extends Omit<
ScreenshotMatcherOptions,
'comparatorName' | 'comparatorOptions'
>, CustomComparatorsRegistry {}
export interface ToMatchScreenshotComparators
extends ScreenshotComparatorRegistry {}
export interface CDPSession extends WebdriverCDPSession {}
}