Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions packages/wdio-types/src/Options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import type http from 'node:http'
import type https from 'node:https'
import type { URL } from 'node:url'

import type { Hooks, ServiceEntry } from './Services.js'
import type { ReporterEntry } from './Reporters.js'

export type WebDriverLogTypes = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent'
export type SupportedProtocols = 'webdriver' | 'devtools' | './protocol-stub.js'
export type Agents = { http?: unknown, https?: unknown }

export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE' | 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace'

export interface RequestLibOptions {
agent?: Agents
followRedirect?: boolean
headers?: Record<string, string | string[] | undefined>
https?: Record<string, unknown>
json?: Record<string, unknown>
method?: Method
responseType?: 'json' | 'buffer' | 'text'
retry?: { limit: number, methods?: Method[], calculateDelay?: (retryOptions: { computedValue: number }) => number }
searchParams?: Record<string, string | number | boolean | null | undefined> | URLSearchParams
throwHttpErrors?: boolean
timeout?: { response: number }
url?: URL
path?: string
username?: string
password?: string
body?: unknown
}

export interface RequestLibResponse<Body = unknown> {
statusCode: number
body?: Body
Expand Down Expand Up @@ -104,14 +128,29 @@ export interface WebDriver extends Connection {
headers?: {
[name: string]: string
}
/**
* Allows you to use a custom http/https/http2 [agent](https://www.npmjs.com/package/got#agent) to make requests.
*
* @default
* ```js
* {
* http: new http.Agent({ keepAlive: true }),
* https: new https.Agent({ keepAlive: true })
* }
* ```
*/
agent?: {
http: http.Agent,
https: https.Agent
}
/**
* Function intercepting [HTTP request options](https://github.com/sindresorhus/got#options) before a WebDriver request is made.
*/
transformRequest?: (requestOptions: RequestInit) => RequestInit
transformRequest?: (requestOptions: RequestLibOptions) => RequestLibOptions
/**
* Function intercepting HTTP response objects after a WebDriver response has arrived.
*/
transformResponse?: (response: RequestLibResponse, requestOptions: RequestInit) => RequestLibResponse
transformResponse?: (response: RequestLibResponse, requestOptions: RequestLibOptions) => RequestLibResponse

/**
* Appium direct connect options (see: https://appiumpro.com/editions/86-connecting-directly-to-appium-hosts-in-distributed-environments)
Expand Down
6 changes: 4 additions & 2 deletions packages/webdriver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@
"url": "https://github.com/webdriverio/webdriverio/issues"
},
"dependencies": {
"@testplane/wdio-config": "workspace:*",
"@testplane/wdio-logger": "workspace:*",
"@testplane/wdio-protocols": "workspace:*",
"@testplane/wdio-types": "workspace:*",
"@testplane/wdio-utils": "workspace:*",
"@types/node": "^20.1.0",
"@types/ws": "^8.5.3",
"@testplane/wdio-config": "workspace:*",
"@testplane/wdio-logger": "workspace:*",
"deepmerge-ts": "^7.0.3",
"got": "12.6.1",
"ky": "0.33.0",
"undici": "6.12.0",
"ws": "^8.8.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webdriver/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { WebSocket } from 'ws'

import WebDriver from './index.js'
import { BrowserSocket } from './bidi/socket.js'
import { FetchRequest } from './request/web.js'
import { WebRequest } from './request/web.js'

export default WebDriver
export * from './index.js'
Expand All @@ -13,7 +13,7 @@ import { environment } from './environment.js'
const log = logger('webdriver')

environment.value = {
Request: FetchRequest,
Request: WebRequest,
Socket: BrowserSocket,
createBidiConnection: (webSocketUrl: string, options: unknown) => {
log.info(`Connecting to webSocketUrl ${webSocketUrl}`)
Expand Down
11 changes: 4 additions & 7 deletions packages/webdriver/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,17 @@ export default function (
* - `deleteSession` calls which should go through in case we retry the command.
* - requests that don't require a session.
*/
const { isAborted, abortSignal, cleanup } = manageSessionAbortions.call(this)
const { isAborted, cleanup } = manageSessionAbortions.call(this)
const requiresSession = endpointUri.includes('/:sessionId/')
if (isAborted && command !== 'deleteSession' && requiresSession) {
throw new Error(`Trying to run command "${commandCallStructure(command, args)}" after session has been deleted, aborting request without executing it`)
}

const request = new environment.value.Request(method, endpoint, body, abortSignal, isHubCommand, {
onPerformance: (data) => this.emit('request.performance', data),
onRequest: (data) => this.emit('request.start', data),
onResponse: (data) => this.emit('request.end', data),
onRetry: (data) => this.emit('request.retry', data)
})
const request = new environment.value.Request(method, endpoint, body, isHubCommand)
request.on('performance', (...args) => this.emit('request.performance', ...args))
this.emit('command', { command, method, endpoint, body })
log.info('COMMAND', commandCallStructure(command, args))

/**
* use then here so we can better unit test what happens before and after the request
*/
Expand Down
17 changes: 14 additions & 3 deletions packages/webdriver/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Options } from '@testplane/wdio-types'

import { environment } from './environment.js'

import type { Options } from '@testplane/wdio-types'
import type { RemoteConfig } from './types.js'

export const DEFAULTS: Options.Definition<Required<RemoteConfig>> = {
Expand Down Expand Up @@ -92,6 +92,12 @@ export const DEFAULTS: Options.Definition<Required<RemoteConfig>> = {
type: 'number',
default: 3
},
/**
* Override default agent
*/
agent: {
type: 'object'
},
/**
* Override default agent
*/
Expand All @@ -109,7 +115,7 @@ export const DEFAULTS: Options.Definition<Required<RemoteConfig>> = {
*/
transformRequest: {
type: 'function',
default: (requestOptions: RequestInit) => requestOptions
default: (requestOptions: Options.RequestLibOptions) => requestOptions
},
/**
* Function transforming the response object after it is received
Expand Down Expand Up @@ -144,5 +150,10 @@ export const DEFAULTS: Options.Definition<Required<RemoteConfig>> = {
}
}

export const REG_EXPS = {
commandName: /.*\/session\/[0-9a-f-]+\/(.*)/,
execFn: /return \(([\s\S]*)\)\.apply\(null, arguments\)/
}

export const ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf'
export const SHADOW_ELEMENT_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
6 changes: 3 additions & 3 deletions packages/webdriver/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type WebSocket from 'ws'

import type { BrowserSocket } from './bidi/socket.js'
import type { FetchRequest } from './request/web.js'

import type { WebRequest } from './request/web.js'
import type { NodeJSRequest } from './request/node.js'
/**
* @internal
*/
Expand All @@ -14,7 +14,7 @@ export interface EnvironmentVariables {
}

export interface EnvironmentDependencies {
Request: typeof FetchRequest,
Request: typeof WebRequest | typeof NodeJSRequest,
Socket: typeof BrowserSocket,
createBidiConnection: (wsUrl?: string, options?: unknown) => Promise<WebSocket | undefined>,
variables: EnvironmentVariables
Expand Down
9 changes: 4 additions & 5 deletions packages/webdriver/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import os from 'node:os'
import ws from 'ws'

import WebDriver from './index.js'
import { FetchRequest } from './request/node.js'
import { FetchRequest as WebFetchRequest } from './request/web.js'
import { NodeJSRequest } from './request/node.js'
import { WebRequest } from './request/web.js'
import { createBidiConnection } from './node/bidi.js'
import type { BrowserSocket } from './bidi/socket.js'

Expand All @@ -22,16 +22,15 @@ environment.value = {
*/
process.env.WDIO_USE_NATIVE_FETCH ||
/**
* For unit tests we use the WebFetchRequest implementation as we can better mock the
* For unit tests we use the WebRequest implementation as we can better mock the
* requests in the unit tests.
*/
process.env.WDIO_UNIT_TESTS
) ? WebFetchRequest : FetchRequest,
) ? WebRequest : NodeJSRequest,
Socket: ws as unknown as typeof BrowserSocket,
createBidiConnection,
variables: {
WEBDRIVER_CACHE_DIR: process.env.WEBDRIVER_CACHE_DIR || os.tmpdir(),
PROXY_URL: process.env.HTTP_PROXY || process.env.HTTPS_PROXY
}
}

18 changes: 0 additions & 18 deletions packages/webdriver/src/request/constants.ts

This file was deleted.

148 changes: 0 additions & 148 deletions packages/webdriver/src/request/error.ts

This file was deleted.

Loading
Loading