-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
getSize.ts
60 lines (48 loc) · 1.63 KB
/
getSize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { RectReturn } from '@wdio/protocols'
import { getElementRect } from '../../utils'
export type Size = Pick<RectReturn, 'width' | 'height'>;
function getSize (this: WebdriverIO.Element): Promise<Size>;
function getSize (this: WebdriverIO.Element, prop: keyof RectReturn): Promise<number>;
/**
*
* Get the width and height for an DOM-element.
*
* <example>
:getSize.js
it('should demonstrate the getSize command', async () => {
await browser.url('http://github.com')
const logo = await $('.octicon-mark-github')
const size = await logo.getSize()
console.log(size) // outputs: { width: 32, height: 32 }
const width = await logo.getSize('width')
console.log(width) // outputs: 32
const height = await logo.getSize('height')
console.log(height) // outputs: 32
})
* </example>
*
* @alias element.getElementSize
* @param {String=} prop size to receive [optional] ("width" or "height")
* @return {Object|Number} requested element size (`{ width: <Number>, height: <Number> }`) or actual width/height as number if prop param is given
* @type property
*
*/
async function getSize (
this: WebdriverIO.Element,
prop?: keyof RectReturn
): Promise<Size | number> {
let rect: Partial<RectReturn> = {}
if (this.isW3C) {
rect = await getElementRect(this)
} else {
rect = await this.getElementSize(this.elementId) as RectReturn
}
if (prop && typeof rect[prop] === 'number') {
return rect[prop] as number
}
return {
width: rect.width,
height: rect.height
} as Size
}
export default getSize