Angular CDK testing API 参考文档
import {ContentContainerComponentHarness} from '@angular/cdk/testing';
类
ComponentHarness
所有组件工具都应该扩展的组件测试工具的基类。这个基础组件工具提供了定位元素和子组件工具的基本能力。它应该在用户定义自己的测试工具时继承。
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.
属性
名称 | 描述 |
---|---|
|
方法
documentRootLocatorFactory | |
---|---|
获取 document 根元素的 Gets a |
|
返回值 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
|
|
---|---|
获取一个代表该组件宿主元素的 Gets a |
|
返回值 Returns |
|
Promise<TestElement>
|
|
locatorFor | |
---|---|
创建可用于查找一个异步定位器函数,它可用于查找此 Creates an asynchronous locator function that can be used to find a 一系列查询,用于指定要搜索的测试工具和元素: @param queries A list of queries specifying which harnesses and elements to search for:
一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 @return An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
|
参数 Parameters |
|
queries
|
|
...queries T
|
|
返回值 Returns |
|
AsyncFactoryFn<LocatorFnResult<T>>
|
一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
locatorForAll | |
---|---|
创建异步定位器函数,可用于查找此 Creates an asynchronous locator function that can be used to find 一系列查询,用于指定要搜索的测试工具和元素: @param queries A list of queries specifying which harnesses and elements to search for:
一个异步定位器函数,它会搜索和返回所有匹配指定搜索条件的元素和测试工具的 @return An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
|
参数 Parameters |
|
queries
|
|
...queries T
|
|
返回值 Returns |
|
AsyncFactoryFn<LocatorFnResult<T>[]>
|
一个异步定位器函数,它会搜索和返回所有匹配指定搜索条件的元素和测试工具的 An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
locatorForOptional | |
---|---|
创建一个异步定位器函数,可用于查找此 Creates an asynchronous locator function that can be used to find a 一系列查询,用于指定要搜索的测试工具和元素: @param queries A list of queries specifying which harnesses and elements to search for:
一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 @return An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
|
参数 Parameters |
|
queries
|
|
...queries T
|
|
返回值 Returns |
|
AsyncFactoryFn<LocatorFnResult<T> | null>
|
一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
异步
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. |
ContentContainerComponentHarness
extends
ComponentHarness
作者们应该扩展的组件测试工具的基类,如果他们预计测试工具的消费者可能要在该组件的 <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
|
|
---|---|
获取一个代表该组件宿主元素的 Gets a |
|
返回值 Returns |
|
Promise<TestElement>
|
|
HarnessPredicate
用于把 ComponentHarness 类与过滤该类实例的谓词函数相关联的类。
A class used to associate a ComponentHarness class with predicates functions that can be used to filter instances of the class.
属性
名称 | 描述 |
---|---|
|
方法
add | |
---|---|
添加谓词函数来筛选候选工具。 Adds a predicate function to be run against candidate harnesses. 该谓词的描述,可以用在错误信息中。 @param description A description of this predicate that may be used in error messages. 一个异步谓词函数。 @param predicate An async predicate function. this(用于支持方法的链式调用)。 @return this (for method chaining). |
|
参数 Parameters |
|
description string
|
|
predicate AsyncPredicate<T>
|
|
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. 该选项的名字(可以在错误信息中使用)。 @param name The name of the option (may be used in error messages). 选项的值。 @param option The option value. 如果选项值未定义,则要运行的谓词函数。 @param predicate The predicate function to run if the option value is not undefined. this(用于支持方法的链式调用)。 @return this (for method chaining). |
|
参数 Parameters |
|
name string
|
|
option O
|
|
predicate AsyncOptionPredicate<T, O>
|
|
异步
evaluate
|
|
---|---|
评估指定的测试工具是否满足这个谓词。 Evaluates whether the given harness satisfies this predicate. 要检查的测试工具 @param harness The harness to check 如果测试工具满足这个谓词就会返回一个解析成 true 的 Promise,否则返回解析成 false 的。 @return A promise that resolves to true if the harness satisfies this predicate, and resolves to false otherwise. |
|
参数 Parameters |
|
harness T
|
|
返回值 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. 要过滤的测试工具列表。 @param harnesses The list of harnesses to filter. 一些满足此谓词的测试工具列表。 @return A list of harnesses that satisfy this predicate. |
|
参数 Parameters |
|
harnesses T[]
|
|
返回值 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. |
HarnessEnvironment
可以扩展的基础测试工具环境类,它允许 ComponentHarness
用在不同的测试环境下(比如 testbed,protractor 等)。这个类实现了 HarnessLoader
和 LocatorFactory
的功能。这个类是原始元素类型 E
的泛型类,供特定测试环境使用。
Base harness environment class that can be extended to allow ComponentHarness
es 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.
属性
名称 | 描述 |
---|---|
|
|
|
方法
createComponentHarness | |
---|---|
使用指定的原始宿主元素为指定的测试工具类型创建一个 Creates a |
|
参数 Parameters |
|
harnessType ComponentHarnessConstructor<T>
|
|
element E
|
|
返回值 Returns |
|
T
|
|
createEnvironment | |
---|---|
创建一个以指定原始元素为根的 Creates a |
|
参数 Parameters |
|
element E
|
|
返回值 Returns |
|
HarnessEnvironment<E>
|
|
createTestElement | |
---|---|
从原始元素中创建一个 Creates a |
|
参数 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. |
接口
HarnessLoader
用于加载 ComponentHarness 对象的接口。测试的作者可以使用这个接口实例化 ComponentHarness
。
Interface used to load ComponentHarness objects. This interface is used by test authors to
instantiate ComponentHarness
es.
方法
异步
getAllChildLoaders
|
|
---|---|
在当前实例的根元素下搜索具有指定选择器的所有元素,并返回一个以每个匹配的元素为根的 Searches for all elements with the given selector under the current instances's root element,
and returns an array of 新 @param selector The selector for the root element of the new
@return A list of |
|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader[]>
|
A list of |
异步
getAllHarnesses
|
|
---|---|
在 Searches for all instances of the component corresponding to the given harness type under the
要创建的测试工具的查询 @param query A query for a harness to create 指定测试工具类型的列表实例。 @return A list instances of the given harness type. |
|
参数 Parameters |
|
query HarnessQuery<T>
|
|
返回值 Returns |
|
Promise<T[]>
|
指定测试工具类型的列表实例。 A list instances of the given harness type. |
异步
getChildLoader
|
|
---|---|
在当前实例的根元素下搜索具有指定选择器的元素,并返回一个以匹配元素为根的 Searches for an element with the given selector under the current instances's root element,
and returns a
@param selector The selector for the root element of the new 一个根据指定选择器匹配的元素为根的 @return A 是否未找到匹配的元素。 @throws If a matching element can't be found. |
|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader>
|
一个根据指定选择器匹配的元素为根的 A |
异步
getHarness
|
|
---|---|
在 Searches for an instance of the component corresponding to the given harness type under the
要创建的测试工具的查询 @param query A query for a harness to create 指定测试工具类型的一个实例 @return An instance of the given harness type 如果匹配的组件实例无法找到。 @throws If a matching component instance can't be found. |
|
参数 Parameters |
|
query HarnessQuery<T>
|
|
返回值 Returns |
|
Promise<T>
|
指定测试工具类型的一个实例 An instance of the given harness type |
LocatorFactory
用来创建异步定位器函数的接口,用于查找元素和组件测试工具。 这个接口供 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.
属性
名称 | 描述 |
---|---|
|
The root element of this |
方法
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
|
|
---|---|
从 Gets a 根元素的选择器。 @param selector The selector for the root element. 一个以指定选择器匹配的第一个元素为根的 @return A 是否找不到匹配指定选择器的元素。 @throws If no matching element is found for the given selector. |
|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader>
|
一个以指定选择器匹配的第一个元素为根的 A |
异步
harnessLoaderForAll
|
|
---|---|
获取 Gets a list of 根元素的选择器。 @param selector The selector for the root element.
@return A list of |
|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader[]>
|
A list of |
异步
harnessLoaderForOptional
|
|
---|---|
在 Gets a 根元素的选择器。 @param selector The selector for the root element. 一个以指定选择器匹配的第一个元素为根的 @return A |
|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader | null>
|
一个以指定选择器匹配的第一个元素为根的 A |
locatorFor | |
---|---|
创建一个异步定位器函数,用于在这个 Creates an asynchronous locator function that can be used to find a 一系列查询,用于指定要搜索的测试工具和元素: @param queries A list of queries specifying which harnesses and elements to search for:
一个异步定位器函数,用于搜索并返回满足指定搜索条件的第一个元素或测试工具的 例如,指定以下 DOM: e.g. Given the following DOM:
|
|
参数 Parameters |
|
queries
|
|
...queries T
|
|
返回值 Returns |
|
AsyncFactoryFn<LocatorFnResult<T>>
|
一个异步定位器函数,用于搜索并返回满足指定搜索条件的第一个元素或测试工具的 An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
locatorForAll | |
---|---|
创建一个异步定位器函数,用于查找 Creates an asynchronous locator function that can be used to find 一系列查询,用于指定要搜索的测试工具和元素: @param queries A list of queries specifying which harnesses and elements to search for:
一个异步定位器函数,用于搜索并返回满足指定搜索条件的第一个元素或测试工具的 @return An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
|
参数 Parameters |
|
queries
|
|
...queries T
|
|
返回值 Returns |
|
AsyncFactoryFn<LocatorFnResult<T>[]>
|
一个异步定位器函数,用于搜索并返回满足指定搜索条件的第一个元素或测试工具的 An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
locatorForOptional | |
---|---|
创建一个异步定位器函数,用于查找 Creates an asynchronous locator function that can be used to find a 一系列查询,用于指定要搜索的测试工具和元素: @param queries A list of queries specifying which harnesses and elements to search for:
一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 @return An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
|
参数 Parameters |
|
queries
|
|
...queries T
|
|
返回值 Returns |
|
AsyncFactoryFn<LocatorFnResult<T> | null>
|
一个异步定位器函数,用于搜索和返回与指定搜索条件匹配的第一个元素或测试工具的 An asynchronous locator function that searches for and returns a 例如,指定以下 DOM: e.g. Given the following DOM:
|
异步
rootHarnessLoader
|
|
---|---|
一个以此 @return A |
|
返回值 Returns |
|
Promise<HarnessLoader>
|
一个以此 A |
异步
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. |
ComponentHarnessConstructor
ComponentHarness 子类的构造方法。
Constructor for a ComponentHarness subclass.
属性
名称 | 描述 |
---|---|
|
|
方法
new | |
---|---|
参数 Parameters |
|
locatorFactory LocatorFactory
|
|
返回值 Returns |
|
T
|
|
BaseHarnessFilters
一组可以用来过滤 ComponentHarness
实例列表的条件。
A set of criteria that can be used to filter a list of ComponentHarness
instances.
属性
名称 | 描述 |
---|---|
|
只查找嵌套在具有指定选择器的元素下的实例。 Only find instances that are nested under an element with the given selector. |
|
只查找那些宿主元素与指定选择器匹配的实例。 Only find instances whose host element matches the given selector. |
ModifierKeys
打字时可能会按住的修饰键。
Modifier keys that may be held while typing.
属性
名称 | 描述 |
---|---|
|
|
|
|
|
|
|
TestElement
这可以作为单元和 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 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 |
|
参数 Parameters |
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
dispatchEvent
|
|
---|---|
派发具有特定名称的事件。 Dispatches an event with a particular name. 要派发的事件名称。 @param name Name of the event to be dispatched. |
|
参数 Parameters |
|
name string
|
|
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. 元素内部的相对坐标 x。 @param relativeX Coordinate within the element, along the X-axis at which to click. 元素内部的相对坐标 y。 @param relativeY Coordinate within the element, along the Y-axis at which to click. 单击时按住的修饰键 @param modifiers Modifier keys held while clicking |
|
参数 Parameters |
|
relativeX number
|
|
relativeY number
|
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
selectOptions
|
|
---|---|
选择 Selects the options at the specified indexes inside of a native |
|
参数 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. 影响要包含哪些文本的选项。 @param options Options that affect what text is included. |
|
参数 Parameters |
|
options? TextOptions
|
|
返回值 Returns |
|
Promise<string>
|
|
TextOptions
属性
名称 | 描述 |
---|---|
|
(可选)要排除的元素的选择器。 Optional selector for elements to exclude. |
ElementDimensions
元素规格及其相对于视口的位置。
Dimensions for element size and its position relative to the viewport.
属性
名称 | 描述 |
---|---|
|
|
|
|
|
|
|
AutoChangeDetectionStatus
表示自动变更检测的状态。
Represents the status of auto change detection.
属性
名称 | 描述 |
---|---|
|
是否禁用了自动变更检测功能。 Whether auto change detection is disabled. |
|
一个可选的回调函数(如果有的话)表明在处理状态变化时,是否应该立即运行变更检测。变量检测一旦完成就应该调用此回调函数。 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 | |
---|---|
允许测试 Allows a test 自动变更检测状态的处理程序。 @param handler The handler for the auto change detection status. |
|
参数 Parameters |
|
handler (status
|
|
stopHandlingAutoChangeDetectionStatus | |
---|---|
允许 Allows a |
异步
manualChangeDetection
|
|
---|---|
在指定函数的持续时间内,禁用测试工具体系的自动变更检测功能。 Disables the harness system's auto change detection for the duration of the given function. 禁用自动变更检测的功能。 @param fn The function to disable auto change detection for. 指定函数的结果。 @return The result of the given function. |
|
参数 Parameters |
|
fn () => Promise<T>
|
|
异步
parallel
|
|
---|---|
参数 Parameters |
|
values () => any
|
|
返回值 Returns |
|
Promise<T[]>
|
|
类型别名
AsyncFactoryFn
一个异步函数,调用时返回一个 Promise。
An async function that returns a promise when called.
type AsyncFactoryFn = () => Promise<T>;
AsyncPredicate
一个异步函数,它接受一个 item 并返回一个布尔型 Promise
An async function that takes an item and returns a boolean promise
type AsyncPredicate = (item: T) => Promise<boolean>;
AsyncOptionPredicate
一个异步函数,它接受一个 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>;
HarnessQuery
ComponentHarness
的查询,它表示为一个 ComponentHarnessConstructor
或 HarnessPredicate
。
A query for a ComponentHarness
, which is expressed as either a ComponentHarnessConstructor
or
a HarnessPredicate
.
type HarnessQuery = ComponentHarnessConstructor<T> | HarnessPredicate<T>;
LocatorFnResult
在使用特定查询列表进行搜索时得到的结果类型。这个类型取决于被查询的特定条目。
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 typeC1
如果其中一个查询是针对
HarnessPredicate<C2>
的,那就意味着该结果可能是C2
If one of the queries is for a
HarnessPredicate<C2>
, it means that the result might be a harness of typeC2
如果其中一个查询是针对某个
string
,那就意味着该结果可能是一个TestElement
。If one of the queries is for a
string
, it means that the result might be aTestElement
.
由于我们不能确定哪个查询匹配,所以结果类型就是所有可能结果类型的并集。
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<[
ComponentHarnessConstructor<MyHarness>,
HarnessPredicate<MyOtherHarness>,
string
]>
相当于:
MyHarness | MyOtherHarness | TestElement
.
e.g.
The type:
LocatorFnResult<[
ComponentHarnessConstructor<MyHarness>,
HarnessPredicate<MyOtherHarness>,
string
]>
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];
EventData
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';
类
TestbedHarnessEnvironment
extends
HarnessEnvironment
A HarnessEnvironment
implementation for Angular's Testbed.
属性
名称 | 描述 |
---|---|
|
方法
createEnvironment | |
---|---|
Creates a |
|
参数 Parameters |
|
element Element
|
|
返回值 Returns |
|
HarnessEnvironment<Element>
|
|
createTestElement | |
---|---|
Creates a |
|
参数 Parameters |
|
element Element
|
|
返回值 Returns |
|
TestElement
|
|
documentRootLocatorFactory | |
---|---|
返回值 Returns |
|
LocatorFactory
|
|
异步
forceStabilize
|
|
---|---|
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. |
异步
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<Element[]>
|
|
异步
getChildLoader
|
|
---|---|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader>
|
|
getDocumentRoot | |
---|---|
Gets the root element for the document. |
|
返回值 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
|
|
---|---|
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. |
UnitTestElement
A TestElement
implementation for unit tests.
属性
名称 | 描述 |
---|---|
|
方法
异步
blur
|
|
---|---|
Blur the element. |
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
clear
|
|
---|---|
Clear the element's input (for input and textarea elements only). |
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
click
|
|
---|---|
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 |
|
参数 Parameters |
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
click
|
|
---|---|
Click the element at the element's center. |
|
参数 Parameters |
|
location "center"
|
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
click
|
|
---|---|
Click the element at the specified coordinates relative to the top-left of the element. @param relativeX Coordinate within the element, along the X-axis at which to click. @param relativeY Coordinate within the element, along the Y-axis at which to click. @param modifiers Modifier keys held while clicking |
|
参数 Parameters |
|
relativeX number
|
|
relativeY number
|
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
dispatchEvent
|
|
---|---|
Dispatches an event with a particular name. @param name Name of the event to be dispatched. |
|
参数 Parameters |
|
name string
|
|
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
|
|
---|---|
Gets the value for the given attribute from the element. |
|
参数 Parameters |
|
name string
|
|
返回值 Returns |
|
Promise<string | null>
|
|
异步
getCssValue
|
|
---|---|
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
|
|
---|---|
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
|
|
---|---|
参数 Parameters |
|
...args [ModifierKeys?] | ["center", ModifierKeys?] | [number, number, ModifierKeys?]
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
selectOptions
|
|
---|---|
Selects the options at the specified indexes inside of a native |
|
参数 Parameters |
|
...optionIndexes number[]
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
sendKeys
|
|
---|---|
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. |
异步
sendKeys
|
|
---|---|
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 |
|
modifiers ModifierKeys
|
|
...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. @param options Options that affect what text is included. |
|
参数 Parameters |
|
options? TextOptions
|
|
返回值 Returns |
|
Promise<string>
|
|
接口
TestbedHarnessEnvironmentOptions
Options to configure the environment.
属性
名称 | 描述 |
---|---|
|
The query function used to find DOM elements. |
Angular CDK testing-protractor API 参考文档
import {} from '@angular/cdk/testing/protractor';
类
ProtractorElement
用于 Protractor 的 TestElement
A TestElement
implementation for Protractor.
属性
名称 | 描述 |
---|---|
|
方法
异步
blur
|
|
---|---|
Blur the element. |
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
clear
|
|
---|---|
Clear the element's input (for input and textarea elements only). |
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
click
|
|
---|---|
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 |
|
参数 Parameters |
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
click
|
|
---|---|
Click the element at the element's center. |
|
参数 Parameters |
|
location "center"
|
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
click
|
|
---|---|
Click the element at the specified coordinates relative to the top-left of the element. @param relativeX Coordinate within the element, along the X-axis at which to click. @param relativeY Coordinate within the element, along the Y-axis at which to click. @param modifiers Modifier keys held while clicking |
|
参数 Parameters |
|
relativeX number
|
|
relativeY number
|
|
modifiers? ModifierKeys
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
dispatchEvent
|
|
---|---|
Dispatches an event with a particular name. @param name Name of the event to be dispatched. |
|
参数 Parameters |
|
name string
|
|
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
|
|
---|---|
Gets the value for the given attribute from the element. |
|
参数 Parameters |
|
name string
|
|
返回值 Returns |
|
Promise<string | null>
|
|
异步
getCssValue
|
|
---|---|
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
|
|
---|---|
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
|
|
---|---|
参数 Parameters |
|
...args [ModifierKeys?] | ["center", ModifierKeys?] | [number, number, ModifierKeys?]
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
selectOptions
|
|
---|---|
Selects the options at the specified indexes inside of a native |
|
参数 Parameters |
|
...optionIndexes number[]
|
|
返回值 Returns |
|
Promise<void>
|
Promise that resolves when the action completes. |
异步
sendKeys
|
|
---|---|
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. |
异步
sendKeys
|
|
---|---|
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 |
|
modifiers ModifierKeys
|
|
...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. @param options Options that affect what text is included. |
|
参数 Parameters |
|
options? TextOptions
|
|
返回值 Returns |
|
Promise<string>
|
|
ProtractorHarnessEnvironment
extends
HarnessEnvironment
Protractor 的 HarnessEnvironment
实现。
A HarnessEnvironment
implementation for Protractor.
属性
名称 | 描述 |
---|---|
|
方法
createEnvironment | |
---|---|
Creates a |
|
参数 Parameters |
|
element any
|
|
返回值 Returns |
|
HarnessEnvironment<ElementFinder>
|
|
createTestElement | |
---|---|
Creates a |
|
参数 Parameters |
|
element any
|
|
返回值 Returns |
|
TestElement
|
|
documentRootLocatorFactory | |
---|---|
返回值 Returns |
|
LocatorFactory
|
|
异步
forceStabilize
|
|
---|---|
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. |
异步
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<ElementFinder[]>
|
|
异步
getChildLoader
|
|
---|---|
参数 Parameters |
|
selector string
|
|
返回值 Returns |
|
Promise<HarnessLoader>
|
|
getDocumentRoot | |
---|---|
Gets the root element for the document. |
|
返回值 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>
|
|
接口
ProtractorHarnessEnvironmentOptions
用于配置环境的选项。
Options to configure the environment.
属性
名称 | 描述 |
---|---|
|
用于查找 DOM 元素的查询功能。 The query function used to find DOM elements. |