Skip to content

Commit 85f99fb

Browse files
Merge pull request #1309 from erwinheitzman/add-new-matchers
(expect-webdriverio): add new matchers
2 parents e09f71c + 0ed7ef0 commit 85f99fb

23 files changed

+2190
-390
lines changed

__mocks__/@wdio/globals.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ function getHTMLFn(includeSelectorTag) {
2020
return this._html ? this._html(includeSelectorTag) : undefined
2121
}
2222

23+
function getComputedLabelFn() {
24+
return this._computed_label ? this._computed_label() : undefined
25+
}
26+
27+
function getComputedRoleFn() {
28+
return this._computed_role ? this._computed_role() : undefined
29+
}
30+
31+
function getSizeFn(property?: 'height' | 'width') {
32+
return this._size ? this._size(property) : undefined
33+
}
34+
2335
const element = {
2436
$,
2537
$$,
@@ -33,6 +45,9 @@ const element = {
3345
getProperty: getPropertyFn,
3446
getText: getTextFn,
3547
getHTML: getHTMLFn,
48+
getComputedLabel: getComputedLabelFn,
49+
getComputedRole: getComputedRoleFn,
50+
getSize: getSizeFn,
3651
}
3752

3853
export function $(selector) {

docs/API.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,58 @@ const elem = await $('#elem')
349349
await expect(elem).toBeChecked()
350350
```
351351

352+
### toHaveComputedLabel
353+
354+
Checks if element has a specific computed WAI-ARIA label. Can also be called with an array as parameter in the case where the element can have different labels.
355+
356+
##### Usage
357+
358+
```js
359+
await browser.url('https://webdriver.io/')
360+
const elem = await $('a[href="https://github.com/webdriverio/webdriverio"]')
361+
await expect(elem).toHaveComputedLabel('GitHub repository')
362+
await expect(elem).toHaveComputedLabel(['GitHub repository', 'Private repository'])
363+
```
364+
365+
### toHaveComputedLabelContaining
366+
367+
Checks if element contains a specific computed WAI-ARIA label. Can also be called with an array as parameter in the case where the element can have different labels.
368+
369+
##### Usage
370+
371+
```js
372+
await browser.url('https://webdriver.io/')
373+
const elem = await $('a[href="https://github.com/webdriverio/webdriverio"]')
374+
await expect(elem).toHaveComputedLabelContaining('GitHub')
375+
await expect(elem).toHaveComputedLabelContaining(['GitHub', 'Private'])
376+
```
377+
378+
### toHaveComputedRole
379+
380+
Checks if element has a specific computed WAI-ARIA role. Can also be called with an array as parameter in the case where the element can have different labels.
381+
382+
##### Usage
383+
384+
```js
385+
await browser.url('https://webdriver.io/')
386+
const elem = await $('[aria-label="Skip to main content"]')
387+
await expect(elem).toHaveComputedRole('region')
388+
await expect(elem).toHaveComputedRole(['region', 'section'])
389+
```
390+
391+
### toHaveComputedRoleContaining
392+
393+
Checks if element contains a specific computed WAI-ARIA role. Can also be called with an array as parameter in the case where the element can have different roles.
394+
395+
##### Usage
396+
397+
```js
398+
await browser.url('https://webdriver.io/')
399+
const elem = await $('[aria-label="Skip to main content"]')
400+
await expect(elem).toHaveComputedRoleContaining('reg')
401+
await expect(elem).toHaveComputedRoleContaining(['reg', 'sec'])
402+
```
403+
352404
### toHaveHref
353405

354406
Checks if link element has a specific link target.
@@ -486,6 +538,42 @@ await expect(list).toHaveChildren(3) // the list has 3 items
486538
await expect(list).toHaveChildren({ eq: 3 })
487539
```
488540

541+
### toHaveWidth
542+
543+
Checks if element has a specific width.
544+
545+
##### Usage
546+
547+
```js
548+
await browser.url('http://github.com')
549+
const logo = await $('.octicon-mark-github')
550+
await expect(logo).toHaveWidth(32)
551+
```
552+
553+
### toHaveHeight
554+
555+
Checks if element has a specific height.
556+
557+
##### Usage
558+
559+
```js
560+
await browser.url('http://github.com')
561+
const logo = await $('.octicon-mark-github')
562+
await expect(logo).toHaveHeight(32)
563+
```
564+
565+
### toHaveSize
566+
567+
Checks if element has a specific size.
568+
569+
##### Usage
570+
571+
```js
572+
await browser.url('http://github.com')
573+
const logo = await $('.octicon-mark-github')
574+
await expect(logo).toHaveSize({ width: 32, height: 32 })
575+
```
576+
489577
### toBeElementsArrayOfSize
490578

491579
Checks amount of fetched elements using [`$$`](https://webdriver.io/docs/api/element/$$) command.

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"devDependencies": {
6161
"@types/debug": "^4.1.8",
6262
"@types/jest": "^29.5.4",
63+
"@types/lodash.isequal": "^4.5.6",
6364
"@types/node": "^20.6.0",
6465
"@typescript-eslint/eslint-plugin": "^6.7.0",
6566
"@typescript-eslint/parser": "^6.7.0",
@@ -84,6 +85,7 @@
8485
},
8586
"dependencies": {
8687
"expect": "^29.7.0",
87-
"jest-matcher-utils": "^29.7.0"
88+
"jest-matcher-utils": "^29.7.0",
89+
"lodash.isequal": "^4.5.0"
8890
}
8991
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
waitUntil,
3+
enhanceError,
4+
compareText,
5+
compareTextWithArray,
6+
executeCommand,
7+
wrapExpectedWithArray,
8+
updateElementsArray,
9+
} from '../../utils.js'
10+
11+
async function condition(
12+
el: WebdriverIO.Element,
13+
label: string | RegExp | Array<string | RegExp>,
14+
options: ExpectWebdriverIO.HTMLOptions
15+
) {
16+
const actualLabel = await el.getComputedLabel()
17+
if (Array.isArray(label)) {
18+
return compareTextWithArray(actualLabel, label, options)
19+
}
20+
return compareText(actualLabel, label, options)
21+
}
22+
23+
export async function toHaveComputedLabel(
24+
received: WebdriverIO.Element | WebdriverIO.ElementArray,
25+
label: string | RegExp | Array<string | RegExp>,
26+
options: ExpectWebdriverIO.StringOptions = {}
27+
) {
28+
const isNot = this.isNot
29+
const { expectation = 'computed label', verb = 'have' } = this
30+
31+
let el = await received
32+
let actualLabel
33+
34+
const pass = await waitUntil(
35+
async () => {
36+
const result = await executeCommand.call(this, el, condition, options, [label, options])
37+
el = result.el
38+
actualLabel = result.values
39+
40+
return result.success
41+
},
42+
isNot,
43+
options
44+
)
45+
46+
updateElementsArray(pass, received, el)
47+
48+
const message = enhanceError(
49+
el,
50+
wrapExpectedWithArray(el, actualLabel, label),
51+
actualLabel,
52+
this,
53+
verb,
54+
expectation,
55+
'',
56+
options
57+
)
58+
59+
return {
60+
pass,
61+
message: (): string => message,
62+
}
63+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { toHaveComputedLabel } from './toHaveComputedLabel.js'
2+
3+
export function toHaveComputedLabelContaining(el: WebdriverIO.Element, label: string | RegExp | Array<string | RegExp>, options: ExpectWebdriverIO.StringOptions = {}) {
4+
return toHaveComputedLabel.call(this, el, label, {
5+
...options,
6+
containing: true
7+
})
8+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
waitUntil,
3+
enhanceError,
4+
compareText,
5+
compareTextWithArray,
6+
executeCommand,
7+
wrapExpectedWithArray,
8+
updateElementsArray,
9+
} from '../../utils.js'
10+
11+
async function condition(
12+
el: WebdriverIO.Element,
13+
role: string | RegExp | Array<string | RegExp>,
14+
options: ExpectWebdriverIO.HTMLOptions
15+
) {
16+
const actualRole = await el.getComputedRole()
17+
if (Array.isArray(role)) {
18+
return compareTextWithArray(actualRole, role, options)
19+
}
20+
return compareText(actualRole, role, options)
21+
}
22+
23+
export async function toHaveComputedRole(
24+
received: WebdriverIO.Element | WebdriverIO.ElementArray,
25+
role: string | RegExp | Array<string | RegExp>,
26+
options: ExpectWebdriverIO.StringOptions = {}
27+
) {
28+
const isNot = this.isNot
29+
const { expectation = 'computed role', verb = 'have' } = this
30+
31+
let el = await received
32+
let actualRole
33+
34+
const pass = await waitUntil(
35+
async () => {
36+
const result = await executeCommand.call(this, el, condition, options, [role, options])
37+
el = result.el
38+
actualRole = result.values
39+
40+
return result.success
41+
},
42+
isNot,
43+
options
44+
)
45+
46+
updateElementsArray(pass, received, el)
47+
48+
const message = enhanceError(
49+
el,
50+
wrapExpectedWithArray(el, actualRole, role),
51+
actualRole,
52+
this,
53+
verb,
54+
expectation,
55+
'',
56+
options
57+
)
58+
59+
return {
60+
pass,
61+
message: (): string => message,
62+
}
63+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { toHaveComputedRole } from './toHaveComputedRole.js'
2+
3+
export function toHaveComputedRoleContaining(el: WebdriverIO.Element, role: string | RegExp | Array<string | RegExp>, options: ExpectWebdriverIO.StringOptions = {}) {
4+
return toHaveComputedRole.call(this, el, role, {
5+
...options,
6+
containing: true
7+
})
8+
}

0 commit comments

Comments
 (0)