Base class for component test harnesses that all component harness authors should extend. This base component harness provides the basic ability to locate element and sub-component harnesses.
API
abstract class ComponentHarness {
constructor(locatorFactory: LocatorFactory): ComponentHarness;
host(): Promise<TestElement>;
protected documentRootLocatorFactory(): LocatorFactory;
protected locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T): () => Promise<LocatorFnResult<T>>;
protected locatorForOptional<T extends (HarnessQuery<any> | string)[]>(...queries: T): () => Promise<LocatorFnResult<T> | null>;
protected locatorForAll<T extends (HarnessQuery<any> | string)[]>(...queries: T): () => Promise<LocatorFnResult<T>[]>;
protected forceStabilize(): Promise<void>;
protected waitForTasksOutsideAngular(): Promise<void>;
}
constructor
ComponentHarnessComponentHarness
host
Promise<TestElement>Gets a Promise for the TestElement representing the host element of the component.
Promise<TestElement>
documentRootLocatorFactory
LocatorFactoryGets 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).
LocatorFactory
locatorFor
() => Promise<LocatorFnResult<T>>Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the host element of this ComponentHarness.
For example, given the following DOM and assuming DivHarness.hostSelector is 'div'
<div id="d1"></div><div id="d2"></div>
then we expect:
await ch.locatorFor(DivHarness, 'div')() // Gets a `DivHarness` instance for #d1
await ch.locatorFor('div', DivHarness)() // Gets a `TestElement` instance for #d1
await ch.locatorFor('span')() // Throws because the `Promise` rejects
TA list of queries specifying which harnesses and elements to search for:
- A
stringsearches for elements matching the CSS selector specified by the string. - A
ComponentHarnessconstructor searches forComponentHarnessinstances matching the given class. - A
HarnessPredicatesearches forComponentHarnessinstances matching the given predicate.
() => Promise<LocatorFnResult<T>>
locatorForOptional
() => Promise<LocatorFnResult<T> | null>Creates an asynchronous locator function that can be used to find a ComponentHarness instance or element under the host element of this ComponentHarness.
For example, given the following DOM and assuming DivHarness.hostSelector is 'div'
<div id="d1"></div><div id="d2"></div>
then we expect:
await ch.locatorForOptional(DivHarness, 'div')() // Gets a `DivHarness` instance for #d1
await ch.locatorForOptional('div', DivHarness)() // Gets a `TestElement` instance for #d1
await ch.locatorForOptional('span')() // Gets `null`
TA list of queries specifying which harnesses and elements to search for:
- A
stringsearches for elements matching the CSS selector specified by the string. - A
ComponentHarnessconstructor searches forComponentHarnessinstances matching the given class. - A
HarnessPredicatesearches forComponentHarnessinstances matching the given predicate.
() => Promise<LocatorFnResult<T> | null>
locatorForAll
() => Promise<LocatorFnResult<T>[]>Creates an asynchronous locator function that can be used to find ComponentHarness instances or elements under the host element of this ComponentHarness.
For example, given the following DOM and assuming DivHarness.hostSelector is 'div' and IdIsD1Harness.hostSelector is '#d1'
<div id="d1"></div><div id="d2"></div>
then we expect:
// Gets [DivHarness for #d1, TestElement for #d1, DivHarness for #d2, TestElement for #d2]
await ch.locatorForAll(DivHarness, 'div')()
// Gets [TestElement for #d1, TestElement for #d2]
await ch.locatorForAll('div', '#d1')()
// Gets [DivHarness for #d1, IdIsD1Harness for #d1, DivHarness for #d2]
await ch.locatorForAll(DivHarness, IdIsD1Harness)()
// Gets []
await ch.locatorForAll('span')()
TA list of queries specifying which harnesses and elements to search for:
- A
stringsearches for elements matching the CSS selector specified by the string. - A
ComponentHarnessconstructor searches forComponentHarnessinstances matching the given class. - A
HarnessPredicatesearches forComponentHarnessinstances matching the given predicate.
() => Promise<LocatorFnResult<T>[]>
forceStabilize
Promise<void>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.
Promise<void>
waitForTasksOutsideAngular
Promise<void>Waits for all scheduled or running async tasks to complete. This allows harness authors to wait for async tasks outside of the Angular zone.
Promise<void>