Skip to content

Commit 63bd0c6

Browse files
Rewrite webdriverio element commands (#6139)
Co-authored-by: Mykola Grybyk <25589559+mgrybyk@users.noreply.github.com>
1 parent c251860 commit 63bd0c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+506
-215
lines changed

packages/webdriverio/src/commands/browser/$$.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@
4343
import { findElements, enhanceElementsArray } from '../../utils'
4444
import { getElements } from '../../utils/getElementObject'
4545

46-
export default async function $$ (this: WebdriverIO.BrowserObject, selector: string) {
46+
export default async function $$ (
47+
this: WebdriverIO.BrowserObject,
48+
selector: string
49+
) {
4750
const res = await findElements.call(this, selector)
4851
const elements = await getElements.call(this, selector, res)
49-
return enhanceElementsArray(elements, this, selector)
52+
return enhanceElementsArray(elements, this, selector) as WebdriverIO.Element[]
5053
}

packages/webdriverio/src/commands/browser/$.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ import { getElement } from '../../utils/getElementObject'
5252
import { ELEMENT_KEY } from '../../constants'
5353
import type { ElementReference } from '../../types'
5454

55-
export default async function $ (this: WebdriverIO.BrowserObject, selector: string | ElementReference) {
55+
export default async function $ (
56+
this: WebdriverIO.BrowserObject,
57+
selector: string | ElementReference
58+
) {
59+
const elemReference = selector as ElementReference
60+
5661
/**
5762
* convert protocol result into WebdriverIO element
5863
* e.g. when element was fetched with `getActiveElement`
5964
*/
60-
if (typeof selector !== 'string' && typeof (selector as ElementReference)[ELEMENT_KEY] === 'string') {
61-
return getElement.call(this, undefined, selector as ElementReference)
65+
if (typeof selector !== 'string' && typeof elemReference[ELEMENT_KEY] === 'string') {
66+
return getElement.call(this, undefined, elemReference)
6267
}
6368

6469
const res = await findElement.call(this, selector)

packages/webdriverio/src/commands/browser/custom$$.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ import { getElements } from '../../utils/getElementObject'
2626
import { ELEMENT_KEY } from '../../constants'
2727
import type { ElementReference } from '../../types'
2828

29-
async function custom$$ (this: WebdriverIO.BrowserObject, strategyName: string, ...strategyArguments: any[]) {
29+
export default async function custom$$ (
30+
this: WebdriverIO.BrowserObject,
31+
strategyName: string,
32+
...strategyArguments: any[]
33+
) {
3034
const strategy = this.strategies.get(strategyName)
3135

3236
if (!strategy) {
3337
throw Error('No strategy found for ' + strategyName)
3438
}
3539

36-
let res: ElementReference | ElementReference[] = await this.execute(strategy, ...strategyArguments)
40+
let res: ElementReference | ElementReference[] = await this.execute(
41+
strategy,
42+
...strategyArguments
43+
)
3744

3845
/**
3946
* if the user's script return just one element
@@ -49,5 +56,3 @@ async function custom$$ (this: WebdriverIO.BrowserObject, strategyName: string,
4956
const elements = res.length ? await getElements.call(this, strategy.toString(), res) : []
5057
return enhanceElementsArray(elements, this, strategyName, 'custom$$', [strategyArguments])
5158
}
52-
53-
export default custom$$

packages/webdriverio/src/commands/browser/custom$.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ import { getElement } from '../../utils/getElementObject'
2525
import { ELEMENT_KEY } from '../../constants'
2626
import type { ElementReference } from '../../types'
2727

28-
async function custom$ (this: WebdriverIO.BrowserObject, strategyName: string, ...strategyArguments: any[]) {
28+
export default async function custom$ (
29+
this: WebdriverIO.BrowserObject,
30+
strategyName: string,
31+
...strategyArguments: any[]
32+
) {
2933
const strategy = this.strategies.get(strategyName)
3034

3135
if (!strategy) {
3236
throw Error('No strategy found for ' + strategyName)
3337
}
3438

35-
let res: ElementReference | ElementReference[] = await this.execute(strategy, ...strategyArguments)
39+
let res: ElementReference | ElementReference[] = await this.execute(
40+
strategy,
41+
...strategyArguments
42+
)
3643

3744
/**
3845
* if the user's script returns multiple elements
@@ -49,5 +56,3 @@ async function custom$ (this: WebdriverIO.BrowserObject, strategyName: string, .
4956

5057
throw Error('Your locator strategy script must return an element')
5158
}
52-
53-
export default custom$

packages/webdriverio/src/commands/browser/debug.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
import { serializeError } from 'serialize-error'
3333
import WDIORepl from '@wdio/repl'
3434

35-
export default function debug(this: WebdriverIO.BrowserObject, commandTimeout = 5000) {
35+
export default function debug(
36+
this: WebdriverIO.BrowserObject,
37+
commandTimeout = 5000
38+
) {
3639
const repl = new WDIORepl()
3740
const { introMessage } = WDIORepl
3841

packages/webdriverio/src/commands/browser/deleteCookies.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
*
4343
*/
4444

45-
export default function deleteCookies(this: WebdriverIO.BrowserObject, names?: string | string[]) {
45+
export default function deleteCookies(
46+
this: WebdriverIO.BrowserObject,
47+
names?: string | string[]
48+
) {
4649
if (names === undefined) {
4750
return this.deleteAllCookies()
4851
}

packages/webdriverio/src/commands/browser/execute.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ import { verifyArgsAndStripIfElement } from '../../utils'
3535
*
3636
*/
3737

38-
export default function execute (this: WebdriverIO.BrowserObject, script: string | Function, ...args: any[]) {
39-
/*!
38+
export default function execute (
39+
this: WebdriverIO.BrowserObject,
40+
script: string | Function,
41+
...args: any[]
42+
) {
43+
/**
4044
* parameter check
4145
*/
4246
if ((typeof script !== 'string' && typeof script !== 'function')) {
4347
throw new Error('number or type of arguments don\'t agree with execute protocol command')
4448
}
4549

46-
/*!
50+
/**
4751
* instances started as multibrowserinstance can't getting called with
4852
* a function parameter, therefor we need to check if it starts with "function () {"
4953
*/

packages/webdriverio/src/commands/browser/executeAsync.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ import { verifyArgsAndStripIfElement } from '../../utils'
4343
*
4444
*/
4545

46-
export default function executeAsync (this: WebdriverIO.BrowserObject, script: string | Function, ...args: any[]) {
47-
/*!
46+
export default function executeAsync (
47+
this: WebdriverIO.BrowserObject,
48+
script: string | Function,
49+
...args: any[]
50+
) {
51+
/**
4852
* parameter check
4953
*/
5054
if ((typeof script !== 'string' && typeof script !== 'function')) {
5155
throw new Error('number or type of arguments don\'t agree with execute protocol command')
5256
}
5357

54-
/*!
58+
/**
5559
* instances started as multibrowserinstance can't getting called with
5660
* a function parameter, therefor we need to check if it starts with "function () {"
5761
*/

packages/webdriverio/src/commands/browser/getCookies.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
* @uses webdriver/getAllCookies
3131
*
3232
*/
33-
export default async function getCookies(this: WebdriverIO.BrowserObject, names?: string | string[]) {
33+
export default async function getCookies(
34+
this: WebdriverIO.BrowserObject,
35+
names?: string | string[]
36+
) {
3437
if (names === undefined) {
35-
return this.getAllCookies()
38+
return this.getAllCookies() as Promise<WebDriver.Cookie[]>
3639
}
3740

3841
const namesList = Array.isArray(names) ? names : [names]

packages/webdriverio/src/commands/browser/getPuppeteer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default async function getPuppeteer (this: WebdriverIO.BrowserObject) {
1616
* that instance
1717
*/
1818
if (this.puppeteer) {
19-
return this.puppeteer
19+
return this.puppeteer as puppeteer.Browser
2020
}
2121

2222
/**
@@ -28,7 +28,7 @@ export default async function getPuppeteer (this: WebdriverIO.BrowserObject) {
2828
browserURL: `http://${chromiumOptions.debuggerAddress}`,
2929
defaultViewport: null
3030
})
31-
return this.puppeteer
31+
return this.puppeteer as puppeteer.Browser
3232
}
3333

3434
/**
@@ -56,7 +56,7 @@ export default async function getPuppeteer (this: WebdriverIO.BrowserObject) {
5656
browserURL: `http://localhost:${rdPort}`,
5757
defaultViewport: null
5858
})
59-
return this.puppeteer
59+
return this.puppeteer as puppeteer.Browser
6060
}
6161
}
6262

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/**
22
*
3-
* Returns browser window size (and position for drivers with W3C support).
3+
* Returns browser window size.
44
*
55
* <example>
66
* :getWindowSize.js
77
it('should return browser window size', function () {
8-
const windowSize = browser.getWindowSize(500, 600);
8+
const windowSize = browser.getWindowSize();
99
console.log(windowSize);
10-
// outputs
11-
// Firefox: { x: 4, y: 23, width: 1280, height: 767 }
12-
// Chrome: { width: 1280, height: 767 }
10+
// outputs `{ width: 1280, height: 767 }`
1311
});
1412
* </example>
1513
*
@@ -21,11 +19,18 @@
2119

2220
import { getBrowserObject } from '../../utils'
2321

24-
export default function getWindowSize(this: WebdriverIO.BrowserObject) {
22+
interface BrowserSize {
23+
width: number
24+
height: number
25+
}
26+
27+
export default async function getWindowSize(this: WebdriverIO.BrowserObject) {
2528
const browser = getBrowserObject(this)
2629

2730
if (!browser.isW3C) {
28-
return browser._getWindowSize()
31+
return browser._getWindowSize() as BrowserSize
2932
}
30-
return browser.getWindowRect()
33+
34+
const { width, height } = await browser.getWindowRect() as BrowserSize
35+
return { width, height } as BrowserSize
3136
}

packages/webdriverio/src/commands/browser/keys.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727

2828
import { checkUnicode } from '../../utils'
2929

30-
export default function keys (this: WebdriverIO.BrowserObject, value: string | string[]) {
30+
export default function keys (
31+
this: WebdriverIO.BrowserObject,
32+
value: string | string[]
33+
) {
3134
let keySequence: string[] = []
3235

3336
/**

packages/webdriverio/src/commands/browser/mock.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ import { getBrowserObject } from '../../utils'
102102

103103
const SESSION_MOCKS: Record<string, Set<Interception>> = {}
104104

105-
export default async function mock (this: WebdriverIO.BrowserObject, url: string, filterOptions: WebdriverIO.MockFilterOptions) {
105+
export default async function mock (
106+
this: WebdriverIO.BrowserObject,
107+
url: string,
108+
filterOptions?: WebdriverIO.MockFilterOptions
109+
): Promise<Interception> {
106110
const NetworkInterception = this.isSauce ? WebDriverNetworkInterception : DevtoolsNetworkInterception
107111

108112
if (!this.isSauce) {
@@ -121,7 +125,9 @@ export default async function mock (this: WebdriverIO.BrowserObject, url: string
121125
if (SESSION_MOCKS[handle].size === 0 && !this.isSauce) {
122126
const pages = await this.puppeteer.pages()
123127

124-
// get active page
128+
/**
129+
* get active page
130+
*/
125131
let page
126132
for (let i = 0; i < pages.length && !page; i++) {
127133
const isHidden = await pages[i].evaluate(() => document.hidden)
@@ -130,7 +136,9 @@ export default async function mock (this: WebdriverIO.BrowserObject, url: string
130136
}
131137
}
132138

133-
// fallback to the first page
139+
/**
140+
* fallback to the first page
141+
*/
134142
if (!page) {
135143
page = pages[0]
136144
}

packages/webdriverio/src/commands/browser/newWindow.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@
3636

3737
import newWindowHelper from '../../scripts/newWindow'
3838

39-
export default async function newWindow (this: WebdriverIO.BrowserObject, url: string, { windowName = 'New Window', windowFeatures = '' }: WebdriverIO.NewWindowOptions = {}) {
40-
/*!
39+
export default async function newWindow (
40+
this: WebdriverIO.BrowserObject,
41+
url: string,
42+
{ windowName = 'New Window', windowFeatures = '' }: WebdriverIO.NewWindowOptions = {}
43+
) {
44+
/**
4145
* parameter check
4246
*/
4347
if (typeof url !== 'string') {
4448
throw new Error('number or type of arguments don\'t agree with newWindow command')
4549
}
4650

47-
/*!
51+
/**
4852
* mobile check
4953
*/
5054
if (this.isMobile) {

packages/webdriverio/src/commands/browser/pause.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
*
2121
*/
2222

23-
export default function pause (this: WebdriverIO.BrowserObject, milliseconds = 1000) {
23+
export default function pause (
24+
this: WebdriverIO.BrowserObject,
25+
milliseconds = 1000
26+
) {
2427
return new Promise((resolve) => setTimeout(resolve, milliseconds))
2528
}

packages/webdriverio/src/commands/browser/savePDF.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
import fs from 'fs'
3232
import { getAbsoluteFilepath, assertDirectoryExists } from '../../utils'
3333

34-
export default async function savePDF (this: WebdriverIO.BrowserObject, filepath: string, options?: WebdriverIO.PDFPrintOptions) {
34+
export default async function savePDF (
35+
this: WebdriverIO.BrowserObject,
36+
filepath: string,
37+
options?: WebdriverIO.PDFPrintOptions
38+
) {
3539
/**
3640
* type check
3741
*/

packages/webdriverio/src/commands/browser/saveRecordingScreen.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import fs from 'fs'
2323
import { getAbsoluteFilepath, assertDirectoryExists } from '../../utils'
2424

25-
export default async function saveRecordingScreen (this: WebdriverIO.BrowserObject, filepath: string) {
25+
export default async function saveRecordingScreen (
26+
this: WebdriverIO.BrowserObject,
27+
filepath: string
28+
) {
2629
/**
2730
* type check
2831
*/

packages/webdriverio/src/commands/browser/saveScreenshot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import fs from 'fs'
2222
import { getAbsoluteFilepath, assertDirectoryExists } from '../../utils'
2323

24-
export default async function saveScreenshot (this: WebdriverIO.BrowserObject, filepath: string) {
24+
export default async function saveScreenshot (
25+
this: WebdriverIO.BrowserObject,
26+
filepath: string
27+
) {
2528
/**
2629
* type check
2730
*/

packages/webdriverio/src/commands/browser/setCookies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default async function setCookies(
6262
throw new Error('Invalid input (see https://webdriver.io/docs/api/browser/setCookies.html for documentation.')
6363
}
6464

65-
return Promise.all(cookieObjsList
65+
await Promise.all(cookieObjsList
6666
.map(cookieObj => this.addCookie(cookieObj)))
67+
return
6768
}

0 commit comments

Comments
 (0)