1- import { By , promise as wdpromise , WebDriver , WebElement } from 'selenium-webdriver' ;
1+ import { By , ByHash , promise as wdpromise , WebDriver , WebElement } from 'selenium-webdriver' ;
22
33let clientSideScripts = require ( './clientsidescripts' ) ;
44
5-
65// 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.
78export class WebdriverBy {
89 className : ( className : string ) => By = By . className ;
910 css : ( css : string ) => By = By . css ;
@@ -15,15 +16,21 @@ export class WebdriverBy {
1516 tagName : ( tagName : string ) => By = By . tagName ;
1617 xpath : ( xpath : string ) => By = By . xpath ;
1718}
19+ export type WebDriverLocator = By | ByHash | Function ;
1820
1921// Protractor locator strategy
20- export interface Locator {
21- findElementsOverride ? :
22+ export interface ProtractorLocator {
23+ findElementsOverride :
2224 ( driver : WebDriver , using: WebElement ,
2325 rootSelector : string) => wdpromise . Promise < WebElement [ ] > ;
2426 row ?: ( index : number ) => Locator ;
2527 column ?: ( index : string ) => Locator ;
2628}
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+ }
2734
2835/**
2936 * The Protractor Locators. These provide ways of finding elements in
@@ -70,7 +77,7 @@ export class ProtractorBy extends WebdriverBy {
7077 * element. It should return an array of elements.
7178 */
7279 addLocator ( name : string , script : Function | string ) {
73- this [ name ] = ( ...args : any [ ] ) : Locator => {
80+ this [ name ] = ( ...args : any [ ] ) : ProtractorLocator => {
7481 let locatorArguments = args ;
7582 return {
7683 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
@@ -119,9 +126,9 @@ export class ProtractorBy extends WebdriverBy {
119126 * var deprecatedSyntax = element(by.binding('{{person.name}}'));
120127 *
121128 * @param {string } bindingDescriptor
122- * @returns {Locator } location strategy
129+ * @returns {ProtractorLocator } location strategy
123130 */
124- binding ( bindingDescriptor : string ) : Locator {
131+ binding ( bindingDescriptor : string ) : ProtractorLocator {
125132 return {
126133 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
127134 wdpromise . Promise < WebElement [ ] > = > {
@@ -151,9 +158,9 @@ export class ProtractorBy extends WebdriverBy {
151158 * expect(element(by.exactBinding('phone')).isPresent()).toBe(false);
152159 *
153160 * @param {string } bindingDescriptor
154- * @returns {Locator } location strategy
161+ * @returns {ProtractorLocator } location strategy
155162 */
156- exactBinding ( bindingDescriptor : string ) : Locator {
163+ exactBinding ( bindingDescriptor : string ) : ProtractorLocator {
157164 return {
158165 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
159166 wdpromise . Promise < WebElement [ ] > = > {
@@ -179,9 +186,9 @@ export class ProtractorBy extends WebdriverBy {
179186 * expect(input.getAttribute('value')).toBe('Foo123');
180187 *
181188 * @param {string } model ng-model expression.
182- * @returns {Locator } location strategy
189+ * @returns {ProtractorLocator } location strategy
183190 */
184- model ( model : string ) : Locator {
191+ model ( model : string ) : ProtractorLocator {
185192 return {
186193 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
187194 wdpromise . Promise < WebElement [ ] > = > {
@@ -204,9 +211,9 @@ export class ProtractorBy extends WebdriverBy {
204211 * element(by.buttonText('Save'));
205212 *
206213 * @param {string } searchText
207- * @returns {Locator } location strategy
214+ * @returns {ProtractorLocator } location strategy
208215 */
209- buttonText ( searchText : string ) : Locator {
216+ buttonText ( searchText : string ) : ProtractorLocator {
210217 return {
211218 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
212219 wdpromise . Promise < WebElement [ ] > = > {
@@ -229,9 +236,9 @@ export class ProtractorBy extends WebdriverBy {
229236 * element(by.partialButtonText('Save'));
230237 *
231238 * @param {string } searchText
232- * @returns {Locator } location strategy
239+ * @returns {ProtractorLocator } location strategy
233240 */
234- partialButtonText ( searchText : string ) : Locator {
241+ partialButtonText ( searchText : string ) : ProtractorLocator {
235242 return {
236243 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
237244 wdpromise . Promise < WebElement [ ] > = > {
@@ -245,7 +252,7 @@ export class ProtractorBy extends WebdriverBy {
245252 } ;
246253
247254 // Generate either by.repeater or by.exactRepeater
248- private byRepeaterInner ( exact : boolean , repeatDescriptor : string) : Locator {
255+ private byRepeaterInner ( exact : boolean , repeatDescriptor : string) : ProtractorLocator {
249256 let name = 'by.' + ( exact ? 'exactR' : 'r' ) + 'epeater' ;
250257 return {
251258 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
@@ -256,7 +263,7 @@ export class ProtractorBy extends WebdriverBy {
256263 toString : ( ) : string => {
257264 return name + '("' + repeatDescriptor + '")' ;
258265 } ,
259- row : ( index : number ) : Locator => {
266+ row : ( index : number ) : ProtractorLocator => {
260267 return {
261268 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
262269 wdpromise . Promise < WebElement [ ] > = > {
@@ -267,7 +274,7 @@ export class ProtractorBy extends WebdriverBy {
267274 toString : ( ) : string => {
268275 return name + '(' + repeatDescriptor + '").row("' + index + '")"' ;
269276 } ,
270- column : ( binding : string) : Locator => {
277+ column : ( binding : string) : ProtractorLocator => {
271278 return {
272279 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
273280 wdpromise . Promise < WebElement [ ] > = > {
@@ -283,7 +290,7 @@ export class ProtractorBy extends WebdriverBy {
283290 }
284291 } ;
285292 } ,
286- column : ( binding : string) : Locator => {
293+ column : ( binding : string) : ProtractorLocator => {
287294 return {
288295 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
289296 wdpromise . Promise < WebElement [ ] > = > {
@@ -294,7 +301,7 @@ export class ProtractorBy extends WebdriverBy {
294301 toString : ( ) : string => {
295302 return name + '("' + repeatDescriptor + '").column("' + binding + '")' ;
296303 } ,
297- row : ( index : number ) : Locator => {
304+ row : ( index : number ) : ProtractorLocator => {
298305 return {
299306 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
300307 wdpromise . Promise < WebElement [ ] > = > {
@@ -365,9 +372,9 @@ export class ProtractorBy extends WebdriverBy {
365372 * var divs = element.all(by.repeater('book in library'));
366373 *
367374 * @param {string } repeatDescriptor
368- * @returns {Locator } location strategy
375+ * @returns {ProtractorLocator } location strategy
369376 */
370- repeater ( repeatDescriptor : string) : Locator {
377+ repeater ( repeatDescriptor : string) : ProtractorLocator {
371378 return this . byRepeaterInner ( false , repeatDescriptor ) ;
372379 }
373380
@@ -387,9 +394,9 @@ export class ProtractorBy extends WebdriverBy {
387394 * expect(element(by.exactRepeater('car in cars')).isPresent()).toBe(true);
388395 *
389396 * @param {string } repeatDescriptor
390- * @returns {Locator } location strategy
397+ * @returns {ProtractorLocator } location strategy
391398 */
392- exactRepeater ( repeatDescriptor : string) : Locator {
399+ exactRepeater ( repeatDescriptor : string) : ProtractorLocator {
393400 return this . byRepeaterInner ( true , repeatDescriptor ) ;
394401 }
395402
@@ -408,9 +415,9 @@ export class ProtractorBy extends WebdriverBy {
408415 *
409416 * @param {string } cssSelector css selector
410417 * @param {string } searchString text search
411- * @returns {Locator } location strategy
418+ * @returns {ProtractorLocator } location strategy
412419 */
413- cssContainingText ( cssSelector : string, searchText : string) : Locator {
420+ cssContainingText ( cssSelector : string, searchText : string) : ProtractorLocator {
414421 return {
415422 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
416423 wdpromise . Promise < WebElement [ ] > = > {
@@ -441,9 +448,9 @@ export class ProtractorBy extends WebdriverBy {
441448 * expect(firstOption.getText()).toEqual('red');
442449 *
443450 * @param {string } optionsDescriptor ng-options expression.
444- * @returns {Locator } location strategy
451+ * @returns {ProtractorLocator } location strategy
445452 */
446- options ( optionsDescriptor : string) : Locator {
453+ options ( optionsDescriptor : string) : ProtractorLocator {
447454 return {
448455 findElementsOverride : ( driver : WebDriver , using: WebElement , rootSelector : string) :
449456 wdpromise . Promise < WebElement [ ] > = > {
0 commit comments