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

More typings improvements #3335

Merged
merged 1 commit into from Jan 15, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/webdriverio/tests/commands/browser/call.test.js
Expand Up @@ -12,10 +12,11 @@ describe('call command', () => {
})
})

it('should call a fn', () => {
const callFn = jest.fn()
browser.call(callFn)
it('should call a fn and return result', () => {
const callFn = jest.fn(() => true)
const result = browser.call(callFn)
expect(callFn).toBeCalled()
expect(result).toEqual(true)
})

it('should not fail if nothing is applied', () => {
Expand Down
88 changes: 5 additions & 83 deletions scripts/templates/webdriver.tpl.d.ts
Expand Up @@ -208,98 +208,20 @@ declare namespace WebDriver {
hostname?: string;
port?: number;
path?: string;
queryParams: {
queryParams?: {
[name: string]: string;
},
capabilities: DesiredCapabilities;
logLevel: WebDriverLogTypes;
logOutput: string | NodeJS.WritableStream
capabilities?: DesiredCapabilities;
logLevel?: WebDriverLogTypes;
logOutput?: string | NodeJS.WritableStream
connectionRetryTimeout?: number;
connectionRetryCount?: number;
user?: string;
key?: string;
}

interface Hooks {

onPrepare?(
config: Options,
capabilities: DesiredCapabilities
): void;

onComplete?(exitCode: number): void;

onReload?(oldSessionId: string, newSessionId: string): void;

before?(
capabilities: DesiredCapabilities,
specs: string[]
): void;

beforeCommand?(
commandName: string,
args: any[]
): void;

beforeHook?(): void;

beforeSession?(
config: Options,
capabilities: DesiredCapabilities,
specs: string[]
): void;

beforeSuite?(suite: Suite): void;
beforeTest?(test: Test): void;
afterHook?(): void;

after?(
result: number,
capabilities: DesiredCapabilities,
specs: string[]
): void;

afterCommand?(
commandName: string,
args: any[],
result: any,
error?: Error
): void;

afterSession?(
config: Options,
capabilities: DesiredCapabilities,
specs: string[]
): void;

afterSuite?(suite: Suite): void;
afterTest?(test: Test): void;

// cucumber specific hooks
beforeFeature?(feature: string): void;
beforeScenario?(scenario: string): void;
beforeStep?(step: string): void;
afterFeature?(feature: string): void;
afterScenario?(scenario: any): void;
afterStep?(stepResult: any): void;
}

interface Suite {
file: string;
parent: string;
pending: boolean;
title: string;
type: string;
}

interface Test extends Suite {
currentTest: string;
passed: boolean;
duration: any;
}

function newSession(
options?: Options & Hooks,
options?: Options,
modifier?: (...args: any[]) => any,
proto?: object,
commandWrapper?: (commandName: string, fn: (...args: any[]) => any) => any
Expand Down
84 changes: 83 additions & 1 deletion scripts/templates/webdriverio.tpl.d.ts
Expand Up @@ -57,7 +57,7 @@ declare namespace WebdriverIO {
specs?: string[],
exclude?: string[],
suites?: object,
capabilities: WebDriver.DesiredCapabilities|WebDriver.DesiredCapabilities[],
capabilities?: WebDriver.DesiredCapabilities|WebDriver.DesiredCapabilities[],
outputDir?: string,
baseUrl?: string,
bail?: number,
Expand All @@ -71,12 +71,91 @@ declare namespace WebdriverIO {
execArgv?: string[]
}

interface Hooks {

onPrepare?(
config: Options,
capabilities: WebDriver.DesiredCapabilities
): void;

onComplete?(exitCode: number): void;

onReload?(oldSessionId: string, newSessionId: string): void;

before?(
capabilities: WebDriver.DesiredCapabilities,
specs: string[]
): void;

beforeCommand?(
commandName: string,
args: any[]
): void;

beforeHook?(): void;

beforeSession?(
config: Options,
capabilities: WebDriver.DesiredCapabilities,
specs: string[]
): void;

beforeSuite?(suite: Suite): void;
beforeTest?(test: Test): void;
afterHook?(): void;

after?(
result: number,
capabilities: WebDriver.DesiredCapabilities,
specs: string[]
): void;

afterCommand?(
commandName: string,
args: any[],
result: any,
error?: Error
): void;

afterSession?(
config: Options,
capabilities: WebDriver.DesiredCapabilities,
specs: string[]
): void;

afterSuite?(suite: Suite): void;
afterTest?(test: Test): void;

// cucumber specific hooks
beforeFeature?(feature: string): void;
beforeScenario?(scenario: string): void;
beforeStep?(step: string): void;
afterFeature?(feature: string): void;
afterScenario?(scenario: any): void;
afterStep?(stepResult: any): void;
}

interface Suite {
file: string;
parent: string;
pending: boolean;
title: string;
type: string;
}

interface Test extends Suite {
currentTest: string;
passed: boolean;
duration: any;
}

interface Element<T> {
// ... element commands ...
}

type Execute = <T>(script: string | ((...arguments: any[]) => T), ...arguments: any[]) => T;
type ExecuteAsync = (script: string | ((...arguments: any[]) => any), ...arguments: any[]) => any;
type Call = <T>(callback: Function) => T;

interface Browser<T> {
addCommand(
Expand All @@ -85,6 +164,7 @@ declare namespace WebdriverIO {
): any;
execute: Execute;
executeAsync: ExecuteAsync;
call: Call;
options: Options;
waitUntil(
condition: Function,
Expand All @@ -94,6 +174,8 @@ declare namespace WebdriverIO {
): undefined
// ... browser commands ...
}

type Config = WebDriver.Options & Options & Hooks;
}

declare var browser: WebDriver.Client<void> & WebdriverIO.Browser<void>;
Expand Down
2 changes: 1 addition & 1 deletion scripts/type-generation/webdriverio-generate-typings.js
Expand Up @@ -95,7 +95,7 @@ const gatherCommands = (commandPath, commandFile) => {
}
}

if (!['execute', 'executeAsync', 'waitUntil'].includes(commandName)) {
if (!['execute', 'executeAsync', 'waitUntil', 'call'].includes(commandName)) {
allTypeLines.push(`${commandName}(${allParameters.length > 0 ? '\n ' : ''}${allParameters.join(',\n ')}${allParameters.length > 0 ? '\n ' : ''}): ${returnType}`)
}
}
Expand Down