Skip to content

Commit

Permalink
add type support for browser properties
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed May 4, 2020
1 parent add1948 commit ef05501
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/wdio-sync/webdriverio-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ declare namespace WebdriverIO {
script?: number
}

interface Browser {
interface Browser extends WebDriver.BaseClient {
config: Config;
options: RemoteOptions;

Expand Down
34 changes: 27 additions & 7 deletions packages/webdriver/webdriver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ declare namespace WebDriver {
* Defines the [capabilities](https://w3c.github.io/webdriver/webdriver-spec.html#capabilities) you want to run in your Selenium session.
*/
capabilities?: DesiredCapabilities;
requestedCapabilities?: DesiredCapabilities;
/**
* Level of logging verbosity.
*/
Expand Down Expand Up @@ -610,9 +611,28 @@ declare namespace WebDriver {
mjpegScalingFactor?: number,
}

interface BaseClient {
// id of WebDriver session
sessionId: string;
// assigned capabilities by the browser driver / WebDriver server
capabilities: DesiredCapabilities;
// original requested capabilities
requestedCapabilities: DesiredCapabilities;

/**
* browser flags
*/
// true if session runs on a mobile device
isMobile: boolean;
// true if mobile session runs on iOS
isIOS: boolean;
// true if mobile session runs on Android
isAndroid: boolean;
}

// generated typings
// webdriver types
interface Client {
interface Client extends BaseClient {

/**
* [webdriver]
Expand Down Expand Up @@ -1008,7 +1028,7 @@ declare namespace WebDriver {
}

// appium types
interface Client {
interface Client extends BaseClient {

/**
* [appium]
Expand Down Expand Up @@ -1446,7 +1466,7 @@ declare namespace WebDriver {
}

// jsonwp types
interface Client {
interface Client extends BaseClient {

/**
* [jsonwp]
Expand Down Expand Up @@ -2115,7 +2135,7 @@ declare namespace WebDriver {
}

// mjsonwp types
interface Client {
interface Client extends BaseClient {

/**
* [mjsonwp]
Expand Down Expand Up @@ -2182,7 +2202,7 @@ declare namespace WebDriver {
}

// chromium types
interface Client {
interface Client extends BaseClient {

/**
* [chromium]
Expand Down Expand Up @@ -2354,7 +2374,7 @@ declare namespace WebDriver {
}

// saucelabs types
interface Client {
interface Client extends BaseClient {

/**
* [saucelabs]
Expand Down Expand Up @@ -2400,7 +2420,7 @@ declare namespace WebDriver {
}

// selenium types
interface Client {
interface Client extends BaseClient {

/**
* [selenium]
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverio/webdriverio-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ declare namespace WebdriverIO {
script?: number
}

interface Browser {
interface Browser extends WebDriver.BaseClient {
config: Config;
options: RemoteOptions;

Expand Down
20 changes: 20 additions & 0 deletions scripts/templates/webdriver.tpl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ declare namespace WebDriver {
* Defines the [capabilities](https://w3c.github.io/webdriver/webdriver-spec.html#capabilities) you want to run in your Selenium session.
*/
capabilities?: DesiredCapabilities;
requestedCapabilities?: DesiredCapabilities;
/**
* Level of logging verbosity.
*/
Expand Down Expand Up @@ -604,6 +605,25 @@ declare namespace WebDriver {
mjpegScalingFactor?: number,
}

interface BaseClient {
// id of WebDriver session
sessionId: string;
// assigned capabilities by the browser driver / WebDriver server
capabilities: DesiredCapabilities;
// original requested capabilities
requestedCapabilities: DesiredCapabilities;

/**
* browser flags
*/
// true if session runs on a mobile device
isMobile: boolean;
// true if mobile session runs on iOS
isIOS: boolean;
// true if mobile session runs on Android
isAndroid: boolean;
}

// generated typings
// ... insert here ...

Expand Down
2 changes: 1 addition & 1 deletion scripts/templates/webdriverio.tpl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ declare namespace WebdriverIO {
script?: number
}

interface Browser {
interface Browser extends WebDriver.BaseClient {
config: Config;
options: RemoteOptions;

Expand Down
2 changes: 1 addition & 1 deletion scripts/type-generation/webdriver-generate-typings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ${INDENTATION} */`
const lines = []
for (const [protocolName, definition] of Object.entries(PROTOCOLS)) {
lines.push(` // ${protocolName} types`)
lines.push(' interface Client {')
lines.push(' interface Client extends BaseClient {')

for (const [, methods] of Object.entries(definition)) {
for (const [, description] of Object.entries(methods)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/typings/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ browser.addLocatorStrategy('myStrat', () => {})
browser.sharedStore.get('foo')
browser.sharedStore.set('foo', ['q', 1, true, null, {'w' : {}, 'e': [] }, [{}]])

// test access to base client properties
browser.sessionId
browser.capabilities.browserName
browser.requestedCapabilities.browserName
browser.isMobile
browser.isAndroid
browser.isIOS

// allure-reporter
allure.addFeature('')

Expand Down
8 changes: 8 additions & 0 deletions tests/typings/webdriverio/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ async function bar() {

// addLocatorStrategy
browser.addLocatorStrategy('myStrat', () => {})

// test access to base client properties
browser.sessionId
browser.capabilities.browserName
browser.requestedCapabilities.browserName
browser.isMobile
browser.isAndroid
browser.isIOS
}

// allure-reporter
Expand Down

0 comments on commit ef05501

Please sign in to comment.