index.d.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /** An async function that returns a promise when called. */
  2. export declare type AsyncFactoryFn<T> = () => Promise<T>;
  3. /** An async function that takes an item and an option value and returns a boolean promise. */
  4. export declare type AsyncOptionPredicate<T, O> = (item: T, option: O) => Promise<boolean>;
  5. /** An async function that takes an item and returns a boolean promise */
  6. export declare type AsyncPredicate<T> = (item: T) => Promise<boolean>;
  7. /** Represents the status of auto change detection. */
  8. export declare interface AutoChangeDetectionStatus {
  9. /** Whether auto change detection is disabled. */
  10. isDisabled: boolean;
  11. /**
  12. * An optional callback, if present it indicates that change detection should be run immediately,
  13. * while handling the status change. The callback should then be called as soon as change
  14. * detection is done.
  15. */
  16. onDetectChangesNow?: () => void;
  17. }
  18. /** A set of criteria that can be used to filter a list of `ComponentHarness` instances. */
  19. export declare interface BaseHarnessFilters {
  20. /** Only find instances whose host element matches the given selector. */
  21. selector?: string;
  22. /** Only find instances that are nested under an element with the given selector. */
  23. ancestor?: string;
  24. }
  25. /**
  26. * Base class for component harnesses that all component harness authors should extend. This base
  27. * component harness provides the basic ability to locate element and sub-component harness. It
  28. * should be inherited when defining user's own harness.
  29. */
  30. export declare abstract class ComponentHarness {
  31. protected readonly locatorFactory: LocatorFactory;
  32. constructor(locatorFactory: LocatorFactory);
  33. /** Gets a `Promise` for the `TestElement` representing the host element of the component. */
  34. host(): Promise<TestElement>;
  35. /**
  36. * Gets a `LocatorFactory` for the document root element. This factory can be used to create
  37. * locators for elements that a component creates outside of its own root element. (e.g. by
  38. * appending to document.body).
  39. */
  40. protected documentRootLocatorFactory(): LocatorFactory;
  41. /**
  42. * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
  43. * or element under the host element of this `ComponentHarness`.
  44. * @param queries A list of queries specifying which harnesses and elements to search for:
  45. * - A `string` searches for elements matching the CSS selector specified by the string.
  46. * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
  47. * given class.
  48. * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
  49. * predicate.
  50. * @return An asynchronous locator function that searches for and returns a `Promise` for the
  51. * first element or harness matching the given search criteria. Matches are ordered first by
  52. * order in the DOM, and second by order in the queries list. If no matches are found, the
  53. * `Promise` rejects. The type that the `Promise` resolves to is a union of all result types for
  54. * each query.
  55. *
  56. * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
  57. * `DivHarness.hostSelector === 'div'`:
  58. * - `await ch.locatorFor(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
  59. * - `await ch.locatorFor('div', DivHarness)()` gets a `TestElement` instance for `#d1`
  60. * - `await ch.locatorFor('span')()` throws because the `Promise` rejects.
  61. */
  62. protected locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>>;
  63. /**
  64. * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
  65. * or element under the host element of this `ComponentHarness`.
  66. * @param queries A list of queries specifying which harnesses and elements to search for:
  67. * - A `string` searches for elements matching the CSS selector specified by the string.
  68. * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
  69. * given class.
  70. * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
  71. * predicate.
  72. * @return An asynchronous locator function that searches for and returns a `Promise` for the
  73. * first element or harness matching the given search criteria. Matches are ordered first by
  74. * order in the DOM, and second by order in the queries list. If no matches are found, the
  75. * `Promise` is resolved with `null`. The type that the `Promise` resolves to is a union of all
  76. * result types for each query or null.
  77. *
  78. * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
  79. * `DivHarness.hostSelector === 'div'`:
  80. * - `await ch.locatorForOptional(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
  81. * - `await ch.locatorForOptional('div', DivHarness)()` gets a `TestElement` instance for `#d1`
  82. * - `await ch.locatorForOptional('span')()` gets `null`.
  83. */
  84. protected locatorForOptional<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T> | null>;
  85. /**
  86. * Creates an asynchronous locator function that can be used to find `ComponentHarness` instances
  87. * or elements under the host element of this `ComponentHarness`.
  88. * @param queries A list of queries specifying which harnesses and elements to search for:
  89. * - A `string` searches for elements matching the CSS selector specified by the string.
  90. * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
  91. * given class.
  92. * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
  93. * predicate.
  94. * @return An asynchronous locator function that searches for and returns a `Promise` for all
  95. * elements and harnesses matching the given search criteria. Matches are ordered first by
  96. * order in the DOM, and second by order in the queries list. If an element matches more than
  97. * one `ComponentHarness` class, the locator gets an instance of each for the same element. If
  98. * an element matches multiple `string` selectors, only one `TestElement` instance is returned
  99. * for that element. The type that the `Promise` resolves to is an array where each element is
  100. * the union of all result types for each query.
  101. *
  102. * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
  103. * `DivHarness.hostSelector === 'div'` and `IdIsD1Harness.hostSelector === '#d1'`:
  104. * - `await ch.locatorForAll(DivHarness, 'div')()` gets `[
  105. * DivHarness, // for #d1
  106. * TestElement, // for #d1
  107. * DivHarness, // for #d2
  108. * TestElement // for #d2
  109. * ]`
  110. * - `await ch.locatorForAll('div', '#d1')()` gets `[
  111. * TestElement, // for #d1
  112. * TestElement // for #d2
  113. * ]`
  114. * - `await ch.locatorForAll(DivHarness, IdIsD1Harness)()` gets `[
  115. * DivHarness, // for #d1
  116. * IdIsD1Harness, // for #d1
  117. * DivHarness // for #d2
  118. * ]`
  119. * - `await ch.locatorForAll('span')()` gets `[]`.
  120. */
  121. protected locatorForAll<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>[]>;
  122. /**
  123. * Flushes change detection and async tasks in the Angular zone.
  124. * In most cases it should not be necessary to call this manually. However, there may be some edge
  125. * cases where it is needed to fully flush animation events.
  126. */
  127. protected forceStabilize(): Promise<void>;
  128. /**
  129. * Waits for all scheduled or running async tasks to complete. This allows harness
  130. * authors to wait for async tasks outside of the Angular zone.
  131. */
  132. protected waitForTasksOutsideAngular(): Promise<void>;
  133. }
  134. /** Constructor for a ComponentHarness subclass. */
  135. export declare interface ComponentHarnessConstructor<T extends ComponentHarness> {
  136. new (locatorFactory: LocatorFactory): T;
  137. /**
  138. * `ComponentHarness` subclasses must specify a static `hostSelector` property that is used to
  139. * find the host element for the corresponding component. This property should match the selector
  140. * for the Angular component.
  141. */
  142. hostSelector: string;
  143. }
  144. /**
  145. * Base class for component harnesses that authors should extend if they anticipate that consumers
  146. * of the harness may want to access other harnesses within the `<ng-content>` of the component.
  147. */
  148. export declare abstract class ContentContainerComponentHarness<S extends string = string> extends ComponentHarness implements HarnessLoader {
  149. getChildLoader(selector: S): Promise<HarnessLoader>;
  150. getAllChildLoaders(selector: S): Promise<HarnessLoader[]>;
  151. getHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T>;
  152. getHarnessOrNull<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T | null>;
  153. getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]>;
  154. hasHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<boolean>;
  155. /**
  156. * Gets the root harness loader from which to start
  157. * searching for content contained by this harness.
  158. */
  159. protected getRootHarnessLoader(): Promise<HarnessLoader>;
  160. }
  161. /**
  162. * Dimensions for element size and its position relative to the viewport.
  163. */
  164. export declare interface ElementDimensions {
  165. top: number;
  166. left: number;
  167. width: number;
  168. height: number;
  169. }
  170. /** Data that can be attached to a custom event dispatched from a `TestElement`. */
  171. export declare type EventData = string | number | boolean | Function | undefined | null | EventData[] | {
  172. [key: string]: EventData;
  173. };
  174. /**
  175. * Returns an error which reports that no keys have been specified.
  176. * @docs-private
  177. */
  178. export declare function getNoKeysSpecifiedError(): Error;
  179. /**
  180. * Gets text of element excluding certain selectors within the element.
  181. * @param element Element to get text from,
  182. * @param excludeSelector Selector identifying which elements to exclude,
  183. */
  184. export declare function _getTextWithExcludedElements(element: Element, excludeSelector: string): string;
  185. /**
  186. * Allows a test `HarnessEnvironment` to install its own handler for auto change detection status
  187. * changes.
  188. * @param handler The handler for the auto change detection status.
  189. */
  190. export declare function handleAutoChangeDetectionStatus(handler: (status: AutoChangeDetectionStatus) => void): void;
  191. /**
  192. * Base harness environment class that can be extended to allow `ComponentHarness`es to be used in
  193. * different test environments (e.g. testbed, protractor, etc.). This class implements the
  194. * functionality of both a `HarnessLoader` and `LocatorFactory`. This class is generic on the raw
  195. * element type, `E`, used by the particular test environment.
  196. */
  197. export declare abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
  198. protected rawRootElement: E;
  199. get rootElement(): TestElement;
  200. set rootElement(element: TestElement);
  201. private _rootElement;
  202. protected constructor(rawRootElement: E);
  203. documentRootLocatorFactory(): LocatorFactory;
  204. locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>>;
  205. locatorForOptional<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T> | null>;
  206. locatorForAll<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>[]>;
  207. rootHarnessLoader(): Promise<HarnessLoader>;
  208. harnessLoaderFor(selector: string): Promise<HarnessLoader>;
  209. harnessLoaderForOptional(selector: string): Promise<HarnessLoader | null>;
  210. harnessLoaderForAll(selector: string): Promise<HarnessLoader[]>;
  211. getHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T>;
  212. getHarnessOrNull<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T | null>;
  213. getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]>;
  214. hasHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<boolean>;
  215. getChildLoader(selector: string): Promise<HarnessLoader>;
  216. getAllChildLoaders(selector: string): Promise<HarnessLoader[]>;
  217. /** Creates a `ComponentHarness` for the given harness type with the given raw host element. */
  218. protected createComponentHarness<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>, element: E): T;
  219. abstract forceStabilize(): Promise<void>;
  220. abstract waitForTasksOutsideAngular(): Promise<void>;
  221. /** Gets the root element for the document. */
  222. protected abstract getDocumentRoot(): E;
  223. /** Creates a `TestElement` from a raw element. */
  224. protected abstract createTestElement(element: E): TestElement;
  225. /** Creates a `HarnessLoader` rooted at the given raw element. */
  226. protected abstract createEnvironment(element: E): HarnessEnvironment<E>;
  227. /**
  228. * Gets a list of all elements matching the given selector under this environment's root element.
  229. */
  230. protected abstract getAllRawElements(selector: string): Promise<E[]>;
  231. /**
  232. * Matches the given raw elements with the given list of element and harness queries to produce a
  233. * list of matched harnesses and test elements.
  234. */
  235. private _getAllHarnessesAndTestElements;
  236. /**
  237. * Check whether the given query matches the given element, if it does return the matched
  238. * `TestElement` or `ComponentHarness`, if it does not, return null. In cases where the caller
  239. * knows for sure that the query matches the element's selector, `skipSelectorCheck` can be used
  240. * to skip verification and optimize performance.
  241. */
  242. private _getQueryResultForElement;
  243. }
  244. /**
  245. * Interface used to load ComponentHarness objects. This interface is used by test authors to
  246. * instantiate `ComponentHarness`es.
  247. */
  248. export declare interface HarnessLoader {
  249. /**
  250. * Searches for an element with the given selector under the current instances's root element,
  251. * and returns a `HarnessLoader` rooted at the matching element. If multiple elements match the
  252. * selector, the first is used. If no elements match, an error is thrown.
  253. * @param selector The selector for the root element of the new `HarnessLoader`
  254. * @return A `HarnessLoader` rooted at the element matching the given selector.
  255. * @throws If a matching element can't be found.
  256. */
  257. getChildLoader(selector: string): Promise<HarnessLoader>;
  258. /**
  259. * Searches for all elements with the given selector under the current instances's root element,
  260. * and returns an array of `HarnessLoader`s, one for each matching element, rooted at that
  261. * element.
  262. * @param selector The selector for the root element of the new `HarnessLoader`
  263. * @return A list of `HarnessLoader`s, one for each matching element, rooted at that element.
  264. */
  265. getAllChildLoaders(selector: string): Promise<HarnessLoader[]>;
  266. /**
  267. * Searches for an instance of the component corresponding to the given harness type under the
  268. * `HarnessLoader`'s root element, and returns a `ComponentHarness` for that instance. If multiple
  269. * matching components are found, a harness for the first one is returned. If no matching
  270. * component is found, an error is thrown.
  271. * @param query A query for a harness to create
  272. * @return An instance of the given harness type
  273. * @throws If a matching component instance can't be found.
  274. */
  275. getHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T>;
  276. /**
  277. * Searches for an instance of the component corresponding to the given harness type under the
  278. * `HarnessLoader`'s root element, and returns a `ComponentHarness` for that instance. If multiple
  279. * matching components are found, a harness for the first one is returned. If no matching
  280. * component is found, null is returned.
  281. * @param query A query for a harness to create
  282. * @return An instance of the given harness type (or null if not found).
  283. */
  284. getHarnessOrNull<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T | null>;
  285. /**
  286. * Searches for all instances of the component corresponding to the given harness type under the
  287. * `HarnessLoader`'s root element, and returns a list `ComponentHarness` for each instance.
  288. * @param query A query for a harness to create
  289. * @return A list instances of the given harness type.
  290. */
  291. getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]>;
  292. /**
  293. * Searches for an instance of the component corresponding to the given harness type under the
  294. * `HarnessLoader`'s root element, and returns a boolean indicating if any were found.
  295. * @param query A query for a harness to create
  296. * @return A boolean indicating if an instance was found.
  297. */
  298. hasHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<boolean>;
  299. }
  300. /**
  301. * A class used to associate a ComponentHarness class with predicates functions that can be used to
  302. * filter instances of the class.
  303. */
  304. export declare class HarnessPredicate<T extends ComponentHarness> {
  305. harnessType: ComponentHarnessConstructor<T>;
  306. private _predicates;
  307. private _descriptions;
  308. private _ancestor;
  309. constructor(harnessType: ComponentHarnessConstructor<T>, options: BaseHarnessFilters);
  310. /**
  311. * Checks if the specified nullable string value matches the given pattern.
  312. * @param value The nullable string value to check, or a Promise resolving to the
  313. * nullable string value.
  314. * @param pattern The pattern the value is expected to match. If `pattern` is a string,
  315. * `value` is expected to match exactly. If `pattern` is a regex, a partial match is
  316. * allowed. If `pattern` is `null`, the value is expected to be `null`.
  317. * @return Whether the value matches the pattern.
  318. */
  319. static stringMatches(value: string | null | Promise<string | null>, pattern: string | RegExp | null): Promise<boolean>;
  320. /**
  321. * Adds a predicate function to be run against candidate harnesses.
  322. * @param description A description of this predicate that may be used in error messages.
  323. * @param predicate An async predicate function.
  324. * @return this (for method chaining).
  325. */
  326. add(description: string, predicate: AsyncPredicate<T>): this;
  327. /**
  328. * Adds a predicate function that depends on an option value to be run against candidate
  329. * harnesses. If the option value is undefined, the predicate will be ignored.
  330. * @param name The name of the option (may be used in error messages).
  331. * @param option The option value.
  332. * @param predicate The predicate function to run if the option value is not undefined.
  333. * @return this (for method chaining).
  334. */
  335. addOption<O>(name: string, option: O | undefined, predicate: AsyncOptionPredicate<T, O>): this;
  336. /**
  337. * Filters a list of harnesses on this predicate.
  338. * @param harnesses The list of harnesses to filter.
  339. * @return A list of harnesses that satisfy this predicate.
  340. */
  341. filter(harnesses: T[]): Promise<T[]>;
  342. /**
  343. * Evaluates whether the given harness satisfies this predicate.
  344. * @param harness The harness to check
  345. * @return A promise that resolves to true if the harness satisfies this predicate,
  346. * and resolves to false otherwise.
  347. */
  348. evaluate(harness: T): Promise<boolean>;
  349. /** Gets a description of this predicate for use in error messages. */
  350. getDescription(): string;
  351. /** Gets the selector used to find candidate elements. */
  352. getSelector(): string;
  353. /** Adds base options common to all harness types. */
  354. private _addBaseOptions;
  355. }
  356. /**
  357. * A query for a `ComponentHarness`, which is expressed as either a `ComponentHarnessConstructor` or
  358. * a `HarnessPredicate`.
  359. */
  360. export declare type HarnessQuery<T extends ComponentHarness> = ComponentHarnessConstructor<T> | HarnessPredicate<T>;
  361. /**
  362. * Interface used to create asynchronous locator functions used find elements and component
  363. * harnesses. This interface is used by `ComponentHarness` authors to create locator functions for
  364. * their `ComponentHarness` subclass.
  365. */
  366. export declare interface LocatorFactory {
  367. /** Gets a locator factory rooted at the document root. */
  368. documentRootLocatorFactory(): LocatorFactory;
  369. /** The root element of this `LocatorFactory` as a `TestElement`. */
  370. rootElement: TestElement;
  371. /**
  372. * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
  373. * or element under the root element of this `LocatorFactory`.
  374. * @param queries A list of queries specifying which harnesses and elements to search for:
  375. * - A `string` searches for elements matching the CSS selector specified by the string.
  376. * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
  377. * given class.
  378. * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
  379. * predicate.
  380. * @return An asynchronous locator function that searches for and returns a `Promise` for the
  381. * first element or harness matching the given search criteria. Matches are ordered first by
  382. * order in the DOM, and second by order in the queries list. If no matches are found, the
  383. * `Promise` rejects. The type that the `Promise` resolves to is a union of all result types for
  384. * each query.
  385. *
  386. * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
  387. * `DivHarness.hostSelector === 'div'`:
  388. * - `await lf.locatorFor(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
  389. * - `await lf.locatorFor('div', DivHarness)()` gets a `TestElement` instance for `#d1`
  390. * - `await lf.locatorFor('span')()` throws because the `Promise` rejects.
  391. */
  392. locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>>;
  393. /**
  394. * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
  395. * or element under the root element of this `LocatorFactory`.
  396. * @param queries A list of queries specifying which harnesses and elements to search for:
  397. * - A `string` searches for elements matching the CSS selector specified by the string.
  398. * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
  399. * given class.
  400. * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
  401. * predicate.
  402. * @return An asynchronous locator function that searches for and returns a `Promise` for the
  403. * first element or harness matching the given search criteria. Matches are ordered first by
  404. * order in the DOM, and second by order in the queries list. If no matches are found, the
  405. * `Promise` is resolved with `null`. The type that the `Promise` resolves to is a union of all
  406. * result types for each query or null.
  407. *
  408. * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
  409. * `DivHarness.hostSelector === 'div'`:
  410. * - `await lf.locatorForOptional(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
  411. * - `await lf.locatorForOptional('div', DivHarness)()` gets a `TestElement` instance for `#d1`
  412. * - `await lf.locatorForOptional('span')()` gets `null`.
  413. */
  414. locatorForOptional<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T> | null>;
  415. /**
  416. * Creates an asynchronous locator function that can be used to find `ComponentHarness` instances
  417. * or elements under the root element of this `LocatorFactory`.
  418. * @param queries A list of queries specifying which harnesses and elements to search for:
  419. * - A `string` searches for elements matching the CSS selector specified by the string.
  420. * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
  421. * given class.
  422. * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
  423. * predicate.
  424. * @return An asynchronous locator function that searches for and returns a `Promise` for all
  425. * elements and harnesses matching the given search criteria. Matches are ordered first by
  426. * order in the DOM, and second by order in the queries list. If an element matches more than
  427. * one `ComponentHarness` class, the locator gets an instance of each for the same element. If
  428. * an element matches multiple `string` selectors, only one `TestElement` instance is returned
  429. * for that element. The type that the `Promise` resolves to is an array where each element is
  430. * the union of all result types for each query.
  431. *
  432. * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
  433. * `DivHarness.hostSelector === 'div'` and `IdIsD1Harness.hostSelector === '#d1'`:
  434. * - `await lf.locatorForAll(DivHarness, 'div')()` gets `[
  435. * DivHarness, // for #d1
  436. * TestElement, // for #d1
  437. * DivHarness, // for #d2
  438. * TestElement // for #d2
  439. * ]`
  440. * - `await lf.locatorForAll('div', '#d1')()` gets `[
  441. * TestElement, // for #d1
  442. * TestElement // for #d2
  443. * ]`
  444. * - `await lf.locatorForAll(DivHarness, IdIsD1Harness)()` gets `[
  445. * DivHarness, // for #d1
  446. * IdIsD1Harness, // for #d1
  447. * DivHarness // for #d2
  448. * ]`
  449. * - `await lf.locatorForAll('span')()` gets `[]`.
  450. */
  451. locatorForAll<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>[]>;
  452. /** @return A `HarnessLoader` rooted at the root element of this `LocatorFactory`. */
  453. rootHarnessLoader(): Promise<HarnessLoader>;
  454. /**
  455. * Gets a `HarnessLoader` instance for an element under the root of this `LocatorFactory`.
  456. * @param selector The selector for the root element.
  457. * @return A `HarnessLoader` rooted at the first element matching the given selector.
  458. * @throws If no matching element is found for the given selector.
  459. */
  460. harnessLoaderFor(selector: string): Promise<HarnessLoader>;
  461. /**
  462. * Gets a `HarnessLoader` instance for an element under the root of this `LocatorFactory`
  463. * @param selector The selector for the root element.
  464. * @return A `HarnessLoader` rooted at the first element matching the given selector, or null if
  465. * no matching element is found.
  466. */
  467. harnessLoaderForOptional(selector: string): Promise<HarnessLoader | null>;
  468. /**
  469. * Gets a list of `HarnessLoader` instances, one for each matching element.
  470. * @param selector The selector for the root element.
  471. * @return A list of `HarnessLoader`, one rooted at each element matching the given selector.
  472. */
  473. harnessLoaderForAll(selector: string): Promise<HarnessLoader[]>;
  474. /**
  475. * Flushes change detection and async tasks captured in the Angular zone.
  476. * In most cases it should not be necessary to call this manually. However, there may be some edge
  477. * cases where it is needed to fully flush animation events.
  478. */
  479. forceStabilize(): Promise<void>;
  480. /**
  481. * Waits for all scheduled or running async tasks to complete. This allows harness
  482. * authors to wait for async tasks outside of the Angular zone.
  483. */
  484. waitForTasksOutsideAngular(): Promise<void>;
  485. }
  486. /**
  487. * The result type obtained when searching using a particular list of queries. This type depends on
  488. * the particular items being queried.
  489. * - If one of the queries is for a `ComponentHarnessConstructor<C1>`, it means that the result
  490. * might be a harness of type `C1`
  491. * - If one of the queries is for a `HarnessPredicate<C2>`, it means that the result might be a
  492. * harness of type `C2`
  493. * - If one of the queries is for a `string`, it means that the result might be a `TestElement`.
  494. *
  495. * Since we don't know for sure which query will match, the result type if the union of the types
  496. * for all possible results.
  497. *
  498. * e.g.
  499. * The type:
  500. * `LocatorFnResult&lt;[
  501. * ComponentHarnessConstructor&lt;MyHarness&gt;,
  502. * HarnessPredicate&lt;MyOtherHarness&gt;,
  503. * string
  504. * ]&gt;`
  505. * is equivalent to:
  506. * `MyHarness | MyOtherHarness | TestElement`.
  507. */
  508. export declare type LocatorFnResult<T extends (HarnessQuery<any> | string)[]> = {
  509. [I in keyof T]: T[I] extends new (...args: any[]) => infer C ? C : T[I] extends {
  510. harnessType: new (...args: any[]) => infer C;
  511. } ? C : T[I] extends string ? TestElement : never;
  512. }[number];
  513. /**
  514. * Disables the harness system's auto change detection for the duration of the given function.
  515. * @param fn The function to disable auto change detection for.
  516. * @return The result of the given function.
  517. */
  518. export declare function manualChangeDetection<T>(fn: () => Promise<T>): Promise<T>;
  519. /** Modifier keys that may be held while typing. */
  520. export declare interface ModifierKeys {
  521. control?: boolean;
  522. alt?: boolean;
  523. shift?: boolean;
  524. meta?: boolean;
  525. }
  526. /**
  527. * Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change
  528. * detection over the entire operation such that change detection occurs exactly once before
  529. * resolving the values and once after.
  530. * @param values A getter for the async values to resolve in parallel with batched change detection.
  531. * @return The resolved values.
  532. */
  533. export declare function parallel<T1, T2, T3, T4, T5>(values: () => [
  534. T1 | PromiseLike<T1>,
  535. T2 | PromiseLike<T2>,
  536. T3 | PromiseLike<T3>,
  537. T4 | PromiseLike<T4>,
  538. T5 | PromiseLike<T5>
  539. ]): Promise<[T1, T2, T3, T4, T5]>;
  540. /**
  541. * Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change
  542. * detection over the entire operation such that change detection occurs exactly once before
  543. * resolving the values and once after.
  544. * @param values A getter for the async values to resolve in parallel with batched change detection.
  545. * @return The resolved values.
  546. */
  547. export declare function parallel<T1, T2, T3, T4>(values: () => [
  548. T1 | PromiseLike<T1>,
  549. T2 | PromiseLike<T2>,
  550. T3 | PromiseLike<T3>,
  551. T4 | PromiseLike<T4>
  552. ]): Promise<[T1, T2, T3, T4]>;
  553. /**
  554. * Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change
  555. * detection over the entire operation such that change detection occurs exactly once before
  556. * resolving the values and once after.
  557. * @param values A getter for the async values to resolve in parallel with batched change detection.
  558. * @return The resolved values.
  559. */
  560. export declare function parallel<T1, T2, T3>(values: () => [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
  561. /**
  562. * Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change
  563. * detection over the entire operation such that change detection occurs exactly once before
  564. * resolving the values and once after.
  565. * @param values A getter for the async values to resolve in parallel with batched change detection.
  566. * @return The resolved values.
  567. */
  568. export declare function parallel<T1, T2>(values: () => [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
  569. /**
  570. * Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change
  571. * detection over the entire operation such that change detection occurs exactly once before
  572. * resolving the values and once after.
  573. * @param values A getter for the async values to resolve in parallel with batched change detection.
  574. * @return The resolved values.
  575. */
  576. export declare function parallel<T>(values: () => (T | PromiseLike<T>)[]): Promise<T[]>;
  577. /** Allows a `HarnessEnvironment` to stop handling auto change detection status changes. */
  578. export declare function stopHandlingAutoChangeDetectionStatus(): void;
  579. /**
  580. * This acts as a common interface for DOM elements across both unit and e2e tests. It is the
  581. * interface through which the ComponentHarness interacts with the component's DOM.
  582. */
  583. export declare interface TestElement {
  584. /** Blur the element. */
  585. blur(): Promise<void>;
  586. /** Clear the element's input (for input and textarea elements only). */
  587. clear(): Promise<void>;
  588. /**
  589. * Click the element at the default location for the current environment. If you need to guarantee
  590. * the element is clicked at a specific location, consider using `click('center')` or
  591. * `click(x, y)` instead.
  592. */
  593. click(modifiers?: ModifierKeys): Promise<void>;
  594. /** Click the element at the element's center. */
  595. click(location: 'center', modifiers?: ModifierKeys): Promise<void>;
  596. /**
  597. * Click the element at the specified coordinates relative to the top-left of the element.
  598. * @param relativeX Coordinate within the element, along the X-axis at which to click.
  599. * @param relativeY Coordinate within the element, along the Y-axis at which to click.
  600. * @param modifiers Modifier keys held while clicking
  601. */
  602. click(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
  603. /**
  604. * Right clicks on the element at the specified coordinates relative to the top-left of it.
  605. * @param relativeX Coordinate within the element, along the X-axis at which to click.
  606. * @param relativeY Coordinate within the element, along the Y-axis at which to click.
  607. * @param modifiers Modifier keys held while clicking
  608. */
  609. rightClick(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
  610. /** Focus the element. */
  611. focus(): Promise<void>;
  612. /** Get the computed value of the given CSS property for the element. */
  613. getCssValue(property: string): Promise<string>;
  614. /** Hovers the mouse over the element. */
  615. hover(): Promise<void>;
  616. /** Moves the mouse away from the element. */
  617. mouseAway(): Promise<void>;
  618. /**
  619. * Sends the given string to the input as a series of key presses. Also fires input events
  620. * and attempts to add the string to the Element's value. Note that some environments cannot
  621. * reproduce native browser behavior for keyboard shortcuts such as Tab, Ctrl + A, etc.
  622. * @throws An error if no keys have been specified.
  623. */
  624. sendKeys(...keys: (string | TestKey)[]): Promise<void>;
  625. /**
  626. * Sends the given string to the input as a series of key presses. Also fires input
  627. * events and attempts to add the string to the Element's value.
  628. * @throws An error if no keys have been specified.
  629. */
  630. sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
  631. /**
  632. * Gets the text from the element.
  633. * @param options Options that affect what text is included.
  634. */
  635. text(options?: TextOptions): Promise<string>;
  636. /**
  637. * Sets the value of a `contenteditable` element.
  638. * @param value Value to be set on the element.
  639. * @breaking-change 16.0.0 Will become a required method.
  640. */
  641. setContenteditableValue?(value: string): Promise<void>;
  642. /** Gets the value for the given attribute from the element. */
  643. getAttribute(name: string): Promise<string | null>;
  644. /** Checks whether the element has the given class. */
  645. hasClass(name: string): Promise<boolean>;
  646. /** Gets the dimensions of the element. */
  647. getDimensions(): Promise<ElementDimensions>;
  648. /** Gets the value of a property of an element. */
  649. getProperty<T = any>(name: string): Promise<T>;
  650. /** Checks whether this element matches the given selector. */
  651. matchesSelector(selector: string): Promise<boolean>;
  652. /** Checks whether the element is focused. */
  653. isFocused(): Promise<boolean>;
  654. /** Sets the value of a property of an input. */
  655. setInputValue(value: string): Promise<void>;
  656. /** Selects the options at the specified indexes inside of a native `select` element. */
  657. selectOptions(...optionIndexes: number[]): Promise<void>;
  658. /**
  659. * Dispatches an event with a particular name.
  660. * @param name Name of the event to be dispatched.
  661. */
  662. dispatchEvent(name: string, data?: Record<string, EventData>): Promise<void>;
  663. }
  664. /** An enum of non-text keys that can be used with the `sendKeys` method. */
  665. export declare enum TestKey {
  666. BACKSPACE = 0,
  667. TAB = 1,
  668. ENTER = 2,
  669. SHIFT = 3,
  670. CONTROL = 4,
  671. ALT = 5,
  672. ESCAPE = 6,
  673. PAGE_UP = 7,
  674. PAGE_DOWN = 8,
  675. END = 9,
  676. HOME = 10,
  677. LEFT_ARROW = 11,
  678. UP_ARROW = 12,
  679. RIGHT_ARROW = 13,
  680. DOWN_ARROW = 14,
  681. INSERT = 15,
  682. DELETE = 16,
  683. F1 = 17,
  684. F2 = 18,
  685. F3 = 19,
  686. F4 = 20,
  687. F5 = 21,
  688. F6 = 22,
  689. F7 = 23,
  690. F8 = 24,
  691. F9 = 25,
  692. F10 = 26,
  693. F11 = 27,
  694. F12 = 28,
  695. META = 29
  696. }
  697. export declare interface TextOptions {
  698. /** Optional selector for elements to exclude. */
  699. exclude?: string;
  700. }
  701. export { }