1
- import { By , promise as wdpromise , WebDriver , WebElement } from 'selenium-webdriver' ;
1
+ import { By , ByHash , promise as wdpromise , WebDriver , WebElement } from 'selenium-webdriver' ;
2
2
3
3
let clientSideScripts = require ( './clientsidescripts' ) ;
4
4
5
-
6
5
// Explicitly define webdriver.By.
6
+ // We do this because we want to inherit the static methods of webdriver.By, as opposed to
7
+ // inheriting from the webdriver.By class itself, which is actually analogous to ProtractorLocator.
7
8
export class WebdriverBy {
8
9
className : ( className : string ) => By = By . className ;
9
10
css : ( css : string ) => By = By . css ;
@@ -15,15 +16,21 @@ export class WebdriverBy {
15
16
tagName : ( tagName : string ) => By = By . tagName ;
16
17
xpath : ( xpath : string ) => By = By . xpath ;
17
18
}
19
+ export type WebDriverLocator = By | ByHash | Function ;
18
20
19
21
// Protractor locator strategy
20
- export interface Locator {
21
- findElementsOverride ? :
22
+ export interface ProtractorLocator {
23
+ findElementsOverride :
22
24
( driver : WebDriver , using: WebElement ,
23
25
rootSelector : string) => wdpromise . Promise < WebElement [ ] > ;
24
26
row ?: ( index : number ) => Locator ;
25
27
column ?: ( index : string ) => Locator ;
26
28
}
29
+ export type Locator = ProtractorLocator | WebDriverLocator ;
30
+
31
+ export function isProtractorLocator ( x : Locator ) : x is ProtractorLocator {
32
+ return x && ( typeof ( x as any ) . findElementsOverride === 'function' ) ;
33
+ }
27
34
28
35
/**
29
36
* The Protractor Locators. These provide ways of finding elements in
@@ -70,7 +77,7 @@ export class ProtractorBy extends WebdriverBy {
70
77
* element. It should return an array of elements.
71
78
*/
72
79
addLocator ( name : string , script : Function | string ) {
73
- this [ name ] = ( ...args : any [ ] ) : Locator => {
80
+ this [ name ] = ( ...args : any [ ] ) : ProtractorLocator => {
74
81
let locatorArguments = args ;
75
82
return {
76
83
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
@@ -119,9 +126,9 @@ export class ProtractorBy extends WebdriverBy {
119
126
* var deprecatedSyntax = element(by.binding('{{person.name}}'));
120
127
*
121
128
* @param {string } bindingDescriptor
122
- * @returns {Locator } location strategy
129
+ * @returns {ProtractorLocator } location strategy
123
130
*/
124
- binding ( bindingDescriptor : string ) : Locator {
131
+ binding ( bindingDescriptor : string ) : ProtractorLocator {
125
132
return {
126
133
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
127
134
wdpromise . Promise < WebElement [ ] > = > {
@@ -151,9 +158,9 @@ export class ProtractorBy extends WebdriverBy {
151
158
* expect(element(by.exactBinding('phone')).isPresent()).toBe(false);
152
159
*
153
160
* @param {string } bindingDescriptor
154
- * @returns {Locator } location strategy
161
+ * @returns {ProtractorLocator } location strategy
155
162
*/
156
- exactBinding ( bindingDescriptor : string ) : Locator {
163
+ exactBinding ( bindingDescriptor : string ) : ProtractorLocator {
157
164
return {
158
165
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
159
166
wdpromise . Promise < WebElement [ ] > = > {
@@ -179,9 +186,9 @@ export class ProtractorBy extends WebdriverBy {
179
186
* expect(input.getAttribute('value')).toBe('Foo123');
180
187
*
181
188
* @param {string } model ng-model expression.
182
- * @returns {Locator } location strategy
189
+ * @returns {ProtractorLocator } location strategy
183
190
*/
184
- model ( model : string ) : Locator {
191
+ model ( model : string ) : ProtractorLocator {
185
192
return {
186
193
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
187
194
wdpromise . Promise < WebElement [ ] > = > {
@@ -204,9 +211,9 @@ export class ProtractorBy extends WebdriverBy {
204
211
* element(by.buttonText('Save'));
205
212
*
206
213
* @param {string } searchText
207
- * @returns {Locator } location strategy
214
+ * @returns {ProtractorLocator } location strategy
208
215
*/
209
- buttonText ( searchText : string ) : Locator {
216
+ buttonText ( searchText : string ) : ProtractorLocator {
210
217
return {
211
218
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
212
219
wdpromise . Promise < WebElement [ ] > = > {
@@ -229,9 +236,9 @@ export class ProtractorBy extends WebdriverBy {
229
236
* element(by.partialButtonText('Save'));
230
237
*
231
238
* @param {string } searchText
232
- * @returns {Locator } location strategy
239
+ * @returns {ProtractorLocator } location strategy
233
240
*/
234
- partialButtonText ( searchText : string ) : Locator {
241
+ partialButtonText ( searchText : string ) : ProtractorLocator {
235
242
return {
236
243
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
237
244
wdpromise . Promise < WebElement [ ] > = > {
@@ -245,7 +252,7 @@ export class ProtractorBy extends WebdriverBy {
245
252
} ;
246
253
247
254
// Generate either by.repeater or by.exactRepeater
248
- private byRepeaterInner ( exact : boolean , repeatDescriptor : string) : Locator {
255
+ private byRepeaterInner ( exact : boolean , repeatDescriptor : string) : ProtractorLocator {
249
256
let name = 'by.' + ( exact ? 'exactR' : 'r' ) + 'epeater' ;
250
257
return {
251
258
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
@@ -256,7 +263,7 @@ export class ProtractorBy extends WebdriverBy {
256
263
toString : ( ) : string => {
257
264
return name + '("' + repeatDescriptor + '")' ;
258
265
} ,
259
- row : ( index : number ) : Locator => {
266
+ row : ( index : number ) : ProtractorLocator => {
260
267
return {
261
268
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
262
269
wdpromise . Promise < WebElement [ ] > = > {
@@ -267,7 +274,7 @@ export class ProtractorBy extends WebdriverBy {
267
274
toString : ( ) : string => {
268
275
return name + '(' + repeatDescriptor + '").row("' + index + '")"' ;
269
276
} ,
270
- column : ( binding : string) : Locator => {
277
+ column : ( binding : string) : ProtractorLocator => {
271
278
return {
272
279
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
273
280
wdpromise . Promise < WebElement [ ] > = > {
@@ -283,7 +290,7 @@ export class ProtractorBy extends WebdriverBy {
283
290
}
284
291
} ;
285
292
} ,
286
- column : ( binding : string) : Locator => {
293
+ column : ( binding : string) : ProtractorLocator => {
287
294
return {
288
295
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
289
296
wdpromise . Promise < WebElement [ ] > = > {
@@ -294,7 +301,7 @@ export class ProtractorBy extends WebdriverBy {
294
301
toString : ( ) : string => {
295
302
return name + '("' + repeatDescriptor + '").column("' + binding + '")' ;
296
303
} ,
297
- row : ( index : number ) : Locator => {
304
+ row : ( index : number ) : ProtractorLocator => {
298
305
return {
299
306
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
300
307
wdpromise . Promise < WebElement [ ] > = > {
@@ -365,9 +372,9 @@ export class ProtractorBy extends WebdriverBy {
365
372
* var divs = element.all(by.repeater('book in library'));
366
373
*
367
374
* @param {string } repeatDescriptor
368
- * @returns {Locator } location strategy
375
+ * @returns {ProtractorLocator } location strategy
369
376
*/
370
- repeater ( repeatDescriptor : string) : Locator {
377
+ repeater ( repeatDescriptor : string) : ProtractorLocator {
371
378
return this . byRepeaterInner ( false , repeatDescriptor ) ;
372
379
}
373
380
@@ -387,9 +394,9 @@ export class ProtractorBy extends WebdriverBy {
387
394
* expect(element(by.exactRepeater('car in cars')).isPresent()).toBe(true);
388
395
*
389
396
* @param {string } repeatDescriptor
390
- * @returns {Locator } location strategy
397
+ * @returns {ProtractorLocator } location strategy
391
398
*/
392
- exactRepeater ( repeatDescriptor : string) : Locator {
399
+ exactRepeater ( repeatDescriptor : string) : ProtractorLocator {
393
400
return this . byRepeaterInner ( true , repeatDescriptor ) ;
394
401
}
395
402
@@ -408,9 +415,9 @@ export class ProtractorBy extends WebdriverBy {
408
415
*
409
416
* @param {string } cssSelector css selector
410
417
* @param {string } searchString text search
411
- * @returns {Locator } location strategy
418
+ * @returns {ProtractorLocator } location strategy
412
419
*/
413
- cssContainingText ( cssSelector : string, searchText : string) : Locator {
420
+ cssContainingText ( cssSelector : string, searchText : string) : ProtractorLocator {
414
421
return {
415
422
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
416
423
wdpromise . Promise < WebElement [ ] > = > {
@@ -441,9 +448,9 @@ export class ProtractorBy extends WebdriverBy {
441
448
* expect(firstOption.getText()).toEqual('red');
442
449
*
443
450
* @param {string } optionsDescriptor ng-options expression.
444
- * @returns {Locator } location strategy
451
+ * @returns {ProtractorLocator } location strategy
445
452
*/
446
- options ( optionsDescriptor : string) : Locator {
453
+ options ( optionsDescriptor : string) : ProtractorLocator {
447
454
return {
448
455
findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
449
456
wdpromise . Promise < WebElement [ ] > = > {
0 commit comments