Skip to content

Commit 63bd0c6

Browse files
Rewrite webdriverio element commands (#6139)
Co-authored-by: Mykola Grybyk <[email protected]>
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]

0 commit comments

Comments
 (0)