Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type-safe overwriteCommand #885

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/browser/commands/moveCursorTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type MoveToOptions = {
};

// As of now, we can't export type of the command function directly. See: https://github.com/webdriverio/webdriverio/issues/12527
export type MoveCursorToCommand = (options: MoveToOptions) => Promise<void>;
export type MoveCursorToCommand = (this: WebdriverIO.Element, options: MoveToOptions) => Promise<void>;

const makeMoveCursorToCommand = (session: WebdriverIO.Browser) =>
async function (this: WebdriverIO.Element, { xOffset = 0, yOffset = 0 }: MoveToOptions = {}): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions src/browser/commands/openAndWait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const is: Record<string, (match: Matches) => boolean> = {

const makeOpenAndWaitCommand = (config: BrowserConfig, session: WebdriverIO.Browser) =>
function openAndWait(
this: WebdriverIO.Browser,
uri: string,
{
selector = [],
Expand Down
13 changes: 11 additions & 2 deletions src/browser/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,14 @@ export interface AssertViewOpts extends Partial<AssertViewOptsConfig> {
ignoreDiffPixelCount?: `${number}%` | number;
}

export type AssertViewCommand = (state: string, selectors: string | string[], opts?: AssertViewOpts) => Promise<void>;
export type AssertViewElementCommand = (state: string, opts?: AssertViewOpts) => Promise<void>;
export type AssertViewCommand = (
this: WebdriverIO.Browser,
state: string,
selectors: string | string[],
opts?: AssertViewOpts,
) => Promise<void>;
export type AssertViewElementCommand = (
this: WebdriverIO.Element,
state: string,
opts?: AssertViewOpts,
) => Promise<void>;
44 changes: 26 additions & 18 deletions src/browser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,37 @@ export interface Browser {
applyState: (state: Record<string, unknown>) => void;
}

type FunctionProperties<T> = keyof T &
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
}[keyof T];

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace WebdriverIO {
type OverwriteCommandFn<IsElement extends boolean = false> = (
this: IsElement extends true ? WebdriverIO.Element : WebdriverIO.Browser,
origCommand: (...args: any[]) => Promise<any>, // eslint-disable-line @typescript-eslint/no-explicit-any
...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
) => Promise<any>; // eslint-disable-line @typescript-eslint/no-explicit-any

interface Browser {
getMeta(): Promise<BrowserMeta>;
getMeta(key: string): Promise<unknown>;
getMeta(this: WebdriverIO.Browser): Promise<BrowserMeta>;
getMeta(this: WebdriverIO.Browser, key: string): Promise<unknown>;

setMeta(this: WebdriverIO.Browser, key: string, value: unknown): Promise<void>;

setMeta(key: string, value: unknown): Promise<void>;
extendOptions(this: WebdriverIO.Browser, opts: { [name: string]: unknown }): Promise<void>;

extendOptions(opts: { [name: string]: unknown }): Promise<void>;
getConfig(this: WebdriverIO.Browser): Promise<BrowserConfig>;

getConfig(): Promise<BrowserConfig>;
overwriteCommand<CommandName extends FunctionProperties<WebdriverIO.Browser>>(
name: CommandName,
func: WebdriverIO.Browser[CommandName],
attachToElement?: false,
proto?: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any
instances?: Record<string, WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser>,
): void;

overwriteCommand<IsElement extends boolean = false>(
name: string,
func: OverwriteCommandFn<IsElement>,
attachToElement?: IsElement,
overwriteCommand<CommandName extends FunctionProperties<WebdriverIO.Element>>(
name: CommandName,
func: WebdriverIO.Element[CommandName],
attachToElement: true,
proto?: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any
instances?: Record<string, WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser>,
): void;
Expand Down Expand Up @@ -96,7 +104,7 @@ declare global {
* @param stepCb step callback
* @returns {Promise<T>} value, returned by `stepCb`
*/
runStep<T = unknown>(stepName: string, stepCb: () => Promise<T> | T): Promise<T>;
runStep<T = unknown>(this: WebdriverIO.Browser, stepName: string, stepCb: () => Promise<T> | T): Promise<T>;

// TODO: describe executionContext more precisely
executionContext: (RunnerTest | RunnerHook) & {
Expand All @@ -111,9 +119,9 @@ declare global {

openAndWait: OpenAndWaitCommand;

switchToRepl: (ctx?: Record<string, unknown>) => Promise<void>;
switchToRepl: (this: WebdriverIO.Browser, ctx?: Record<string, unknown>) => Promise<void>;

clearSession: () => Promise<void>;
clearSession: (this: WebdriverIO.Browser) => Promise<void>;
}

interface Element {
Expand Down
Loading