Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/matchers/browser/toHaveTitle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { waitUntil, enhanceError, compareText } from '../../utils.js'
import { waitUntil, enhanceError, compareText, compareTextWithArray } from '../../utils.js'
import { DEFAULT_OPTIONS } from '../../constants.js'

export async function toHaveTitle(
Expand All @@ -17,9 +17,20 @@ export async function toHaveTitle(

let actual
const pass = await waitUntil(async () => {
actual = await browser.getTitle()
const result = await browser.getTitle()
actual = result

return compareText(actual, expectedValue, options).result
if (Array.isArray(result)) {
const results = result.map((item) => {
return Array.isArray(expectedValue)
? compareTextWithArray(item, expectedValue, options).result
: compareText(item, expectedValue, options).result
})

return results.every((res) => res)
}

return compareText(result, expectedValue, options).result
}, isNot, options)

const message = enhanceError('window', expectedValue, actual, this, verb, expectation, '', options)
Expand Down
17 changes: 14 additions & 3 deletions src/matchers/browser/toHaveUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { waitUntil, enhanceError, compareText } from '../../utils.js'
import { waitUntil, enhanceError, compareText, compareTextWithArray } from '../../utils.js'
import { DEFAULT_OPTIONS } from '../../constants.js'

export async function toHaveUrl(
Expand All @@ -17,9 +17,20 @@ export async function toHaveUrl(

let actual
const pass = await waitUntil(async () => {
actual = await browser.getUrl()
const result = await browser.getUrl()
actual = result

return compareText(actual, expectedValue, options).result
if (Array.isArray(result)) {
const results = result.map((item) => {
return Array.isArray(expectedValue)
? compareTextWithArray(item, expectedValue, options).result
: compareText(item, expectedValue, options).result
})

return results.every((res) => res)
}

return compareText(result, expectedValue, options).result
}, isNot, options)

const message = enhanceError('window', expectedValue, actual, this, verb, expectation, '', options)
Expand Down
24 changes: 23 additions & 1 deletion src/matchers/element/toHaveAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElementMaybePromise } from '../../types.js'
import {
compareText,
compareTextWithArray,
enhanceError,
executeCommand,
waitUntil,
Expand All @@ -10,15 +11,36 @@ import {

async function conditionAttr(el: WebdriverIO.Element, attribute: string) {
const attr = await el.getAttribute(attribute)
if (Array.isArray(attr)) {
return {
result: attr.every(a => typeof a === 'string'),
value: attr
}
}
if (typeof attr !== 'string') {
return { result: false, value: attr }
}
return { result: true, value: attr }

}

async function conditionAttrAndValue(el: WebdriverIO.Element, attribute: string, value: string | RegExp | WdioAsymmetricMatcher<string>, options: ExpectWebdriverIO.StringOptions) {
const attr = await el.getAttribute(attribute)
if (Array.isArray(attr)) {
const results = attr.map((item) => {
if (typeof item !== 'string') {
return { result: false, value: item }
}
return Array.isArray(value)
? compareTextWithArray(item, value, options)
: compareText(item, value, options)
})

return {
result: results.every((res) => res.result),
value: attr
}
}

if (typeof attr !== 'string') {
return { result: false, value: attr }
}
Expand Down
29 changes: 29 additions & 0 deletions src/matchers/element/toHaveElementProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElementMaybePromise } from '../../types.js'
import {
compareText,
compareTextWithArray,
enhanceError,
executeCommand,
waitUntil,
Expand All @@ -21,6 +22,34 @@ async function condition(
return { result: false, value: prop }
}

if (Array.isArray(prop)) {
// existence check
if (value === null) {
return { result: prop.every(p => p === null), value: prop }
}

const results = prop.map((item) => {
if (item === null || item === undefined) {
return { result: false, value: item }
}
if (value === null) {
return { result: item === null, value: item }
}
if (!(value instanceof RegExp) && typeof item !== 'string' && !asString) {
return { result: item === value, value: item }
}
const itemStr = item.toString()
return Array.isArray(value)
? compareTextWithArray(itemStr, value as string[], options)
: compareText(itemStr, value as string | RegExp | WdioAsymmetricMatcher<string>, options)
})

return {
result: results.every((res) => res.result),
value: prop
}
}

if (value === null) {
return { result: true, value: prop }
}
Expand Down
19 changes: 15 additions & 4 deletions src/matchers/element/toHaveText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ async function condition(el: WebdriverIO.Element | WebdriverIO.ElementArray, tex
checkAllValuesMatchCondition = resultArray.every(Boolean)
} else {
const actualText = await (el as WebdriverIO.Element).getText()
actualTextArray.push(actualText)
checkAllValuesMatchCondition = Array.isArray(text)
? compareTextWithArray(actualText, text, options).result
: compareText(actualText, text, options).result
if (Array.isArray(actualText)) {
for (const value of actualText) {
actualTextArray.push(value)
const result = Array.isArray(text)
? compareTextWithArray(value, text, options).result
: compareText(value, text, options).result
resultArray.push(result)
}
checkAllValuesMatchCondition = resultArray.every(Boolean)
} else {
actualTextArray.push(actualText)
checkAllValuesMatchCondition = Array.isArray(text)
? compareTextWithArray(actualText, text, options).result
: compareText(actualText, text, options).result
}
}

return {
Expand Down