2024-03-06 18:34:07 +03:00
|
|
|
import { expect as baseExpect, type Locator } from 'playwright/test'
|
2023-11-27 18:48:37 +03:00
|
|
|
|
2024-03-06 18:34:07 +03:00
|
|
|
export const expect = baseExpect.extend({
|
2024-10-09 15:26:56 +03:00
|
|
|
/**
|
|
|
|
* Ensures that at least one of the elements that the Locator points to,
|
|
|
|
* is an attached and visible DOM node.
|
|
|
|
*/
|
2024-03-06 18:34:07 +03:00
|
|
|
async toExist(locator: Locator) {
|
|
|
|
// Counter-intuitive, but correct:
|
|
|
|
// https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-be-visible
|
|
|
|
const assertionName = 'toExist'
|
|
|
|
let pass: boolean
|
|
|
|
try {
|
|
|
|
await expect(locator.first()).toBeVisible()
|
|
|
|
pass = true
|
2024-11-27 17:09:59 +03:00
|
|
|
} catch {
|
2024-03-06 18:34:07 +03:00
|
|
|
pass = false
|
|
|
|
}
|
2024-02-02 20:11:28 +03:00
|
|
|
|
2024-03-06 18:34:07 +03:00
|
|
|
const message = () =>
|
|
|
|
this.utils.matcherHint(assertionName, locator, '', {
|
|
|
|
isNot: this.isNot,
|
|
|
|
})
|
2024-02-28 16:14:48 +03:00
|
|
|
|
2024-03-06 18:34:07 +03:00
|
|
|
return {
|
|
|
|
message,
|
|
|
|
pass,
|
|
|
|
name: assertionName,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async toBeSelected(locator: Locator) {
|
|
|
|
const assertionName = 'toBeSelected'
|
|
|
|
let pass: boolean
|
|
|
|
try {
|
|
|
|
await baseExpect(locator).toHaveClass(/(?<=^| )selected(?=$| )/, { timeout: 50 })
|
|
|
|
pass = true
|
2024-10-11 21:23:02 +03:00
|
|
|
} catch {
|
|
|
|
// Do not log the error.
|
2024-03-06 18:34:07 +03:00
|
|
|
pass = false
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = () =>
|
|
|
|
this.utils.matcherHint(assertionName, locator, '', {
|
|
|
|
isNot: this.isNot,
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
message,
|
|
|
|
pass,
|
|
|
|
name: assertionName,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|