Expect
在编写测试时,您经常需要检查值是否满足特定条件。expect
为您提供了许多"匹配器",让您能够在 browser
、element
或 mock
对象上进行各种验证。
默认选项
以下默认选项与配置中设置的 waitforTimeout
和 waitforInterval
选项相关联。
只有当您想为断言设置特定的超时时间时,才需要设置以下选项。
{
wait: 2000, // ms to wait for expectation to succeed
interval: 100, // interval between attempts
}
如果您想选择不同的超时和间隔时间,可以这样设置这些选项:
// wdio.conf.js
import { setOptions } from 'expect-webdriverio'
export const config = {
// ...
before () {
setOptions({ wait: 5000 })
},
// ...
}
匹配器选项
每个匹配器都可以接受多个选项,允许您修改断言:
命令选项
名称 | 类型 | 详情 |
---|---|---|
wait | number | 等待期望成功的时间(毫秒)。默认值:3000 |
interval | number | 尝试之间的间隔。默认值:100 |
beforeAssertion | function | 在断言之前调用的函数 |
afterAssertion | function | 在断言之后调用的包含断言结果的函数 |
message | string | 断言错误前添加的用户消息 |
字符串选项
在断言字符串时,可以在命令选项之外应用此选项。
名称 | 类型 | 详情 |
---|---|---|
ignoreCase | boolean | 对实际值和期望值应用 toLowerCase |
trim | boolean | 对实际值应用 trim |
replace | Replacer | Replacer[] | 替 换实际值中与字符串/正则表达式匹配的部分。替换器可以是字符串或函数。 |
containing | boolean | 期望实际值包含期望值,否则严格相等。 |
asString | boolean | 可能有助于强制将属性值转换为字符串 |
atStart | boolean | 期望实际值以期望值开始 |
atEnd | boolean | 期望实际值以期望值结束 |
atIndex | number | 期望实际值在给定索引处具有期望值 |
数字选项
在断言数字时,可以在命令选项之外应用此选项。
名称 | 类型 | 详情 |
---|---|---|
eq | number | 等于 |
lte | number | 小于等于 |
gte | number | 大于等于 |
处理 HTML 实体
HTML 实体是一段以"&"符号开始并以分号";"结束的文本("字符串")。实体通常用于显示保留字符(否则会被解释为 HTML 代码)和不可见字符(如不间断空格,例如
)。
要查找或与此类元素交互,请使用实体的 Unicode 等效项。例如:
<div data="Some Value">Some Text</div>
const myElem = await $('div[data="Some\u00a0Value"]')
await expect(myElem).toHaveAttribute('data', 'div[Some\u00a0Value')
await expect(myElem).toHaveText('Some\u00a0Text')
您可以在 HTML 规范 中找到所有 Unicode 引用。
注意: Unicode 不区分大小写,因此 \u00a0
和 \u00A0
都有效。要在浏览器检查中查找元素,请从 Unicode 中删除 u
,例如:div[data="Some\00a0Value"]
浏览器匹配器
toHaveUrl
检查浏览器是否在特定页面上。
用法
await browser.url('https://webdriver.io/')
await expect(browser).toHaveUrl('https://webdriver.io')
用法
await browser.url('https://webdriver.io/')
await expect(browser).toHaveUrl(expect.stringContaining('webdriver'))
toHaveTitle
检查网站是否有特定标题。
用法
await browser.url('https://webdriver.io/')
await expect(browser).toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js')
await expect(browser).toHaveTitle(expect.stringContaining('WebdriverIO'))
toHaveClipboardText
检查浏览器的剪贴板中是否存储了特定文本。
用法
import { Key } from 'webdriverio'
await browser.keys([Key.Ctrl, 'a'])
await browser.keys([Key.Ctrl, 'c'])
await expect(browser).toHaveClipboardText('some clipboard text')
await expect(browser).toHaveClipboardText(expect.stringContaining('clipboard text'))
元素匹配器
toBeDisplayed
在给定元素上调用 isDisplayed
。
用法
const elem = await $('#someElem')
await expect(elem).toBeDisplayed()
toExist
在给定元素上调用 isExisting
。
用法
const elem = await $('#someElem')
await expect(elem).toExist()
toBePresent
与 toExist
相同。
用法
const elem = await $('#someElem')
await expect(elem).toBePresent()
toBeExisting
与 toExist
相同。
用法
const elem = await $('#someElem')
await expect(elem).toBeExisting()
toBeFocused
检查元素是否有焦点。此断言仅在 Web 上下文中有效。
用法
const elem = await $('#someElem')
await expect(elem).toBeFocused()
toHaveAttribute
检查元素是否具有具有特定值的某个属性。
用法
const myInput = await $('input')
await expect(myInput).toHaveAttribute('class', 'form-control')
await expect(myInput).toHaveAttribute('class', expect.stringContaining('control'))
toHaveAttr
与 toHaveAttribute
相同。
用法
const myInput = await $('input')
await expect(myInput).toHaveAttr('class', 'form-control')
await expect(myInput).toHaveAttr('class', expect.stringContaining('control'))
toHaveElementClass
检查元素是否具有单个类名。当元素可以有多个类名时,也可以使用数组作为参数调用。
用法
const myInput = await $('input')
await expect(myInput).toHaveElementClass('form-control', { message: 'Not a form control!' })
await expect(myInput).toHaveElementClass(['form-control' , 'w-full'], { message: 'not full width' })
await expect(myInput).toHaveElementClass(expect.stringContaining('form'), { message: 'Not a form control!' })
toHaveElementProperty
检查元素是否具有特定属性。
用法
const elem = await $('#elem')
await expect(elem).toHaveElementProperty('height', 23)
await expect(elem).not.toHaveElementProperty('height', 0)
toHaveValue
检查输入元素是否具有特定值。
用法
const myInput = await $('input')
await expect(myInput).toHaveValue('admin-user', { ignoreCase: true })
await expect(myInput).toHaveValue(expect.stringContaining('user'), { ignoreCase: true })
toBeClickable
通过在元素上调用 isClickable
检查元素是否可点击。
用法
const elem = await $('#elem')
await expect(elem).toBeClickable()
toBeDisabled
通过在元素上调用 isEnabled
检查元素是否被禁用。
用法
const elem = await $('#elem')
await expect(elem).toBeDisabled()
// same as
await expect(elem).not.toBeEnabled()
toBeEnabled
通过在元素上调用 isEnabled
检查元素是否启用。