Skip to content

Commit c7f5c93

Browse files
feature(expect-webdriverio): add number options support for toHaveHeight and toHaveWidth (#1851)
* feature(expect-webdriverio): add number options support for toHaveHeight and toHaveWidth * feature(expect-webdriverio): add number options support for toHaveHeight and toHaveWidth
1 parent 425ea56 commit c7f5c93

4 files changed

Lines changed: 84 additions & 16 deletions

File tree

src/matchers/element/toHaveHeight.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { DEFAULT_OPTIONS } from '../../constants.js'
22
import type { WdioElementMaybePromise } from '../../types.js'
33
import {
4+
compareNumbers,
45
enhanceError,
56
executeCommand,
7+
numberError,
68
waitUntil,
7-
wrapExpectedWithArray
89
} from '../../utils.js'
910

10-
async function condition(el: WebdriverIO.Element, height: number) {
11+
async function condition(el: WebdriverIO.Element, height: number, options: ExpectWebdriverIO.NumberOptions) {
1112
const actualHeight = await el.getSize('height')
1213

1314
return {
14-
value: actualHeight,
15-
result: actualHeight === height,
15+
result: compareNumbers(actualHeight, options),
16+
value: actualHeight
1617
}
1718
}
1819

1920
export async function toHaveHeight(
2021
received: WdioElementMaybePromise,
21-
expectedValue: number,
22+
expectedValue: number | ExpectWebdriverIO.NumberOptions,
2223
options: ExpectWebdriverIO.CommandOptions = DEFAULT_OPTIONS
2324
) {
2425
const isNot = this.isNot
@@ -30,25 +31,36 @@ export async function toHaveHeight(
3031
options,
3132
})
3233

34+
// type check
35+
let numberOptions: ExpectWebdriverIO.NumberOptions
36+
if (typeof expectedValue === 'number') {
37+
numberOptions = { eq: expectedValue } as ExpectWebdriverIO.NumberOptions
38+
} else if (!expectedValue || (typeof expectedValue.eq !== 'number' && typeof expectedValue.gte !== 'number' && typeof expectedValue.lte !== 'number')) {
39+
throw new Error('Invalid params passed to toHaveHeight.')
40+
} else {
41+
numberOptions = expectedValue
42+
}
43+
3344
let el = await received?.getElement()
3445
let actualHeight
3546

3647
const pass = await waitUntil(
3748
async () => {
38-
const result = await executeCommand.call(this, el, condition, options, [expectedValue, options])
49+
const result = await executeCommand.call(this, el, condition, numberOptions, [expectedValue, numberOptions])
3950

4051
el = result.el as WebdriverIO.Element
4152
actualHeight = result.values
4253

4354
return result.success
4455
},
4556
isNot,
46-
options
57+
{ ...numberOptions, ...options }
4758
)
4859

60+
const error = numberError(numberOptions)
4961
const message = enhanceError(
5062
el,
51-
wrapExpectedWithArray(el, actualHeight, expectedValue),
63+
error,
5264
actualHeight,
5365
this,
5466
verb,

src/matchers/element/toHaveWidth.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { DEFAULT_OPTIONS } from '../../constants.js'
22
import type { WdioElementMaybePromise } from '../../types.js'
33
import {
4+
compareNumbers,
45
enhanceError,
56
executeCommand,
7+
numberError,
68
waitUntil,
7-
wrapExpectedWithArray
89
} from '../../utils.js'
910

10-
async function condition(el: WebdriverIO.Element, width: number) {
11+
async function condition(el: WebdriverIO.Element, width: number, options: ExpectWebdriverIO.NumberOptions) {
1112
const actualWidth = await el.getSize('width')
1213

1314
return {
14-
value: actualWidth,
15-
result: actualWidth === width,
15+
result: compareNumbers(actualWidth, options),
16+
value: actualWidth
1617
}
1718
}
1819

1920
export async function toHaveWidth(
2021
received: WdioElementMaybePromise,
21-
expectedValue: number,
22+
expectedValue: number | ExpectWebdriverIO.NumberOptions,
2223
options: ExpectWebdriverIO.CommandOptions = DEFAULT_OPTIONS
2324
) {
2425
const isNot = this.isNot
@@ -30,25 +31,36 @@ export async function toHaveWidth(
3031
options,
3132
})
3233

34+
// type check
35+
let numberOptions: ExpectWebdriverIO.NumberOptions
36+
if (typeof expectedValue === 'number') {
37+
numberOptions = { eq: expectedValue } as ExpectWebdriverIO.NumberOptions
38+
} else if (!expectedValue || (typeof expectedValue.eq !== 'number' && typeof expectedValue.gte !== 'number' && typeof expectedValue.lte !== 'number')) {
39+
throw new Error('Invalid params passed to toHaveHeight.')
40+
} else {
41+
numberOptions = expectedValue
42+
}
43+
3344
let el = await received?.getElement()
3445
let actualWidth
3546

3647
const pass = await waitUntil(
3748
async () => {
38-
const result = await executeCommand.call(this, el, condition, options, [expectedValue, options])
49+
const result = await executeCommand.call(this, el, condition, numberOptions, [expectedValue, numberOptions])
3950

4051
el = result.el as WebdriverIO.Element
4152
actualWidth = result.values
4253

4354
return result.success
4455
},
4556
isNot,
46-
options
57+
{ ...numberOptions, ...options }
4758
)
4859

60+
const error = numberError(numberOptions)
4961
const message = enhanceError(
5062
el,
51-
wrapExpectedWithArray(el, actualWidth, expectedValue),
63+
error,
5264
actualWidth,
5365
this,
5466
verb,

test/matchers/element/toHaveHeight.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('toHaveHeight', () => {
6767
}
6868

6969
const result = await toHaveHeight.call({}, el, 32, {})
70+
expect(result.message()).toEqual('Expect $(`sel`) to have height\n\nExpected: 32\nReceived: serializes to the same string')
7071
expect(result.pass).toBe(true)
7172
expect(el._attempts).toBe(1)
7273
})
@@ -86,6 +87,7 @@ describe('toHaveHeight', () => {
8687
}
8788

8889
const result = await toHaveHeight.call({}, el, 10, { wait: 0 })
90+
expect(result.message()).toEqual('Expect $(`sel`) to have height\n\nExpected: 10\nReceived: 32')
8991
expect(result.pass).toBe(false)
9092
expect(el._attempts).toBe(1)
9193
})
@@ -109,6 +111,26 @@ describe('toHaveHeight', () => {
109111
expect(el._attempts).toBe(1)
110112
})
111113

114+
test('gte and lte', async () => {
115+
const el: any = await $('sel')
116+
el._attempts = 0
117+
el._size = function (property?: 'width' | 'height') {
118+
this._attempts++
119+
if (property === 'width') {
120+
return 50
121+
}
122+
if (property === 'height') {
123+
return 32
124+
}
125+
return { width: 50, height: 32 }
126+
}
127+
128+
const result = await toHaveHeight.call({}, el, { gte: 31, lte: 33 }, { wait: 0 })
129+
expect(result.message()).toEqual('Expect $(`sel`) to have height\n\nExpected: ">= 31 && <= 33"\nReceived: 32')
130+
expect(result.pass).toBe(true)
131+
expect(el._attempts).toBe(1)
132+
})
133+
112134
test('not - failure', async () => {
113135
const el: any = await $('sel')
114136
el._size = function (property?: 'width' | 'height') {

test/matchers/element/toHaveWidth.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('toHaveWidth', () => {
6767
}
6868

6969
const result = await toHaveWidth.call({}, el, 50, {})
70+
expect(result.message()).toEqual('Expect $(`sel`) to have width\n\nExpected: 50\nReceived: serializes to the same string')
7071
expect(result.pass).toBe(true)
7172
expect(el._attempts).toBe(1)
7273
})
@@ -86,6 +87,7 @@ describe('toHaveWidth', () => {
8687
}
8788

8889
const result = await toHaveWidth.call({}, el, 10, { wait: 0 })
90+
expect(result.message()).toEqual('Expect $(`sel`) to have width\n\nExpected: 10\nReceived: 50')
8991
expect(result.pass).toBe(false)
9092
expect(el._attempts).toBe(1)
9193
})
@@ -109,6 +111,26 @@ describe('toHaveWidth', () => {
109111
expect(el._attempts).toBe(1)
110112
})
111113

114+
test('gte and lte', async () => {
115+
const el: any = await $('sel')
116+
el._attempts = 0
117+
el._size = function (property?: 'width' | 'height') {
118+
this._attempts++
119+
if (property === 'width') {
120+
return 50
121+
}
122+
if (property === 'height') {
123+
return 32
124+
}
125+
return { width: 50, height: 32 }
126+
}
127+
128+
const result = await toHaveWidth.call({}, el, { gte: 49, lte: 51 }, { wait: 0 })
129+
expect(result.message()).toEqual('Expect $(`sel`) to have width\n\nExpected: ">= 49 && <= 51"\nReceived: 50')
130+
expect(result.pass).toBe(true)
131+
expect(el._attempts).toBe(1)
132+
})
133+
112134
test('not - failure', async () => {
113135
const el: any = await $('sel')
114136
el._size = function (property?: 'width' | 'height') {

0 commit comments

Comments
 (0)