Skip to content

Commit

Permalink
More typings improvements (webdriverio#3335)
Browse files Browse the repository at this point in the history
## Proposed changes

[//]: # (Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue.)

- This moves the typings for the hooks to webdriverio typings instead of webdriver.
- Exposes a Config type so that you can import a single object to get a properly typed wdio config with optional properties. This makes it easy to overwrite them.
- Adds an exception for the browser.call typings generation because the return type should not be undefined. 

## Types of changes

[//]: # (What types of changes does your code introduce to WebdriverIO?)
[//]: # (_Put an `x` in the boxes that apply_)

- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

## Checklist

[//]: # (_Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._)

- [x] I have read the [CONTRIBUTING](https://github.com/webdriverio/webdriverio/blob/master/CONTRIBUTING.md) doc
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] I have added necessary documentation (if appropriate)

## Further comments

[//]: # (If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...)

### Reviewers: @webdriverio/technical-committee
  • Loading branch information
ablok authored and Selvaraj, Mohan committed Feb 19, 2019
1 parent 1ff4e9f commit 923d172
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 88 deletions.
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

0 comments on commit 923d172

Please sign in to comment.