组件测试工具 Component Harnesses

test-harnesses 的 API

Angular CDK testing API 参考文档

import {ContentContainerComponentHarness} from '@angular/cdk/testing';

所有组件工具都应该扩展的组件测试工具的基类。这个基础组件工具提供了定位元素和子组件工具的基本能力。它应该在用户定义自己的测试工具时继承。

Base class for component harnesses that all component harness authors should extend. This base component harness provides the basic ability to locate element and sub-component harness. It should be inherited when defining user's own harness.

属性
名称 描述

locatorFactory: LocatorFactory

方法
documentRootLocatorFactory

获取 document 根元素的 LocatorFactory。这个工厂可以用来为组件在自己的根元素之外创建(例如,通过追加到 document.body 中)的元素创建定位器。。

Gets a LocatorFactory for the document root element. This factory can be used to create locators for elements that a component creates outside of its own root element. (e.g. by appending to document.body).

返回值

Returns

LocatorFactory
异步
forceStabilize

刷新 Angular 中的变更检测和异步任务。在大多数情况下,没有必要手动调用它。但是,可能会出现一些需要完全刷新动画事件的边缘情况。

Flushes change detection and async tasks in the Angular zone. In most cases it should not be necessary to call this manually. However, there may be some edge cases where it is needed to fully flush animation events.

异步
host

获取一个代表该组件宿主元素的 TestElementPromise

Gets a Promise for the TestElement representing the host element of the component.

返回值

Returns

Promise<TestElement>
locatorFor

创建可用于查找一个异步定位器函数,它可用于查找此 ComponentHarness 宿主元素下的 ComponentHarness 实例。

Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the host element of this ComponentHarness.

参数

Parameters

queries

一系列查询,用于指定要搜索的测试工具和元素:

A list of queries specifying which harnesses and elements to search for:

  • string 搜索匹配字符串指定的 CSS 选择器的元素。

    A string searches for elements matching the CSS selector specified by the string.

  • ComponentHarness 构造函数会搜索与指定类匹配的 ComponentHarness 实例。

    A ComponentHarness constructor searches for ComponentHarness instances matching the given class.

  • HarnessPredicate 会搜索满足指定谓词的 ComponentHarness 实例。

    A HarnessPredicate searches for ComponentHarness instances matching the given predicate.

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>>

一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 Promise。匹配结果首先按照 DOM 中的顺序排序,然后按查询列表中的顺序排序。如果找不到匹配条目, Promise 会拒绝。 Promise 解析成的类型是每个查询的所有结果类型的并集。

An asynchronous locator function that searches for and returns a Promise for the first element or harness matching the given search criteria. Matches are ordered first by order in the DOM, and second by order in the queries list. If no matches are found, the Promise rejects. The type that the Promise resolves to is a union of all result types for each query.

例如,指定以下 DOM: <div id="d1" /><div id="d2" /> ,并假设 DivHarness.hostSelector === 'div'

e.g. Given the following DOM: <div id="d1" /><div id="d2" />, and assuming DivHarness.hostSelector === 'div':

  • await ch.locatorFor(DivHarness, 'div')() 会得到 #d1DivHarness 实例

    await ch.locatorFor(DivHarness, 'div')() gets a DivHarness instance for #d1

  • await ch.locatorFor('div', DivHarness)() 会得到 #d1TestElement 实例

    await ch.locatorFor('div', DivHarness)() gets a TestElement instance for #d1

  • await ch.locatorFor('span')() 会抛出错误,因为 Promise 拒绝了。

    await ch.locatorFor('span')() throws because the Promise rejects.

locatorForAll

创建异步定位器函数,可用于查找此 ComponentHarness 的宿主元素下的 ComponentHarness 实例或元素。

Creates an asynchronous locator function that can be used to find ComponentHarness instances or elements under the host element of this ComponentHarness.

参数

Parameters

queries

一系列查询,用于指定要搜索的测试工具和元素:

A list of queries specifying which harnesses and elements to search for:

  • string 搜索匹配字符串指定的 CSS 选择器的元素。

    A string searches for elements matching the CSS selector specified by the string.

  • ComponentHarness 构造函数会搜索与指定类匹配的 ComponentHarness 实例。

    A ComponentHarness constructor searches for ComponentHarness instances matching the given class.

  • HarnessPredicate 搜索满足指定谓词的 ComponentHarness 实例。

    A HarnessPredicate searches for ComponentHarness instances matching the given predicate.

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>[]>

一个异步定位器函数,它会搜索和返回所有匹配指定搜索条件的元素和测试工具的 Promise。匹配结果首先按照 DOM 中的顺序排序,然后查询列表中的顺序排序。如果一个元素与多个 ComponentHarness 类匹配,那么该定位器会为同一个元素获取每个元素的实例。如果一个元素匹配多个 string 选择器,只会为该元素返回一个 TestElement 实例。 Promise 解析成的类型是一个数组,其中每个元素都是每个查询的所有结果类型的并集。

An asynchronous locator function that searches for and returns a Promise for all elements and harnesses matching the given search criteria. Matches are ordered first by order in the DOM, and second by order in the queries list. If an element matches more than one ComponentHarness class, the locator gets an instance of each for the same element. If an element matches multiple string selectors, only one TestElement instance is returned for that element. The type that the Promise resolves to is an array where each element is the union of all result types for each query.

例如,指定以下 DOM: <div id="d1" /><div id="d2" /> ,并假设 DivHarness.hostSelector === 'div'IdIsD1Harness.hostSelector === '#d1'

e.g. Given the following DOM: <div id="d1" /><div id="d2" />, and assuming DivHarness.hostSelector === 'div' and IdIsD1Harness.hostSelector === '#d1':

  • await ch.locatorForAll(DivHarness, 'div')() 会得到 [ DivHarness, // 对于 #d1 TestElement, // 对于 #d1 DivHarness, // 对于 #d2 TestElement // 对于 #d2 ]

    await ch.locatorForAll(DivHarness, 'div')() gets [ DivHarness, // for #d1 TestElement, // for #d1 DivHarness, // for #d2 TestElement // for #d2 ]

  • await ch.locatorForAll('div', '#d1')() 会得到 [ TestElement, // 对于 #d1 TestElement // 对于 #d2 ]

    await ch.locatorForAll('div', '#d1')() gets [ TestElement, // for #d1 TestElement // for #d2 ]

  • await ch.locatorForAll(DivHarness, IdIsD1Harness)() 会得到 [ DivHarness, // 对于 #d1 IdIsD1Harness, // 对于 #d1 DivHarness // 对于 #d2 ]

    await ch.locatorForAll(DivHarness, IdIsD1Harness)() gets [ DivHarness, // for #d1 IdIsD1Harness, // for #d1 DivHarness // for #d2 ]

  • await ch.locatorForAll('span')() 会得到 []

    await ch.locatorForAll('span')() gets [].

locatorForOptional

创建一个异步定位器函数,可用于查找此 ComponentHarness 宿主元素下的 ComponentHarness 实例或元素。

Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the host element of this ComponentHarness.

参数

Parameters

queries

一系列查询,用于指定要搜索的测试工具和元素:

A list of queries specifying which harnesses and elements to search for:

  • string 搜索匹配字符串指定的 CSS 选择器的元素。

    A string searches for elements matching the CSS selector specified by the string.

  • ComponentHarness 构造函数会搜索与指定类匹配的 ComponentHarness 实例。

    A ComponentHarness constructor searches for ComponentHarness instances matching the given class.

  • HarnessPredicate 搜索满足指定谓词的 ComponentHarness 实例。

    A HarnessPredicate searches for ComponentHarness instances matching the given predicate.

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T> | null>

一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 Promise。匹配结果首先按照 DOM 中的顺序排序,然后在查询列表中的顺序排序。如果找不到匹配条目, Promise 就会被解析为 nullPromise 解析成的类型是每个查询的所有结果类型的并集,或 null。

An asynchronous locator function that searches for and returns a Promise for the first element or harness matching the given search criteria. Matches are ordered first by order in the DOM, and second by order in the queries list. If no matches are found, the Promise is resolved with null. The type that the Promise resolves to is a union of all result types for each query or null.

例如,指定以下 DOM: <div id="d1" /><div id="d2" /> ,并假设 DivHarness.hostSelector === 'div'

e.g. Given the following DOM: <div id="d1" /><div id="d2" />, and assuming DivHarness.hostSelector === 'div':

  • await ch.locatorForOptional(DivHarness, 'div')() 获取 #d1 DivHarness 实例

    await ch.locatorForOptional(DivHarness, 'div')() gets a DivHarness instance for #d1

  • await ch.locatorForOptional('div', DivHarness)() 获取 #d1 TestElement 实例

    await ch.locatorForOptional('div', DivHarness)() gets a TestElement instance for #d1

  • await ch.locatorForOptional('span')() 得到 null

    await ch.locatorForOptional('span')() gets null.

异步
waitForTasksOutsideAngular

等待所有已安排或正在运行的异步任务完成。这使得测试工具的作者可以等待 Angular 中的异步任务。

Waits for all scheduled or running async tasks to complete. This allows harness authors to wait for async tasks outside of the Angular zone.

作者们应该扩展的组件测试工具的基类,如果他们预计测试工具的消费者可能要在该组件的 <ng-content> 中访问其它测试工具。

Base class for component harnesses that authors should extend if they anticipate that consumers of the harness may want to access other harnesses within the <ng-content> of the component.

方法
异步
getAllChildLoaders

参数

Parameters

selector

S

返回值

Returns

Promise<HarnessLoader[]>
异步
getAllHarnesses

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T[]>
异步
getChildLoader

参数

Parameters

selector

S

返回值

Returns

Promise<HarnessLoader>
异步
getHarness

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T>
异步
getRootHarnessLoader

获取根测试工具加载器,从中开始搜索该测试工具所包含的内容。

Gets the root harness loader from which to start searching for content contained by this harness.

返回值

Returns

Promise<HarnessLoader>
异步
host

获取一个代表该组件宿主元素的 TestElementPromise

Gets a Promise for the TestElement representing the host element of the component.

返回值

Returns

Promise<TestElement>

用于把 ComponentHarness 类与过滤该类实例的谓词函数相关联的类。

A class used to associate a ComponentHarness class with predicates functions that can be used to filter instances of the class.

属性
名称 描述

harnessType: ComponentHarnessConstructor<T>

方法
add

添加谓词函数来筛选候选工具。

Adds a predicate function to be run against candidate harnesses.

参数

Parameters

description

string

该谓词的描述,可以用在错误信息中。

A description of this predicate that may be used in error messages.

predicate

AsyncPredicate<T>

一个异步谓词函数。

An async predicate function.

addOption

添加一个谓词函数,该函数取决于要对候选工具使用的选项值。如果该选项值未定义,那么该谓词就会被忽略。

Adds a predicate function that depends on an option value to be run against candidate harnesses. If the option value is undefined, the predicate will be ignored.

参数

Parameters

name

string

该选项的名字(可以在错误信息中使用)。

The name of the option (may be used in error messages).

option

O

选项的值。

The option value.

predicate

AsyncOptionPredicate<T, O>

如果选项值未定义,则要运行的谓词函数。

The predicate function to run if the option value is not undefined.

异步
evaluate

评估指定的测试工具是否满足这个谓词。

Evaluates whether the given harness satisfies this predicate.

参数

Parameters

harness

T

要检查的测试工具

The harness to check

返回值

Returns

Promise<boolean>

如果测试工具满足这个谓词就会返回一个解析成 true 的 Promise,否则返回解析成 false 的。

A promise that resolves to true if the harness satisfies this predicate, and resolves to false otherwise.

异步
filter

使用此谓词过滤测试工具列表。

Filters a list of harnesses on this predicate.

参数

Parameters

harnesses

T[]

要过滤的测试工具列表。

The list of harnesses to filter.

返回值

Returns

Promise<T[]>

一些满足此谓词的测试工具列表。

A list of harnesses that satisfy this predicate.

getDescription

获取此谓词的描述信息,以供在错误消息中使用。

Gets a description of this predicate for use in error messages.

getSelector

获取用于查找候选元素的选择器。

Gets the selector used to find candidate elements.

可以扩展的基础测试工具环境类,它允许 ComponentHarness 用在不同的测试环境下(比如 testbed,protractor 等)。这个类实现了 HarnessLoaderLocatorFactory 的功能。这个类是原始元素类型 E 的泛型类,供特定测试环境使用。

Base harness environment class that can be extended to allow ComponentHarnesses to be used in different test environments (e.g. testbed, protractor, etc.). This class implements the functionality of both a HarnessLoader and LocatorFactory. This class is generic on the raw element type, E, used by the particular test environment.

属性
名称 描述

rawRootElement: E

rootElement: TestElement

方法
createComponentHarness

使用指定的原始宿主元素为指定的测试工具类型创建一个 ComponentHarness

Creates a ComponentHarness for the given harness type with the given raw host element.

参数

Parameters

harnessType

ComponentHarnessConstructor<T>

element

E

返回值

Returns

T
createEnvironment

创建一个以指定原始元素为根的 HarnessLoader

Creates a HarnessLoader rooted at the given raw element.

参数

Parameters

element

E

返回值

Returns

HarnessEnvironment<E>
createTestElement

从原始元素中创建一个 TestElement

Creates a TestElement from a raw element.

参数

Parameters

element

E

返回值

Returns

TestElement
documentRootLocatorFactory

返回值

Returns

LocatorFactory
异步
forceStabilize

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getAllChildLoaders

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader[]>
异步
getAllHarnesses

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T[]>
异步
getAllRawElements

在这个环境的根元素下,获取与指定选择器匹配的所有元素列表。

Gets a list of all elements matching the given selector under this environment's root element.

参数

Parameters

selector

string

返回值

Returns

Promise<E[]>
异步
getChildLoader

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader>
getDocumentRoot

获取该文档的根元素。

Gets the root element for the document.

返回值

Returns

E
异步
getHarness

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T>
异步
harnessLoaderFor

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader>
异步
harnessLoaderForAll

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader[]>
异步
harnessLoaderForOptional

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader | null>
locatorFor

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>>
locatorForAll

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>[]>
locatorForOptional

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T> | null>
异步
rootHarnessLoader

返回值

Returns

Promise<HarnessLoader>
异步
waitForTasksOutsideAngular

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

用于加载 ComponentHarness 对象的接口。测试的作者可以使用这个接口实例化 ComponentHarness

Interface used to load ComponentHarness objects. This interface is used by test authors to instantiate ComponentHarnesses.

方法
异步
getAllChildLoaders

在当前实例的根元素下搜索具有指定选择器的所有元素,并返回一个以每个匹配的元素为根的 HarnessLoader 数组。

Searches for all elements with the given selector under the current instances's root element, and returns an array of HarnessLoaders, one for each matching element, rooted at that element.

参数

Parameters

selector

string

HarnessLoader 的根元素的选择器

The selector for the root element of the new HarnessLoader

返回值

Returns

Promise<HarnessLoader[]>

HarnessLoader 列表,它是每个匹配元素的列表,并以相应的元素为根。

A list of HarnessLoaders, one for each matching element, rooted at that element.

异步
getAllHarnesses

HarnessLoader 的根元素下搜索与指定的测试工具类型对应的所有组件实例,并为每个实例返回 ComponentHarness

Searches for all instances of the component corresponding to the given harness type under the HarnessLoader's root element, and returns a list ComponentHarness for each instance.

参数

Parameters

query

HarnessQuery<T>

要创建的测试工具的查询

A query for a harness to create

返回值

Returns

Promise<T[]>

指定测试工具类型的列表实例。

A list instances of the given harness type.

异步
getChildLoader

在当前实例的根元素下搜索具有指定选择器的元素,并返回一个以匹配元素为根的 HarnessLoader。如果多个元素与选择器匹配,则使用第一个元素。如果没有符合的元素,就抛出一个错误。

Searches for an element with the given selector under the current instances's root element, and returns a HarnessLoader rooted at the matching element. If multiple elements match the selector, the first is used. If no elements match, an error is thrown.

参数

Parameters

selector

string

HarnessLoader 根元素的选择器

The selector for the root element of the new HarnessLoader

返回值

Returns

Promise<HarnessLoader>

一个根据指定选择器匹配的元素为根的 HarnessLoader

A HarnessLoader rooted at the element matching the given selector.

异步
getHarness

HarnessLoader 的根元素下搜索与指定的测试工具类型对应的组件实例,并返回该实例的 ComponentHarness。如果找到了多个匹配的组件,就会返回第一个匹配的组件。如果找不到匹配的组件,则会抛出错误。

Searches for an instance of the component corresponding to the given harness type under the HarnessLoader's root element, and returns a ComponentHarness for that instance. If multiple matching components are found, a harness for the first one is returned. If no matching component is found, an error is thrown.

参数

Parameters

query

HarnessQuery<T>

要创建的测试工具的查询

A query for a harness to create

返回值

Returns

Promise<T>

指定测试工具类型的一个实例

An instance of the given harness type

用来创建异步定位器函数的接口,用于查找元素和组件测试工具。 这个接口供 ComponentHarness 的作者使用,用于为其 ComponentHarness 子类创建定位器函数。

Interface used to create asynchronous locator functions used find elements and component harnesses. This interface is used by ComponentHarness authors to create locator functions for their ComponentHarness subclass.

属性
名称 描述

rootElement: TestElement

LocatorFactory 的根元素是一个 TestElement

The root element of this LocatorFactory as a TestElement.

方法
documentRootLocatorFactory

获取一个以 document 根为根的定位器工厂。

Gets a locator factory rooted at the document root.

返回值

Returns

LocatorFactory
异步
forceStabilize

刷新在 Angular zone 中捕获的变更检测和异步任务。在大多数情况下,没有必要手动调用它。但是,可能会出现一些需要完全刷新动画事件的边缘情况。

Flushes change detection and async tasks captured in the Angular zone. In most cases it should not be necessary to call this manually. However, there may be some edge cases where it is needed to fully flush animation events.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
harnessLoaderFor

LocatorFactory 的根下为某个元素获取一个 HarnessLoader 实例。

Gets a HarnessLoader instance for an element under the root of this LocatorFactory.

参数

Parameters

selector

string

根元素的选择器。

The selector for the root element.

返回值

Returns

Promise<HarnessLoader>

一个以指定选择器匹配的第一个元素为根的 HarnessLoader

A HarnessLoader rooted at the first element matching the given selector.

异步
harnessLoaderForAll

获取 HarnessLoader 实例的列表,每个实例对应一个匹配的元素。

Gets a list of HarnessLoader instances, one for each matching element.

参数

Parameters

selector

string

根元素的选择器。

The selector for the root element.

返回值

Returns

Promise<HarnessLoader[]>

HarnessLoader 列表,每一条目都以指定选择器匹配的元素为根。

A list of HarnessLoader, one rooted at each element matching the given selector.

异步
harnessLoaderForOptional

LocatorFactory 的根下为一个元素获取 HarnessLoader 实例。

Gets a HarnessLoader instance for an element under the root of this LocatorFactory

参数

Parameters

selector

string

根元素的选择器。

The selector for the root element.

返回值

Returns

Promise<HarnessLoader | null>

一个以指定选择器匹配的第一个元素为根的 HarnessLoader 实例,如果没有找到匹配的元素则为 null。

A HarnessLoader rooted at the first element matching the given selector, or null if no matching element is found.

locatorFor

创建一个异步定位器函数,用于在这个 LocatorFactory 的根元素下查找 ComponentHarness 实例或元素。

Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the root element of this LocatorFactory.

参数

Parameters

queries

一系列查询,用于指定要搜索的测试工具和元素:

A list of queries specifying which harnesses and elements to search for:

  • string 搜索匹配字符串指定的 CSS 选择器的元素。

    A string searches for elements matching the CSS selector specified by the string.

  • ComponentHarness 构造函数会搜索与指定类匹配的 ComponentHarness 实例

    A ComponentHarness constructor searches for ComponentHarness instances matching the given class.

  • HarnessPredicate 搜索满足指定谓词的 ComponentHarness 实例

    A HarnessPredicate searches for ComponentHarness instances matching the given predicate.

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>>

一个异步定位器函数,用于搜索并返回满足指定搜索条件的第一个元素或测试工具的 Promise。匹配结果首先按照 DOM 中的顺序排序,然后按在查询列表中的顺序排序。如果找不到匹配条目, Promise 就会拒绝(reject)。 Promise 解析(resolve)成的类型是每个查询的所有结果类型的并集。

An asynchronous locator function that searches for and returns a Promise for the first element or harness matching the given search criteria. Matches are ordered first by order in the DOM, and second by order in the queries list. If no matches are found, the Promise rejects. The type that the Promise resolves to is a union of all result types for each query.

例如,指定以下 DOM: <div id="d1" /><div id="d2" /> ,并假设 DivHarness.hostSelector === 'div'

e.g. Given the following DOM: <div id="d1" /><div id="d2" />, and assuming DivHarness.hostSelector === 'div':

  • await lf.locatorFor(DivHarness, 'div')() 会得到 #d1DivHarness 实例

    await lf.locatorFor(DivHarness, 'div')() gets a DivHarness instance for #d1

  • await lf.locatorFor('div', DivHarness)() 会得到 #d1TestElement 实例

    await lf.locatorFor('div', DivHarness)() gets a TestElement instance for #d1

  • await lf.locatorFor('span')() 会抛出错误,因为 Promise 拒绝了。

    await lf.locatorFor('span')() throws because the Promise rejects.

locatorForAll

创建一个异步定位器函数,用于查找 LocatorFactory 根元素下的 ComponentHarness 实例或元素。

Creates an asynchronous locator function that can be used to find ComponentHarness instances or elements under the root element of this LocatorFactory.

参数

Parameters

queries

一系列查询,用于指定要搜索的测试工具和元素:

A list of queries specifying which harnesses and elements to search for:

  • string 搜索匹配字符串指定的 CSS 选择器的元素。

    A string searches for elements matching the CSS selector specified by the string.

  • ComponentHarness 构造函数会搜索与指定类匹配的 ComponentHarness 实例。

    A ComponentHarness constructor searches for ComponentHarness instances matching the given class.

  • HarnessPredicate 搜索满足指定谓词的 ComponentHarness 实例。

    A HarnessPredicate searches for ComponentHarness instances matching the given predicate.

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>[]>

一个异步定位器函数,用于搜索并返回满足指定搜索条件的第一个元素或测试工具的 Promise。匹配结果首先按照 DOM 中的顺序排序,然后按在查询列表中的顺序排序。如果一个元素与多个 ComponentHarness 类匹配,那么该定位器会为同一个元素获取每个元素的实例。如果一个元素匹配多个 string 选择器,只会为它返回一个 TestElement 实例。Promise 解析成的类型是一个数组,其中每个元素都是每个查询的所有结果类型的并集。

An asynchronous locator function that searches for and returns a Promise for all elements and harnesses matching the given search criteria. Matches are ordered first by order in the DOM, and second by order in the queries list. If an element matches more than one ComponentHarness class, the locator gets an instance of each for the same element. If an element matches multiple string selectors, only one TestElement instance is returned for that element. The type that the Promise resolves to is an array where each element is the union of all result types for each query.

例如,指定以下 DOM: <div id="d1" /><div id="d2" /> ,并假设 DivHarness.hostSelector === 'div'IdIsD1Harness.hostSelector === '#d1'

e.g. Given the following DOM: <div id="d1" /><div id="d2" />, and assuming DivHarness.hostSelector === 'div' and IdIsD1Harness.hostSelector === '#d1':

  • await lf.locatorForAll(DivHarness, 'div')() 会得到 [ DivHarness, // 对于 #d1 TestElement, // 对于 #d1 DivHarness, // 对于 #d2 TestElement // 对于 #d2 ]

    await lf.locatorForAll(DivHarness, 'div')() gets [ DivHarness, // for #d1 TestElement, // for #d1 DivHarness, // for #d2 TestElement // for #d2 ]

  • await lf.locatorForAll('div', '#d1')() 会得到 [ TestElement, // 对于 #d1 TestElement // 对于 #d2 ]

    await lf.locatorForAll('div', '#d1')() gets [ TestElement, // for #d1 TestElement // for #d2 ]

  • await lf.locatorForAll(DivHarness, IdIsD1Harness)() 会得到 [ DivHarness, // 对于 #d1 IdIsD1Harness, // 对于 #d1 DivHarness // 对于 #d2 ]

    await lf.locatorForAll(DivHarness, IdIsD1Harness)() gets [ DivHarness, // for #d1 IdIsD1Harness, // for #d1 DivHarness // for #d2 ]

  • await lf.locatorForAll('span')() 会得到 []

    await lf.locatorForAll('span')() gets [].

locatorForOptional

创建一个异步定位器函数,用于查找 LocatorFactory 根元素下的 ComponentHarness 实例或元素。

Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the root element of this LocatorFactory.

参数

Parameters

queries

一系列查询,用于指定要搜索的测试工具和元素:

A list of queries specifying which harnesses and elements to search for:

  • string 搜索满足此字符串指定的 CSS 选择器的元素。

    A string searches for elements matching the CSS selector specified by the string.

  • ComponentHarness 构造函数会搜索与指定类匹配的 ComponentHarness 实例。

    A ComponentHarness constructor searches for ComponentHarness instances matching the given class.

  • HarnessPredicate 搜索满足指定谓词的 ComponentHarness 实例。

    A HarnessPredicate searches for ComponentHarness instances matching the given predicate.

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T> | null>

一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 Promise。匹配结果首先按照 DOM 中的顺序排序,然后按查询列表中的顺序排序。如果找不到匹配条目, Promise 就会被解析为 nullPromise 解析成的类型是每个查询的所有结果类型的并集,或 null。

An asynchronous locator function that searches for and returns a Promise for the first element or harness matching the given search criteria. Matches are ordered first by order in the DOM, and second by order in the queries list. If no matches are found, the Promise is resolved with null. The type that the Promise resolves to is a union of all result types for each query or null.

例如,指定以下 DOM: <div id="d1" /><div id="d2" /> ,并假设 DivHarness.hostSelector === 'div'

e.g. Given the following DOM: <div id="d1" /><div id="d2" />, and assuming DivHarness.hostSelector === 'div':

  • await lf.locatorForOptional(DivHarness, 'div')() 会得到 #d1DivHarness 实例

    await lf.locatorForOptional(DivHarness, 'div')() gets a DivHarness instance for #d1

  • await lf.locatorForOptional('div', DivHarness)() 会获取 #d1TestElement 实例

    await lf.locatorForOptional('div', DivHarness)() gets a TestElement instance for #d1

  • await lf.locatorForOptional('span')() 会得到 null

    await lf.locatorForOptional('span')() gets null.

异步
rootHarnessLoader

返回值

Returns

Promise<HarnessLoader>

一个以此 LocatorFactory 的根元素为根的 HarnessLoader

A HarnessLoader rooted at the root element of this LocatorFactory.

异步
waitForTasksOutsideAngular

等待所有已安排或正在运行的异步任务完成。这使得测试工具的作者可以等待 Angular 中的异步任务。

Waits for all scheduled or running async tasks to complete. This allows harness authors to wait for async tasks outside of the Angular zone.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

ComponentHarness 子类的构造方法。

Constructor for a ComponentHarness subclass.

属性
名称 描述

hostSelector: string

ComponentHarness 子类必须指定一个静态的 hostSelector 属性,用于查找对应组件的宿主元素。该属性应与 Angular 组件的选择器匹配。

ComponentHarness subclasses must specify a static hostSelector property that is used to find the host element for the corresponding component. This property should match the selector for the Angular component.

方法
new

参数

Parameters

locatorFactory

LocatorFactory

返回值

Returns

T

一组可以用来过滤 ComponentHarness 实例列表的条件。

A set of criteria that can be used to filter a list of ComponentHarness instances.

属性
名称 描述

ancestor: string

只查找嵌套在具有指定选择器的元素下的实例。

Only find instances that are nested under an element with the given selector.

selector: string

只查找那些宿主元素与指定选择器匹配的实例。

Only find instances whose host element matches the given selector.

打字时可能会按住的修饰键。

Modifier keys that may be held while typing.

属性
名称 描述

alt: boolean

control: boolean

meta: boolean

shift: boolean

这可以作为单元和 e2e 测试中 DOM 元素的通用接口。它是 ComponentHarness 与组件 DOM 交互的接口。

This acts as a common interface for DOM elements across both unit and e2e tests. It is the interface through which the ComponentHarness interacts with the component's DOM.

方法
异步
blur

让元素失去焦点。

Blur the element.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
clear

清除元素的输入(只适用于 input 和 textarea 元素)。

Clear the element's input (for input and textarea elements only).

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
click

单击当前环境下位于默认位置的元素。 如果你需要保证在某个特定的位置单击该元素,可以考虑 click('center')click(x, y)

Click the element at the default location for the current environment. If you need to guarantee the element is clicked at a specific location, consider using click('center') or click(x, y) instead.

参数

Parameters

modifiers?

ModifierKeys

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
dispatchEvent

派发具有特定名称的事件。

Dispatches an event with a particular name.

参数

Parameters

name

string

要派发的事件名称。

Name of the event to be dispatched.

data?

Record<string, EventData>

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
focus

让此元素获得焦点。

Focus the element.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getAttribute

从元素中获取指定 attribute 的值。

Gets the value for the given attribute from the element.

参数

Parameters

name

string

返回值

Returns

Promise<string | null>
异步
getCssValue

获取该元素的指定 CSS 属性的计算值。

Get the computed value of the given CSS property for the element.

参数

Parameters

property

string

返回值

Returns

Promise<string>
异步
getDimensions

获取该元素的规格。

Gets the dimensions of the element.

返回值

Returns

Promise<ElementDimensions>
异步
getProperty

获取一个 element 的 property 的值。

Gets the value of a property of an element.

参数

Parameters

name

string

返回值

Returns

Promise<any>
异步
hasClass

检查元素是否具有指定的类。

Checks whether the element has the given class.

参数

Parameters

name

string

返回值

Returns

Promise<boolean>
异步
hover

将鼠标悬停在元素上方。

Hovers the mouse over the element.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
isFocused

检查该元素是否拥有焦点。

Checks whether the element is focused.

返回值

Returns

Promise<boolean>
异步
matchesSelector

检查此元素是否与指定的选择器匹配。

Checks whether this element matches the given selector.

参数

Parameters

selector

string

返回值

Returns

Promise<boolean>
异步
mouseAway

让鼠标远离元素。

Moves the mouse away from the element.

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
rightClick

在相对于该元素左上角的指定坐标处右键单击它。

Right clicks on the element at the specified coordinates relative to the top-left of it.

参数

Parameters

relativeX

number

元素内部的相对坐标 x。

Coordinate within the element, along the X-axis at which to click.

relativeY

number

元素内部的相对坐标 y。

Coordinate within the element, along the Y-axis at which to click.

modifiers?

ModifierKeys

单击时会按住的修饰键

Modifier keys held while clicking

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
selectOptions

选择 select 元素中指定索引处的选项。

Selects the options at the specified indexes inside of a native select element.

参数

Parameters

...optionIndexes

number[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
sendKeys

以按键序列的形式,把指定的字符串发送给输入设备。同时触发输入事件,并尝试将该字符串添加到 Element 的值中。

Sends the given string to the input as a series of key presses. Also fires input events and attempts to add the string to the Element's value.

参数

Parameters

...keys

(string | TestKey)[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
setInputValue

设置输入框的属性值。

Sets the value of a property of an input.

参数

Parameters

value

string

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
text

从元素中获取文本。

Gets the text from the element.

参数

Parameters

options?

TextOptions

影响要包含哪些文本的选项。

Options that affect what text is included.

返回值

Returns

Promise<string>
属性
名称 描述

exclude: string

(可选)要排除的元素的选择器。

Optional selector for elements to exclude.

元素规格及其相对于视口的位置。

Dimensions for element size and its position relative to the viewport.

属性
名称 描述

height: number

left: number

top: number

width: number

表示自动变更检测的状态。

Represents the status of auto change detection.

属性
名称 描述

isDisabled: boolean

是否禁用了自动变更检测功能。

Whether auto change detection is disabled.

onDetectChangesNow: () => void

一个可选的回调函数(如果有的话)表明在处理状态变化时,是否应该立即运行变更检测。变量检测一旦完成就应该调用此回调函数。

An optional callback, if present it indicates that change detection should be run immediately, while handling the status change. The callback should then be called as soon as change detection is done.

handleAutoChangeDetectionStatus

允许测试 HarnessEnvironment 安装自己的处理程序,以便自动更改变更检测状态。

Allows a test HarnessEnvironment to install its own handler for auto change detection status changes.

参数

Parameters

handler

(status

自动变更检测状态的处理程序。

The handler for the auto change detection status.

stopHandlingAutoChangeDetectionStatus

允许 HarnessEnvironment 停止自动处理变更检测的状态变化。

Allows a HarnessEnvironment to stop handling auto change detection status changes.

异步
manualChangeDetection

在指定函数的持续时间内,禁用测试工具体系的自动变更检测功能。

Disables the harness system's auto change detection for the duration of the given function.

参数

Parameters

fn

() => Promise<T>

禁用自动变更检测的功能。

The function to disable auto change detection for.

异步
parallel

参数

Parameters

values

() => any

返回值

Returns

Promise<T[]>

一个异步函数,调用时返回一个 Promise。

An async function that returns a promise when called.

type AsyncFactoryFn = () => Promise<T>;

一个异步函数,它接受一个 item 并返回一个布尔型 Promise

An async function that takes an item and returns a boolean promise

type AsyncPredicate = (item: T) => Promise<boolean>;

一个异步函数,它接受一个 item 和一个 option 值,并返回一个布尔型 Promise。

An async function that takes an item and an option value and returns a boolean promise.

type AsyncOptionPredicate = (item: T, option: O) => Promise<boolean>;

ComponentHarness 的查询,它表示为一个 ComponentHarnessConstructorHarnessPredicate

A query for a ComponentHarness, which is expressed as either a ComponentHarnessConstructor or a HarnessPredicate.

type HarnessQuery = ComponentHarnessConstructor<T> | HarnessPredicate<T>;

在使用特定查询列表进行搜索时得到的结果类型。这个类型取决于被查询的特定条目。

The result type obtained when searching using a particular list of queries. This type depends on the particular items being queried.

  • 如果其中一个查询是针对 ComponentHarnessConstructor<C1> 的,那就意味着结果可能是 C1

    If one of the queries is for a ComponentHarnessConstructor<C1>, it means that the result might be a harness of type C1

  • 如果其中一个查询是针对 HarnessPredicate<C2> 的,那就意味着该结果可能是 C2

    If one of the queries is for a HarnessPredicate<C2>, it means that the result might be a harness of type C2

  • 如果其中一个查询是针对某个 string,那就意味着该结果可能是一个 TestElement

    If one of the queries is for a string, it means that the result might be a TestElement.

由于我们不能确定哪个查询匹配,所以结果类型就是所有可能结果类型的并集。

Since we don't know for sure which query will match, the result type if the union of the types for all possible results.

例如,类型: LocatorFnResult&lt;[ ComponentHarnessConstructor&lt;MyHarness&gt;, HarnessPredicate&lt;MyOtherHarness&gt;, string ]&gt; 相当于: MyHarness | MyOtherHarness | TestElement.

e.g. The type: LocatorFnResult&lt;[ ComponentHarnessConstructor&lt;MyHarness&gt;, HarnessPredicate&lt;MyOtherHarness&gt;, string ]&gt; is equivalent to: MyHarness | MyOtherHarness | TestElement.

type LocatorFnResult = {
    [I in keyof T]: T[I] extends new (...args: any[]) => infer C ? C : T[I] extends {
        harnessType: new (...args: any[]) => infer C;
    } ? C : T[I] extends string ? TestElement : never;
}[number];

TestElement 派发的自定义事件可以携带的数据。

Data that can be attached to a custom event dispatched from a TestElement.

type EventData = string | number | boolean | undefined | null | EventData[] | {
    [key: string]: EventData;
};

Angular CDK testing-testbed API 参考文档

import {} from '@angular/cdk/testing/testbed';

A HarnessEnvironment implementation for Angular's Testbed.

属性
名称 描述

rootElement: TestElement

方法
createEnvironment

参数

Parameters

element

Element

返回值

Returns

HarnessEnvironment<Element>
createTestElement

参数

Parameters

element

Element

返回值

Returns

TestElement
documentRootLocatorFactory

返回值

Returns

LocatorFactory
异步
forceStabilize

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getAllChildLoaders

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader[]>
异步
getAllHarnesses

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T[]>
异步
getAllRawElements

参数

Parameters

selector

string

返回值

Returns

Promise<Element[]>
异步
getChildLoader

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader>
getDocumentRoot

返回值

Returns

Element
异步
getHarness

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T>
异步
harnessLoaderFor

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader>
异步
harnessLoaderForAll

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader[]>
异步
harnessLoaderForOptional

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader | null>
locatorFor

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>>
locatorForAll

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>[]>
locatorForOptional

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T> | null>
异步
rootHarnessLoader

返回值

Returns

Promise<HarnessLoader>
异步
waitForTasksOutsideAngular

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

A TestElement implementation for unit tests.

属性
名称 描述

element: Element

方法
异步
blur

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
clear

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
click

参数

Parameters

...args

[ModifierKeys?] | ["center", ModifierKeys?] | [number, number, ModifierKeys?]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
dispatchEvent

参数

Parameters

name

string

data?

Record<string, EventData>

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
focus

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getAttribute

参数

Parameters

name

string

返回值

Returns

Promise<string | null>
异步
getCssValue

参数

Parameters

property

string

返回值

Returns

Promise<string>
异步
getDimensions

返回值

Returns

Promise<ElementDimensions>
异步
getProperty

参数

Parameters

name

string

返回值

Returns

Promise<any>
异步
hasClass

参数

Parameters

name

string

返回值

Returns

Promise<boolean>
异步
hover

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
isFocused

返回值

Returns

Promise<boolean>
异步
matchesSelector

参数

Parameters

selector

string

返回值

Returns

Promise<boolean>
异步
mouseAway

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
rightClick

参数

Parameters

...args

[ModifierKeys?] | ["center", ModifierKeys?] | [number, number, ModifierKeys?]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
selectOptions

参数

Parameters

...optionIndexes

number[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
sendKeys

参数

Parameters

...keys

(string | TestKey)[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
sendKeys

参数

Parameters

modifiers

ModifierKeys

...keys

(string | TestKey)[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
setInputValue

参数

Parameters

value

string

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
text

参数

Parameters

options?

TextOptions

返回值

Returns

Promise<string>

Options to configure the environment.

属性
名称 描述

queryFn: (selector: string, root: Element) => Iterable<Element> | ArrayLike<Element>

The query function used to find DOM elements.

Angular CDK testing-protractor API 参考文档

import {} from '@angular/cdk/testing/protractor';

用于 Protractor 的 TestElement

A TestElement implementation for Protractor.

属性
名称 描述

element: ElementFinder

方法
异步
blur

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
clear

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
click

参数

Parameters

...args

[ModifierKeys?] | ["center", ModifierKeys?] | [number, number, ModifierKeys?]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
dispatchEvent

参数

Parameters

name

string

data?

Record<string, EventData>

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
focus

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getAttribute

参数

Parameters

name

string

返回值

Returns

Promise<string | null>
异步
getCssValue

参数

Parameters

property

string

返回值

Returns

Promise<string>
异步
getDimensions

返回值

Returns

Promise<ElementDimensions>
异步
getProperty

参数

Parameters

name

string

返回值

Returns

Promise<any>
异步
hasClass

参数

Parameters

name

string

返回值

Returns

Promise<boolean>
异步
hover

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
isFocused

返回值

Returns

Promise<boolean>
异步
matchesSelector

参数

Parameters

selector

string

返回值

Returns

Promise<boolean>
异步
mouseAway

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
rightClick

参数

Parameters

...args

[ModifierKeys?] | ["center", ModifierKeys?] | [number, number, ModifierKeys?]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
selectOptions

参数

Parameters

...optionIndexes

number[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
sendKeys

参数

Parameters

...keys

(string | TestKey)[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
sendKeys

参数

Parameters

modifiers

ModifierKeys

...keys

(string | TestKey)[]

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
setInputValue

参数

Parameters

value

string

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
text

参数

Parameters

options?

TextOptions

返回值

Returns

Promise<string>

Protractor 的 HarnessEnvironment 实现。

A HarnessEnvironment implementation for Protractor.

属性
名称 描述

rootElement: TestElement

方法
createEnvironment

参数

Parameters

element

any

返回值

Returns

HarnessEnvironment<ElementFinder>
createTestElement

参数

Parameters

element

any

返回值

Returns

TestElement
documentRootLocatorFactory

返回值

Returns

LocatorFactory
异步
forceStabilize

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

异步
getAllChildLoaders

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader[]>
异步
getAllHarnesses

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T[]>
异步
getAllRawElements

参数

Parameters

selector

string

返回值

Returns

Promise<ElementFinder[]>
异步
getChildLoader

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader>
getDocumentRoot

返回值

Returns

ElementFinder
异步
getHarness

参数

Parameters

query

HarnessQuery<T>

返回值

Returns

Promise<T>
异步
harnessLoaderFor

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader>
异步
harnessLoaderForAll

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader[]>
异步
harnessLoaderForOptional

参数

Parameters

selector

string

返回值

Returns

Promise<HarnessLoader | null>
locatorFor

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>>
locatorForAll

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T>[]>
locatorForOptional

参数

Parameters

...queries

T

返回值

Returns

AsyncFactoryFn<LocatorFnResult<T> | null>
异步
rootHarnessLoader

返回值

Returns

Promise<HarnessLoader>
异步
waitForTasksOutsideAngular

返回值

Returns

Promise<void>

Promise that resolves when the action completes.

用于配置环境的选项。

Options to configure the environment.

属性
名称 描述

queryFn: (selector: string, root: ElementFinder) => ElementArrayFinder

用于查找 DOM 元素的查询功能。

The query function used to find DOM elements.