Skip to content
Open
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
9 changes: 9 additions & 0 deletions packages/wdio-types/src/Capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ export interface VendorExtensions extends EdgeCapabilities, AppiumCapabilities,
* Selenium 4.0 Specific
*/
'se:cdp'?: string

/**
* Selenoid custom
*/
'se:wsdriver'?: string
/**
* Selenoid custom
*/
'se:wsdriverVersion'?: string
}

export type AppiumOptions = RemoveAppiumPrefix<AppiumCapabilities & AppiumXCUITestCapabilities & AppiumAndroidCapabilities>
Expand Down
29 changes: 26 additions & 3 deletions packages/wdio-types/src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,28 @@ export interface RequestLibOptions {
}

export interface RequestLibResponse<Body = unknown> {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually had all theses properties, but typings were incorrect

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually use those properties? I think it's a good thing to expose minimal amount of properties. The narrower API surface is, the better.

For example, can we produce such response objects with our custom wsd agent?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually use those properties?

They are on "transformRequest" and "transformResponse" of browser's config

If you tried to use them and do anything usefull, you can't, because types are missing

can we produce such response objects with our custom wsd agent?

Yes

statusCode: number
body?: Body
rawBody?: Buffer
url: string;
method: string;
statusCode: number;
statusMessage: string;
req: {
method: string;
path: string;
host: string;
res: RequestLibResponse<Body>;
},
request: {
options: RequestLibOptions;
requestUrl: URL;
response: RequestLibResponse<Body>;
};
ok: boolean;
body?: Body;
rawBody?: Buffer;
}

export interface CustomWdRequestAgent {
request: (url: globalThis.URL, options: Omit<RequestLibOptions, 'url' | 'retry'>) => Promise<RequestLibResponse>
}

export interface ShardOptions {
Expand Down Expand Up @@ -180,6 +199,10 @@ export interface WebDriver extends Connection {
* when attempting to start a session.
*/
cacheDir?: string
/**
* Custom webdriver request agent
*/
customWdRequestAgent?: CustomWdRequestAgent
}

export type SauceRegions = 'us' | 'eu' | 'us-west-1' | 'us-east-4' | 'eu-central-1' | 'staging'
Expand Down
6 changes: 6 additions & 0 deletions packages/webdriver/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export const DEFAULTS: Options.Definition<Required<RemoteConfig>> = {
cacheDir: {
type: 'string',
default: environment.value.variables.WEBDRIVER_CACHE_DIR
},
/**
* Custom webdriver request agent
*/
customWdRequestAgent: {
type: 'object',
}
}

Expand Down
10 changes: 6 additions & 4 deletions packages/webdriver/src/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default abstract class WebDriverRequest extends EventEmitter {
}

this.emit('request', fullRequestOptions)
return this._request(fullRequestOptions, options.transformResponse, options.connectionRetryCount, 0)
return this._request(fullRequestOptions, options.transformResponse, options.customWdRequestAgent, options.connectionRetryCount, 0)
}

protected async _createOptions (options: RequestOptions, sessionId?: string, isBrowser: boolean = false): Promise<RequestLibOptions> {
Expand Down Expand Up @@ -167,6 +167,7 @@ export default abstract class WebDriverRequest extends EventEmitter {
private async _request (
fullRequestOptions: RequestLibOptions,
transformResponse?: (response: RequestLibResponse, requestOptions: RequestLibOptions) => RequestLibResponse,
customWdRequestAgent: Options.CustomWdRequestAgent | null = null,
totalRetryCount = 0,
retryCount = 0
): Promise<WebDriverResponse> {
Expand All @@ -178,8 +179,9 @@ export default abstract class WebDriverRequest extends EventEmitter {

const { url, retry: _, ...requestLibOptions } = fullRequestOptions
const startTime = this._libPerformanceNow()
let response = await this._libRequest(url!, requestLibOptions)
.catch((err: RequestLibError) => err)
let response = customWdRequestAgent
? await customWdRequestAgent.request(url!, requestLibOptions).catch((err: RequestLibError) => err)
: await this._libRequest(url!, requestLibOptions).catch((err: RequestLibError) => err)
Comment on lines +182 to +184
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If non-null "customWdRequestAgent" was passed, uses it to make request.
"customWdRequestAgent" should have the same API as _libRequest

const durationMillisecond = this._libPerformanceNow() - startTime

/**
Expand All @@ -204,7 +206,7 @@ export default abstract class WebDriverRequest extends EventEmitter {
this.emit('performance', { request: fullRequestOptions, durationMillisecond, success: false, error, retryCount })
log.warn(msg)
log.info(`Retrying ${retryCount}/${totalRetryCount}`)
return this._request(fullRequestOptions, transformResponse, totalRetryCount, retryCount)
return this._request(fullRequestOptions, transformResponse, customWdRequestAgent, totalRetryCount, retryCount)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/webdriver/src/request/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import https from 'node:https'
import { performance } from 'node:perf_hooks'
import type { URL } from 'node:url'

import got, { type OptionsOfTextResponseBody } from 'got'
import got, { type OptionsOfJSONResponseBody } from 'got'
import type { Options } from '@testplane/wdio-types'

import WebDriverRequest, { RequestLibError } from './index.js'
Expand All @@ -26,7 +26,7 @@ export class NodeJSRequest extends WebDriverRequest {

protected async _libRequest (url: URL, opts: Options.RequestLibOptions) {
try {
return (await got(url, opts as OptionsOfTextResponseBody)) as Options.RequestLibResponse
return (await got(url, opts as OptionsOfJSONResponseBody)) as unknown as Options.RequestLibResponse
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
if (!(err instanceof Error)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/webdriver/src/request/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Options as KyOptions } from 'ky'
import ky from 'ky'
import logger from '@testplane/wdio-logger'
import WebDriverRequest from './index.js'
import type { RequestOptions, RequestLibOptions } from './types.js'
import type { RequestOptions, RequestLibOptions, RequestLibResponse } from './types.js'

const log = logger('webdriver')

Expand Down Expand Up @@ -49,7 +49,7 @@ export class WebRequest extends WebDriverRequest {
return {
statusCode: res.status,
body: await res.json(),
}
} as RequestLibResponse
}

protected _libPerformanceNow(): number {
Expand Down
2 changes: 2 additions & 0 deletions packages/webdriver/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ export interface AttachOptions extends Partial<SessionFlags>, Partial<Options.We
capabilities?: WebdriverIO.Capabilities
// original requested capabilities
requestedCapabilities?: Capabilities.WithRequestedCapabilities['capabilities']
// custom agent to make webdriver requests
customWdRequestAgent?: Options.CustomWdRequestAgent
}
1 change: 0 additions & 1 deletion packages/webdriver/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ export class CustomRequestError extends Error {
constructor (body: WebDriverResponse, requestOptions: any) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorObj = (body.value || body) as any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let errorMessage = errorObj.message || errorObj.class || 'unknown error'

/**
Expand Down
Loading