跳到主要内容
版本:4.x

查询相关 API

类型说明

TWaitforParams:

属性类型必须默认值说明
containerHTMLElementwindow.document查询的 root 节点
timeoutnumber1000失效时间
intervalnumber50查询间隔
mutationObserverOptionsMutationObserverInit{subtree: true, childList: true, attributes: true, characterData: true }监听器参数

按照选择器查询

querySelector

function querySelector(selectors: string): HTMLElement

参数:

参数类型必须说明
selectorsstring选择器,同 docuemt.querySelector 一致

用法:

const btns = testUtils.queries.querySelectorAll('.btns')

querySelectorAll

function querySelectorAll(selectors: string): HTMLElement[]

参数:

参数类型必须说明
selectorsstring选择器,同 docuemt.querySelectorAll 一致

用法:

const btns = testUtils.queries.querySelectorAll('.btns')

waitForQuerySelector

async function waitForQuerySelector(selectors: string, params?: TParams): Promise<HTMLElement>

参数:

参数类型必须说明
selectorsstring选择器,同 docuemt.querySelector 一致
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

const btn = await testUtils.queries.waitForQuerySelector('.async-btn')

waitForQuerySelectorAll

async function waitForQuerySelectorAll(selectors: string, params?: TParams): Promise<HTMLElement[]>

参数:

参数类型必须说明
selectorsstring选择器,同 docuemt.querySelectorAll 一致
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

const btns = await testUtils.queries.waitForQuerySelectorAll('.async-btns')

按照文本查询

queryByText

function queryByText(text: string, selector?: string): HTMLElement

参数:

参数类型必须说明
textstring文本内容,部分匹配即可
selectorstring选择器,同 docuemt.querySelector 一致

用法:

// <Text>Hello World!!!</Text>
const textView = testUtils.queries.queryByText('Hello World')

queryByTextAll

function queryAllByText(text: string, selector?: string): HTMLElement[]

参数:

参数类型必须说明
textstring文本内容,部分匹配即可
selectorstring选择器,同 docuemt.querySelector 一致

用法:

// <Text>Hello World!!</Text>
// <View>Hello World!!!</View>
const textViews = testUtils.queries.queryAllByText('Hello World')

waitForQueryByText

async function waitForQueryByText(text: string, selector?: string, params?: TWaitforParams): HTMLElement

参数:

参数类型必须说明
textstring文本内容,部分匹配即可
selectorstring选择器,同 docuemt.querySelector 一致
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

// <Text>Hello World!!!</Text>
const textView = async testUtils.queries.waitForQueryByText("Hello World");

waitForQueryAllByText

async function waitForQueryAllByText(text: string, selector?: string, params?: TWaitforParams): HTMLElement[]

参数:

参数类型必须说明
textstring文本内容,部分匹配即可
selectorstring选择器,同 docuemt.querySelector 一致
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

// <Text>Hello World!!</Text>
// <View>Hello World!!!</View>
const textViews = async testUtils.queries.waitForQueryAllByText("Hello World");

按照 Placeholder 查询

queryByPlaceholder

function queryByPlaceholder(text: string): HTMLElement

参数:

参数类型必须说明
textstringplaceholder 内容

用法:

// <input placeholder="hello" />
const input = testUtils.queries.queryByPlaceholder('hello')

queryAllByPlaceholder

function queryAllByPlaceholder(text: string): HTMLElement[]

参数:

参数类型必须说明
textstringplaceholder 内容

用法:

// <input placeholder="hello" />
// <input placeholder="hello" />
const inputs = testUtils.queries.queryAllByPlaceholder('hello')

waitForQueryByPlaceholder

async function waitForQueryByPlaceholder(text: string, params?: TParams): Promise<HTMLElement>

参数:

参数类型必须说明
textstringplaceholder 内容
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

// <input placeholder="async-placeholde" />
const input = await testUtils.queries.waitForQueryByPlaceholder('async-placeholder')

waitForQueryAllByPlaceholder

async function waitForQueryAllByPlaceholder(text: string, params?: TParams): Promise<HTMLElement[]>

参数:

参数类型必须说明
textstringplaceholder 内容
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

// <input placeholder="async-placeholde" />
// <input placeholder="async-placeholde" />
const inputs = await testUtils.queries.waitForQueryAllByPlaceholder('async-placeholder')

按照属性查询

queryByAttribute

function queryByAttribute(attr: string, value: any): HTMLElement

参数:

参数类型必须说明
attrstring属性 key
valueany属性 value

用法:

// <div key="value" />
const view = testUtils.queries.queryByAttribute('key', 'value')

queryAllByAttribute

function queryAllByAttribute(attr: string, value: any): HTMLElement[]

参数:

参数类型必须说明
attrstring属性 key
valueany属性 value

用法:

// <div key="value" />
// <div key="value" />
const view = testUtils.queries.queryAllByAttribute('key', 'value')

waitForQueryByAttribute

async function waitForQueryByAttribute(attr: string, value: string, params?: TParams): Promise<HTMLElement>

参数:

参数类型必须说明
attrstring属性 key
valueany属性 value
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

// <div key="value" />
const view = await testUtils.queries.waitForQueryByAttribute('key', 'value')

waitForQueryAllByAttribute

async function waitForQueryAllByAttribute(attr: string, value: string, params?: TParams): Promise<HTMLElement[]>

参数:

参数类型必须说明
attrstring属性 key
valueany属性 value
paramsTWaitforParams参数:见 TWaitforParams 说明

用法:

// <div key="value" />
// <div key="value" />
const inputs = await testUtils.queries.waitForQueryAllByAttribute('key', 'value')