7
7
*/
8
8
9
9
/**
10
- * Test Element interface
11
- * This is a wrapper of native element
10
+ * This acts as a common interface for DOM elements across both unit and e2e tests. It is the
11
+ * interface through which the ComponentHarness interacts with the component's DOM.
12
12
*/
13
13
export interface TestElement {
14
+ /** Blur the element. */
14
15
blur ( ) : Promise < void > ;
16
+ /** Clear the element's input (for input elements only). */
15
17
clear ( ) : Promise < void > ;
18
+ /** Click the element. */
16
19
click ( ) : Promise < void > ;
20
+ /** Focus the element. */
17
21
focus ( ) : Promise < void > ;
22
+ /** Get the computed value of the given CSS property for the element. */
18
23
getCssValue ( property : string ) : Promise < string > ;
24
+ /** Hovers the mouse over the element. */
19
25
hover ( ) : Promise < void > ;
26
+ /**
27
+ * Sends the given string to the input as a series of key presses. Also fires input events
28
+ * and attempts to add the string to the Element's value.
29
+ */
20
30
sendKeys ( keys : string ) : Promise < void > ;
31
+ /** Gets the text from the element. */
21
32
text ( ) : Promise < string > ;
33
+ /**
34
+ * Gets the value for the given attribute from the element. If the attribute does not exist,
35
+ * falls back to reading the property.
36
+ */
22
37
getAttribute ( name : string ) : Promise < string | null > ;
23
38
}
24
39
25
- /**
26
- * Extra searching options used by searching functions
27
- *
28
- * @param allowNull Optional, whether the found element can be null. If
29
- * allowNull is set, the searching function will always try to fetch the element
30
- * at once. When the element cannot be found, the searching function should
31
- * return null if allowNull is set to true, throw an error if allowNull is set
32
- * to false. If allowNull is not set, the framework will choose the behaviors
33
- * that make more sense for each test type (e.g. for unit test, the framework
34
- * will make sure the element is not null; otherwise throw an error); however,
35
- * the internal behavior is not guaranteed and user should not rely on it. Note
36
- * that in most cases, you don't need to care about whether an element is
37
- * present when loading the element and don't need to set this parameter unless
38
- * you do want to check whether the element is present when calling the
39
- * searching function. e.g. you want to make sure some element is not there when
40
- * loading the element in order to check whether a "ngif" works well.
41
- *
42
- * @param global Optional. If global is set to true, the selector will match any
43
- * element on the page and is not limited to the root of the harness. If
44
- * global is unset or set to false, the selector will only find elements under
45
- * the current root.
46
- */
47
- export interface Options {
40
+ /** Options that can be specified when querying for an Element. */
41
+ export interface QueryOptions {
42
+ /**
43
+ * Whether the found element can be null. If allowNull is set, the searching function will always
44
+ * try to fetch the element at once. When the element cannot be found, the searching function
45
+ * should return null if allowNull is set to true, throw an error if allowNull is set to false.
46
+ * If allowNull is not set, the framework will choose the behaviors that make more sense for each
47
+ * test type (e.g. for unit test, the framework will make sure the element is not null; otherwise
48
+ * throw an error); however, the internal behavior is not guaranteed and user should not rely on
49
+ * it. Note that in most cases, you don't need to care about whether an element is present when
50
+ * loading the element and don't need to set this parameter unless you do want to check whether
51
+ * the element is present when calling the searching function. e.g. you want to make sure some
52
+ * element is not there when loading the element in order to check whether a "ngif" works well.
53
+ */
48
54
allowNull ?: boolean ;
55
+ /**
56
+ * If global is set to true, the selector will match any element on the page and is not limited to
57
+ * the root of the harness. If global is unset or set to false, the selector will only find
58
+ * elements under the current root.
59
+ */
49
60
global ?: boolean ;
50
61
}
51
62
52
- /**
53
- * Type narrowing of Options to allow the overloads of ComponentHarness.find to
54
- * return null only if allowNull is set to true.
55
- */
56
- interface OptionsWithAllowNullSet extends Options {
57
- allowNull : true ;
58
- }
59
-
60
63
/**
61
64
* Locator interface
62
65
*/
@@ -71,7 +74,7 @@ export interface Locator {
71
74
* @param selector The CSS selector of the test elements.
72
75
* @param options Optional, extra searching options
73
76
*/
74
- querySelector ( selector : string , options ?: Options ) : Promise < TestElement | null > ;
77
+ querySelector ( selector : string , options ?: QueryOptions ) : Promise < TestElement | null > ;
75
78
76
79
/**
77
80
* Search all matched test elements under current root by CSS selector.
@@ -87,7 +90,7 @@ export interface Locator {
87
90
*/
88
91
load < T extends ComponentHarness > (
89
92
componentHarness : ComponentHarnessType < T > , root : string ,
90
- options ?: Options ) : Promise < T | null > ;
93
+ options ?: QueryOptions ) : Promise < T | null > ;
91
94
92
95
/**
93
96
* Load all Component Harnesses under current root.
@@ -127,7 +130,7 @@ export class ComponentHarness {
127
130
* @param selector The CSS selector of the test element.
128
131
* @param options Extra searching options
129
132
*/
130
- protected find ( selector : string , options : OptionsWithAllowNullSet ) :
133
+ protected find ( selector : string , options : QueryOptions & { allowNull : true } ) :
131
134
( ) => Promise < TestElement | null > ;
132
135
133
136
/**
@@ -136,7 +139,7 @@ export class ComponentHarness {
136
139
* @param selector The CSS selector of the test element.
137
140
* @param options Extra searching options
138
141
*/
139
- protected find ( selector : string , options : Options ) : ( ) => Promise < TestElement > ;
142
+ protected find ( selector : string , options : QueryOptions ) : ( ) => Promise < TestElement > ;
140
143
141
144
/**
142
145
* Generate a function to find the first matched Component Harness.
@@ -155,7 +158,7 @@ export class ComponentHarness {
155
158
*/
156
159
protected find < T extends ComponentHarness > (
157
160
componentHarness : ComponentHarnessType < T > , root : string ,
158
- options : OptionsWithAllowNullSet ) : ( ) => Promise < T | null > ;
161
+ options : QueryOptions & { allowNull : true } ) : ( ) => Promise < T | null > ;
159
162
160
163
/**
161
164
* Generate a function to find the first matched Component Harness.
@@ -165,15 +168,15 @@ export class ComponentHarness {
165
168
*/
166
169
protected find < T extends ComponentHarness > (
167
170
componentHarness : ComponentHarnessType < T > , root : string ,
168
- options : Options ) : ( ) => Promise < T > ;
171
+ options : QueryOptions ) : ( ) => Promise < T > ;
169
172
170
173
protected find < T extends ComponentHarness > (
171
174
selectorOrComponentHarness : string | ComponentHarnessType < T > ,
172
- selectorOrOptions ?: string | Options ,
173
- options ?: Options ) : ( ) => Promise < TestElement | T | null > {
175
+ selectorOrOptions ?: string | QueryOptions ,
176
+ options ?: QueryOptions ) : ( ) => Promise < TestElement | T | null > {
174
177
if ( typeof selectorOrComponentHarness === 'string' ) {
175
178
const selector = selectorOrComponentHarness ;
176
- return ( ) => this . locator . querySelector ( selector , selectorOrOptions as Options ) ;
179
+ return ( ) => this . locator . querySelector ( selector , selectorOrOptions as QueryOptions ) ;
177
180
} else {
178
181
const componentHarness = selectorOrComponentHarness ;
179
182
const selector = selectorOrOptions as string ;
0 commit comments