10
10
// package? It depends on protractor which we don't want to put in the deps for cdk-experimental.
11
11
12
12
import { browser , by , element as protractorElement , ElementFinder } from 'protractor' ;
13
- import { promise as wdpromise } from 'selenium-webdriver' ;
14
13
15
14
import {
16
15
ComponentHarness ,
@@ -28,33 +27,29 @@ import {
28
27
* loading harness, using the load function that accepts extra searching
29
28
* options.
30
29
* @param componentHarness: Type of user defined harness.
31
- * @param rootSelector: Optional. Css selector to specify the root of component.
30
+ * @param rootSelector: Optional. CSS selector to specify the root of component.
32
31
* Set to 'body' by default
33
32
*/
34
33
export async function load < T extends ComponentHarness > (
35
- componentHarness : ComponentHarnessType < T > ,
36
- rootSelector : string ) : Promise < T > ;
34
+ componentHarness : ComponentHarnessType < T > ,
35
+ rootSelector : string ) : Promise < T > ;
37
36
38
37
/**
39
38
* Component harness factory for protractor.
40
39
* @param componentHarness: Type of user defined harness.
41
- * @param rootSelector: Optional. Css selector to specify the root of component.
40
+ * @param rootSelector: Optional. CSS selector to specify the root of component.
42
41
* Set to 'body' by default.
43
42
* @param options Optional. Extra searching options
44
43
*/
45
44
export async function load < T extends ComponentHarness > (
46
- componentHarness : ComponentHarnessType < T > , rootSelector ?: string ,
47
- options ?: Options ) : Promise < T | null > ;
45
+ componentHarness : ComponentHarnessType < T > , rootSelector ?: string ,
46
+ options ?: Options ) : Promise < T | null > ;
48
47
49
48
export async function load < T extends ComponentHarness > (
50
- componentHarness : ComponentHarnessType < T > , rootSelector = 'body' ,
51
- options ?: Options ) : Promise < T | null > {
49
+ componentHarness : ComponentHarnessType < T > , rootSelector = 'body' ,
50
+ options ?: Options ) : Promise < T | null > {
52
51
const root = await getElement ( rootSelector , undefined , options ) ;
53
- if ( root === null ) {
54
- return null ;
55
- }
56
- const locator = new ProtractorLocator ( root ) ;
57
- return new componentHarness ( locator ) ;
52
+ return root && new componentHarness ( new ProtractorLocator ( root ) ) ;
58
53
}
59
54
60
55
/**
@@ -69,7 +64,7 @@ export function getElementFinder(testElement: TestElement): ElementFinder {
69
64
}
70
65
71
66
class ProtractorLocator implements Locator {
72
- private _root : ProtractorElement ;
67
+ private readonly _root : ProtractorElement ;
73
68
74
69
constructor ( private _rootFinder : ElementFinder ) {
75
70
this . _root = new ProtractorElement ( this . _rootFinder ) ;
@@ -79,16 +74,13 @@ class ProtractorLocator implements Locator {
79
74
return this . _root ;
80
75
}
81
76
82
- async find ( css : string , options ?: Options ) : Promise < TestElement | null > {
83
- const finder = await getElement ( css , this . _rootFinder , options ) ;
84
- if ( finder === null ) {
85
- return null ;
86
- }
87
- return new ProtractorElement ( finder ) ;
77
+ async querySelector ( selector : string , options ?: Options ) : Promise < TestElement | null > {
78
+ const finder = await getElement ( selector , this . _rootFinder , options ) ;
79
+ return finder && new ProtractorElement ( finder ) ;
88
80
}
89
81
90
- async findAll ( css : string ) : Promise < TestElement [ ] > {
91
- const elementFinders = this . _rootFinder . all ( by . css ( css ) ) ;
82
+ async querySelectorAll ( selector : string ) : Promise < TestElement [ ] > {
83
+ const elementFinders = this . _rootFinder . all ( by . css ( selector ) ) ;
92
84
const res : TestElement [ ] = [ ] ;
93
85
await elementFinders . each ( el => {
94
86
if ( el ) {
@@ -99,20 +91,15 @@ class ProtractorLocator implements Locator {
99
91
}
100
92
101
93
async load < T extends ComponentHarness > (
102
- componentHarness : ComponentHarnessType < T > , css : string ,
103
- options ?: Options ) : Promise < T | null > {
104
- const root = await getElement ( css , this . _rootFinder , options ) ;
105
- if ( root === null ) {
106
- return null ;
107
- }
108
- const locator = new ProtractorLocator ( root ) ;
109
- return new componentHarness ( locator ) ;
94
+ componentHarness : ComponentHarnessType < T > , selector : string ,
95
+ options ?: Options ) : Promise < T | null > {
96
+ const root = await getElement ( selector , this . _rootFinder , options ) ;
97
+ return root && new componentHarness ( new ProtractorLocator ( root ) ) ;
110
98
}
111
99
112
100
async loadAll < T extends ComponentHarness > (
113
- componentHarness : ComponentHarnessType < T > ,
114
- rootSelector : string ,
115
- ) : Promise < T [ ] > {
101
+ componentHarness : ComponentHarnessType < T > ,
102
+ rootSelector : string ) : Promise < T [ ] > {
116
103
const roots = this . _rootFinder . all ( by . css ( rootSelector ) ) ;
117
104
const res : T [ ] = [ ] ;
118
105
await roots . each ( el => {
@@ -128,77 +115,66 @@ class ProtractorLocator implements Locator {
128
115
class ProtractorElement implements TestElement {
129
116
constructor ( readonly element : ElementFinder ) { }
130
117
131
- blur ( ) : Promise < void > {
132
- return toPromise < void > ( this . element [ 'blur' ] ( ) ) ;
118
+ async blur ( ) : Promise < void > {
119
+ return this . element [ 'blur' ] ( ) ;
133
120
}
134
121
135
- clear ( ) : Promise < void > {
136
- return toPromise < void > ( this . element . clear ( ) ) ;
122
+ async clear ( ) : Promise < void > {
123
+ return this . element . clear ( ) ;
137
124
}
138
125
139
- click ( ) : Promise < void > {
140
- return toPromise < void > ( this . element . click ( ) ) ;
126
+ async click ( ) : Promise < void > {
127
+ return this . element . click ( ) ;
141
128
}
142
129
143
- focus ( ) : Promise < void > {
144
- return toPromise < void > ( this . element [ 'focus' ] ( ) ) ;
130
+ async focus ( ) : Promise < void > {
131
+ return this . element [ 'focus' ] ( ) ;
145
132
}
146
133
147
- getCssValue ( property : string ) : Promise < string > {
148
- return toPromise < string > ( this . element . getCssValue ( property ) ) ;
134
+ async getCssValue ( property : string ) : Promise < string > {
135
+ return this . element . getCssValue ( property ) ;
149
136
}
150
137
151
138
async hover ( ) : Promise < void > {
152
- return toPromise < void > ( browser . actions ( )
153
- . mouseMove ( await this . element . getWebElement ( ) )
154
- . perform ( ) ) ;
139
+ return browser . actions ( )
140
+ . mouseMove ( await this . element . getWebElement ( ) )
141
+ . perform ( ) ;
155
142
}
156
143
157
- sendKeys ( keys : string ) : Promise < void > {
158
- return toPromise < void > ( this . element . sendKeys ( keys ) ) ;
144
+ async sendKeys ( keys : string ) : Promise < void > {
145
+ return this . element . sendKeys ( keys ) ;
159
146
}
160
147
161
- text ( ) : Promise < string > {
162
- return toPromise ( this . element . getText ( ) ) ;
148
+ async text ( ) : Promise < string > {
149
+ return this . element . getText ( ) ;
163
150
}
164
151
165
- getAttribute ( name : string ) : Promise < string | null > {
166
- return toPromise ( this . element . getAttribute ( name ) ) ;
152
+ async getAttribute ( name : string ) : Promise < string | null > {
153
+ return this . element . getAttribute ( name ) ;
167
154
}
168
155
}
169
156
170
- function toPromise < T > ( p : wdpromise . Promise < T > ) : Promise < T > {
171
- return new Promise < T > ( ( resolve , reject ) => {
172
- p . then ( resolve , reject ) ;
173
- } ) ;
174
- }
175
-
176
157
/**
177
- * Get an element finder based on the css selector and root element.
158
+ * Get an element finder based on the CSS selector and root element.
178
159
* Note that it will check whether the element is present only when
179
160
* Options.allowNull is set. This is for performance purpose.
180
- * @param css the css selector
161
+ * @param selector The CSS selector
181
162
* @param root Optional Search element under the root element. If not set,
182
163
* search element globally. If options.global is set, root is ignored.
183
164
* @param options Optional, extra searching options
184
165
*/
185
- async function getElement ( css : string , root ?: ElementFinder , options ?: Options ) :
166
+ async function getElement ( selector : string , root ?: ElementFinder , options ?: Options ) :
186
167
Promise < ElementFinder | null > {
187
168
const useGlobalRoot = options && ! ! options . global ;
188
- const elem = root === undefined || useGlobalRoot ? protractorElement ( by . css ( css ) ) :
189
- root . element ( by . css ( css ) ) ;
169
+ const elem = root === undefined || useGlobalRoot ?
170
+ protractorElement ( by . css ( selector ) ) : root . element ( by . css ( selector ) ) ;
190
171
const allowNull = options !== undefined && options . allowNull !== undefined ?
191
- options . allowNull :
192
- undefined ;
193
- if ( allowNull !== undefined ) {
194
- // Only check isPresent when allowNull is set
195
- if ( ! ( await elem . isPresent ( ) ) ) {
196
- if ( allowNull ) {
197
- return null ;
198
- }
199
- throw new Error ( 'Cannot find element based on the css selector: ' + css ) ;
172
+ options . allowNull : undefined ;
173
+ if ( allowNull !== undefined && ! ( await elem . isPresent ( ) ) ) {
174
+ if ( allowNull ) {
175
+ return null ;
200
176
}
201
- return elem ;
177
+ throw new Error ( 'Cannot find element based on the CSS selector: ' + selector ) ;
202
178
}
203
179
return elem ;
204
180
}
0 commit comments