/
protractor.js
1105 lines (1013 loc) · 32.6 KB
/
protractor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var url = require('url');
var util = require('util');
var path = require('path');
var webdriver = require('selenium-webdriver');
var clientSideScripts = require('./clientsidescripts.js');
var ProtractorBy = require('./locators.js').ProtractorBy;
var DEFER_LABEL = 'NG_DEFER_BOOTSTRAP!';
var WEB_ELEMENT_FUNCTIONS = [
'click', 'sendKeys', 'getTagName', 'getCssValue', 'getAttribute', 'getText',
'getSize', 'getLocation', 'isEnabled', 'isSelected', 'submit', 'clear',
'isDisplayed', 'getOuterHtml', 'getInnerHtml', 'toWireValue'];
var STACK_SUBSTRINGS_TO_FILTER = [
'node_modules/minijasminenode/lib/',
'node_modules/selenium-webdriver',
'at Module.',
'at Object.Module.',
'at Function.Module',
'(timers.js:',
'jasminewd/index.js',
'protractor/lib/'
];
/*
* Mix in other webdriver functionality to be accessible via protractor.
*/
for (var foo in webdriver) {
exports[foo] = webdriver[foo];
}
/**
* @type {ProtractorBy}
*/
exports.By = new ProtractorBy();
/**
* Mix a function from one object onto another. The function will still be
* called in the context of the original object.
*
* @private
* @param {Object} to
* @param {Object} from
* @param {string} fnName
* @param {function=} setupFn
*/
var mixin = function(to, from, fnName, setupFn) {
to[fnName] = function() {
if (setupFn) {
setupFn();
}
return from[fnName].apply(from, arguments);
};
};
/**
* Build the helper 'element' function for a given instance of Protractor.
*
* @private
* @param {Protractor} ptor
* @param {Array.<webdriver.Locator>=} opt_usingChain
* @return {function(webdriver.Locator): ElementFinder}
*/
var buildElementHelper = function(ptor, opt_usingChain) {
var usingChain = opt_usingChain || [];
var using = function() {
var base = ptor;
for (var i = 0; i < usingChain.length; ++i) {
base = base.findElement(usingChain[i]);
}
return base;
};
/**
* The element function returns an Element Finder. Element Finders do
* not actually attempt to find the element until a method is called on them,
* which means they can be set up in helper files before the page is
* available.
*
* @alias element(locator)
* @view
* <span>{{person.name}}</span>
* <span ng-bind="person.email"></span>
* <input type="text" ng-model="person.name"/>
*
* @example
* // Find element with {{scopeVar}} syntax.
* element(by.binding('person.name')).getText().then(function(name) {
* expect(name).toBe('Foo');
* });
*
* // Find element with ng-bind="scopeVar" syntax.
* expect(element(by.binding('person.email')).getText()).toBe('foo@bar.com');
*
* // Find by model.
* var input = element(by.model('person.name'));
* input.sendKeys('123');
* expect(input.getAttribute('value')).toBe('Foo123');
*
* @param {webdriver.Locator} locator An element locator.
* @return {ElementFinder}
*/
var element = function(locator) {
var elementFinder = {};
var webElementFns = WEB_ELEMENT_FUNCTIONS.concat(
['findElements', 'isElementPresent', 'evaluate', '$$']);
webElementFns.forEach(function(fnName) {
elementFinder[fnName] = function() {
var callerError = new Error();
var args = arguments;
return using().findElement(locator).then(function(element) {
return element[fnName].apply(element, args).then(null, function(e) {
e.stack = e.stack + '\n' + callerError.stack;
throw e;
});
});
};
});
// This is a special case since it doesn't return a promise, instead it
// returns a WebElement.
elementFinder.findElement = function(subLocator) {
return using().findElement(locator).findElement(subLocator);
};
/**
* Return the actual WebElement.
*
* @alias element(locator).find()
* @return {webdriver.WebElement}
*/
elementFinder.find = function() {
return using().findElement(locator);
};
/**
* Determine whether an element is present on the page.
*
* @alias element(locator).isPresent()
*
* @view
* <span>{{person.name}}</span>
*
* @example
* // Element exists.
* expect(element(by.binding('person.name')).isPresent()).toBe(true);
*
* // Element not present.
* expect(element(by.binding('notPresent')).isPresent()).toBe(false);
*
* @return {!webdriver.promise.Promise} A promise which resolves to a
* boolean.
*/
elementFinder.isPresent = function() {
return using().isElementPresent(locator);
};
/**
* Calls to element may be chained to find elements within a parent.
*
* @alias element(locator).element(locator)
* @view
* <div class="parent">
* <div class="child">
* Child text
* <div>{{person.phone}}</div>
* </div>
* </div>
*
* @example
* // Chain 2 element calls.
* var child = element(by.css('.parent')).
* element(by.css('.child'));
* expect(child.getText()).toBe('Child text\n555-123-4567');
*
* // Chain 3 element calls.
* var triple = element(by.css('.parent')).
* element(by.css('.child')).
* element(by.binding('person.phone'));
* expect(triple.getText()).toBe('555-123-4567');
*
* @param {Protractor} ptor
* @param {Array.<webdriver.Locator>=} opt_usingChain
* @return {function(webdriver.Locator): ElementFinder}
*/
elementFinder.element =
buildElementHelper(ptor, usingChain.concat(locator));
/**
* Shortcut for chaining css element finders.
*
* @alias element(locator).$(cssSelector)
* @view
* <div class="parent">
* <div class="child">
* Child text
* <div class="grandchild">{{person.phone}}</div>
* </div>
* </div>
*
* @example
* // Chain 2 element calls.
* var child = element(by.css('.parent')).$('.child');
* expect(child.getText()).toBe('Child text\n555-123-4567');
*
* // Chain 3 element calls.
* var triple = $('.parent').$('.child').$('.grandchild');
* expect(triple.getText()).toBe('555-123-4567');
*
* @param {string} cssSelector A css selector.
* @return {ElementFinder}
*/
elementFinder.$ = function(cssSelector) {
return buildElementHelper(ptor, usingChain.concat(locator))(
webdriver.By.css(cssSelector));
};
return elementFinder;
};
/**
* element.all is used for operations on an array of elements (as opposed
* to a single element).
*
* @alias element.all(locator)
* @view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* @example
* element.all(by.css('.items li')).then(function(items) {
* expect(items.length).toBe(3);
* expect(items[0].getText()).toBe('First');
* });
*
* @param {webdriver.Locator} locator
* @return {ElementArrayFinder}
*/
element.all = function(locator) {
var elementArrayFinder = {};
/**
* Count the number of elements found by the locator.
*
* @alias element.all(locator).count()
* @view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* @example
* var list = element.all(by.css('.items li'));
* expect(list.count()).toBe(3);
*
* @return {!webdriver.promise.Promise} A promise which resolves to the
* number of elements matching the locator.
*/
elementArrayFinder.count = function() {
return using().findElements(locator).then(function(arr) {
return arr.length;
});
};
/**
* Get an element found by the locator by index. The index starts at 0.
*
* @alias element.all(locator).get(index)
* @view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* @example
* var list = element.all(by.css('.items li'));
* expect(list.get(0).getText()).toBe('First');
* expect(list.get(1).getText()).toBe('Second');
*
* @param {number} index Element index.
* @return {webdriver.WebElement} The element at the given index
*/
elementArrayFinder.get = function(index) {
var id = using().findElements(locator).then(function(arr) {
return arr[index];
});
return ptor.wrapWebElement(new webdriver.WebElement(ptor.driver, id));
};
/**
* Get the first element found using the locator.
*
* @alias element.all(locator).first()
* @view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* @example
* var list = element.all(by.css('.items li'));
* expect(list.first().getText()).toBe('First');
*
* @return {webdriver.WebElement} The first matching element
*/
elementArrayFinder.first = function() {
var id = using().findElements(locator).then(function(arr) {
if (!arr.length) {
throw new Error('No element found using locator: ' + locator.message);
}
return arr[0];
});
return ptor.wrapWebElement(new webdriver.WebElement(ptor.driver, id));
};
/**
* Get the last matching element for the locator.
*
* @alias element.all(locator).last()
* @view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* @example
* var list = element.all(by.css('.items li'));
* expect(list.last().getText()).toBe('Third');
*
* @return {webdriver.WebElement} the last matching element
*/
elementArrayFinder.last = function() {
var id = using().findElements(locator).then(function(arr) {
return arr[arr.length - 1];
});
return ptor.wrapWebElement(new webdriver.WebElement(ptor.driver, id));
};
/**
* @type {webdriver.promise.Promise} a promise which will resolve to
* an array of WebElements matching the locator.
*/
elementArrayFinder.then = function(fn) {
return using().findElements(locator).then(fn);
};
/**
* Calls the input function on each WebElement found by the locator.
*
* @alias element.all(locator).each(eachFunction)
* @view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* @example
* element.all(by.css('.items li')).each(function(element) {
* // Will print First, Second, Third.
* element.getText().then(console.log);
* });
*
* @param {function(webdriver.WebElement)} fn Input function
*/
elementArrayFinder.each = function(fn) {
using().findElements(locator).then(function(arr) {
arr.forEach(function(webElem) {
fn(webElem);
});
});
};
/**
* Apply a map function to each element found using the locator. The
* callback receives the web element as the first argument and the index as
* a second arg.
*
* @alias element.all(locator).map(mapFunction)
* @view
* <ul class="items">
* <li class="one">First</li>
* <li class="two">Second</li>
* <li class="three">Third</li>
* </ul>
*
* @example
* var items = element.all(by.css('.items li')).map(function(elm, index) {
* return {
* index: index,
* text: elm.getText(),
* class: elm.getAttribute('class')
* };
* });
* expect(items).toEqual([
* {index: 0, text: 'First', class: 'one'},
* {index: 1, text: 'Second', class: 'two'},
* {index: 2, text: 'Third', class: 'three'}
* ]);
*
* @param {function(webdriver.WebElement, number)} mapFn Map function that
* will be applied to each element.
* @return {!webdriver.promise.Promise} A promise that resolves to an array
* of values returned by the map function.
*/
elementArrayFinder.map = function(mapFn) {
return using().findElements(locator).then(function(arr) {
var list = [];
arr.forEach(function(webElem, index) {
var mapResult = mapFn(webElem, index);
// All nested arrays and objects will also be fully resolved.
webdriver.promise.fullyResolved(mapResult).then(function(resolved) {
list.push(resolved);
});
});
return list;
});
};
return elementArrayFinder;
};
return element;
};
/**
* Build the helper '$' function for a given instance of Protractor.
*
* @private
* @param {Protractor} ptor
* @return {function(string): ElementFinder}
*/
var buildCssHelper = function(ptor) {
return function(cssSelector) {
return buildElementHelper(ptor)(webdriver.By.css(cssSelector));
};
};
/**
* Build the helper '$$' function for a given instance of Protractor.
*
* @private
* @param {Protractor} ptor
* @return {function(string): ElementArrayFinder}
*/
var buildMultiCssHelper = function(ptor) {
return function(cssSelector) {
return buildElementHelper(ptor).all(webdriver.By.css(cssSelector));
};
};
/**
* @param {webdriver.WebDriver} webdriver
* @param {string=} opt_baseUrl A base URL to run get requests against.
* @param {string=} opt_rootElement Selector element that has an ng-app in
* scope.
* @constructor
*/
var Protractor = function(webdriver, opt_baseUrl, opt_rootElement) {
// These functions should delegate to the webdriver instance, but should
// wait for Angular to sync up before performing the action. This does not
// include functions which are overridden by protractor below.
var methodsToSync = ['getCurrentUrl', 'getPageSource', 'getTitle'];
// Mix all other driver functionality into Protractor.
for (var method in webdriver) {
if(!this[method] && typeof webdriver[method] == 'function') {
if (methodsToSync.indexOf(method) !== -1) {
mixin(this, webdriver, method, this.waitForAngular.bind(this));
} else {
mixin(this, webdriver, method);
}
}
}
/**
* The wrapped webdriver instance. Use this to interact with pages that do
* not contain Angular (such as a log-in screen).
*
* @type {webdriver.WebDriver}
*/
this.driver = webdriver;
/**
* Helper function for finding elements.
*
* @type {function(webdriver.Locator): ElementFinder}
*/
this.element = buildElementHelper(this);
/**
* Helper function for finding elements by css.
*
* @type {function(string): ElementFinder}
*/
this.$ = buildCssHelper(this);
/**
* Helper function for finding arrays of elements by css.
*
* @type {function(string): ElementArrayFinder}
*/
this.$$ = buildMultiCssHelper(this);
/**
* All get methods will be resolved against this base URL. Relative URLs are =
* resolved the way anchor tags resolve.
*
* @type {string}
*/
this.baseUrl = opt_baseUrl || '';
/**
* The css selector for an element on which to find Angular. This is usually
* 'body' but if your ng-app is on a subsection of the page it may be
* a subelement.
*
* @type {string}
*/
this.rootEl = opt_rootElement || 'body';
/**
* If true, Protractor will not attempt to synchronize with the page before
* performing actions. This can be harmful because Protractor will not wait
* until $timeouts and $http calls have been processed, which can cause
* tests to become flaky. This should be used only when necessary, such as
* when a page continuously polls an API using $timeout.
*
* @type {boolean}
*/
this.ignoreSynchronization = false;
/**
* An object that holds custom test parameters.
*
* @type {Object}
*/
this.params = {};
this.moduleNames_ = [];
this.moduleScripts_ = [];
};
/**
* Instruct webdriver to wait until Angular has finished rendering and has
* no outstanding $http calls before continuing.
*
* @return {!webdriver.promise.Promise} A promise that will resolve to the
* scripts return value.
*/
Protractor.prototype.waitForAngular = function() {
if (this.ignoreSynchronization) {
return webdriver.promise.fulfilled();
}
return this.driver.executeAsyncScript(
clientSideScripts.waitForAngular, this.rootEl).then(function(browserErr) {
if (browserErr) {
throw 'Error while waiting for Protractor to ' +
'sync with the page: ' + JSON.stringify(browserErr);
}
}).then(null, function(err) {
var timeout;
if (/asynchronous script timeout/.test(err.message)) {
// Timeout on Chrome
timeout = /-?[\d\.]*\ seconds/.exec(err.message);
} else if (/Timed out waiting for async script/.test(err.message)) {
// Timeout on Firefox
timeout = /-?[\d\.]*ms/.exec(err.message);
} else if (/Timed out waiting for an asynchronous script/.test(err.message)) {
// Timeout on Safari
timeout = /-?[\d\.]*\ ms/.exec(err.message);
}
if (timeout) {
throw 'Timed out waiting for Protractor to synchronize with ' +
'the page after ' + timeout + '. Please see ' +
'https://github.com/angular/protractor/blob/master/docs/faq.md';
} else {
throw err;
}
});
};
// TODO: activeelement also returns a WebElement.
/**
* Wrap a webdriver.WebElement with protractor specific functionality.
*
* @param {webdriver.WebElement} element
* @return {webdriver.WebElement} the wrapped web element.
*/
Protractor.prototype.wrapWebElement = function(element) {
var thisPtor = this;
// Before any of the WebElement functions, Protractor will wait to make sure
// Angular is synced up.
var originalFns = {};
WEB_ELEMENT_FUNCTIONS.forEach(function(name) {
originalFns[name] = element[name];
element[name] = function() {
thisPtor.waitForAngular();
return originalFns[name].apply(element, arguments);
};
});
var originalFindElement = element.findElement;
var originalFindElements = element.findElements;
var originalIsElementPresent = element.isElementPresent;
/**
* Shortcut for querying the document directly with css.
*
* @alias $(cssSelector)
* @view
* <div class="count">
* <span class="one">First</span>
* <span class="two">Second</span>
* </div>
*
* @example
* var item = $('.count .two');
* expect(item.getText()).toBe('Second');
*
* @param {string} selector A css selector
* @see webdriver.WebElement.findElement
* @return {!webdriver.WebElement}
*/
element.$ = function(selector) {
var locator = protractor.By.css(selector);
return this.findElement(locator);
};
/**
* @see webdriver.WebElement.findElement
* @return {!webdriver.WebElement}
*/
element.findElement = function(locator, varArgs) {
thisPtor.waitForAngular();
var found;
if (locator.findElementsOverride) {
found = thisPtor.findElementsOverrideHelper_(element, locator);
} else {
found = originalFindElement.apply(element, arguments);
}
return thisPtor.wrapWebElement(found);
};
/**
* Shortcut for querying the document directly with css.
*
* @alias $$(cssSelector)
* @view
* <div class="count">
* <span class="one">First</span>
* <span class="two">Second</span>
* </div>
*
* @example
* // The following protractor expressions are equivalent.
* var list = element.all(by.css('.count span'));
* expect(list.count()).toBe(2);
*
* list = $$('.count span');
* expect(list.count()).toBe(2);
* expect(list.get(0).getText()).toBe('First');
* expect(list.get(1).getText()).toBe('Second');
*
* @param {string} selector a css selector
* @see webdriver.WebElement.findElements
* @return {!webdriver.promise.Promise} A promise that will be resolved to an
* array of the located {@link webdriver.WebElement}s.
*/
element.$$ = function(selector) {
var locator = protractor.By.css(selector);
return this.findElements(locator);
};
/**
* @see webdriver.WebElement.findElements
* @return {!webdriver.promise.Promise} A promise that will be resolved to an
* array of the located {@link webdriver.WebElement}s.
*/
element.findElements = function(locator, varArgs) {
thisPtor.waitForAngular();
var found;
if (locator.findElementsOverride) {
found = locator.findElementsOverride(element.getDriver(), element);
} else {
found = originalFindElements.apply(element, arguments);
}
return found.then(function(elems) {
for (var i = 0; i < elems.length; ++i) {
thisPtor.wrapWebElement(elems[i]);
}
return elems;
});
};
/**
* @see webdriver.WebElement.isElementPresent
* @return {!webdriver.promise.Promise} A promise that will be resolved with
* whether an element could be located on the page.
*/
element.isElementPresent = function(locator, varArgs) {
thisPtor.waitForAngular();
if (locator.findElementsOverride) {
return locator.findElementsOverride(element.getDriver(), element).
then(function (arr) {
return !!arr.length;
});
}
return originalIsElementPresent.apply(element, arguments);
};
/**
* Evaluates the input as if it were on the scope of the current element.
* @param {string} expression
*
* @return {!webdriver.promise.Promise} A promise that will resolve to the
* evaluated expression. The result will be resolved as in
* {@link webdriver.WebDriver.executeScript}. In summary - primitives will
* be resolved as is, functions will be converted to string, and elements
* will be returned as a WebElement.
*/
element.evaluate = function(expression) {
thisPtor.waitForAngular();
return element.getDriver().executeScript(clientSideScripts.evaluate,
element, expression);
};
return element;
};
/**
* Waits for Angular to finish rendering before searching for elements.
* @see webdriver.WebDriver.findElement
* @return {!webdriver.WebElement}
*/
Protractor.prototype.findElement = function(locator, varArgs) {
var found;
this.waitForAngular();
if (locator.findElementsOverride) {
found = this.findElementsOverrideHelper_(null, locator);
} else {
found = this.driver.findElement(locator, varArgs);
}
return this.wrapWebElement(found);
};
/**
* Waits for Angular to finish rendering before searching for elements.
* @see webdriver.WebDriver.findElements
* @return {!webdriver.promise.Promise} A promise that will be resolved to an
* array of the located {@link webdriver.WebElement}s.
*/
Protractor.prototype.findElements = function(locator, varArgs) {
var self = this, found;
this.waitForAngular();
if (locator.findElementsOverride) {
found = locator.findElementsOverride(this.driver);
} else {
found = this.driver.findElements(locator, varArgs);
}
return found.then(function(elems) {
for (var i = 0; i < elems.length; ++i) {
self.wrapWebElement(elems[i]);
}
return elems;
});
};
/**
* Tests if an element is present on the page.
* @see webdriver.WebDriver.isElementPresent
* @return {!webdriver.promise.Promise} A promise that will resolve to whether
* the element is present on the page.
*/
Protractor.prototype.isElementPresent = function(locatorOrElement, varArgs) {
this.waitForAngular();
if (locatorOrElement.findElementsOverride) {
return locatorOrElement.findElementsOverride(this.driver).then(function(arr) {
return !!arr.length;
});
}
return this.driver.isElementPresent(locatorOrElement, varArgs);
};
/**
* Add a module to load before Angular whenever Protractor.get is called.
* Modules will be registered after existing modules already on the page,
* so any module registered here will override preexisting modules with the same
* name.
*
* @param {!string} name The name of the module to load or override.
* @param {!string|Function} script The JavaScript to load the module.
*/
Protractor.prototype.addMockModule = function(name, script) {
this.moduleNames_.push(name);
this.moduleScripts_.push(script);
};
/**
* Clear the list of registered mock modules.
*/
Protractor.prototype.clearMockModules = function() {
this.moduleNames_ = [];
this.moduleScripts_ = [];
};
/**
* Remove a registered mock module.
* @param {!string} name The name of the module to remove.
*/
Protractor.prototype.removeMockModule = function (name) {
var index = this.moduleNames_.indexOf(name);
this.moduleNames_.splice(index, 1);
this.moduleScripts_.splice(index, 1);
};
/**
* See webdriver.WebDriver.get
*
* Navigate to the given destination and loads mock modules before
* Angular. Assumes that the page being loaded uses Angular.
* If you need to access a page which does have Angular on load, use
* the wrapped webdriver directly.
*
* @param {string} destination Destination URL.
* @param {number=} opt_timeout Number of seconds to wait for Angular to start.
*/
Protractor.prototype.get = function(destination, opt_timeout) {
var timeout = opt_timeout || 10;
var self = this;
destination = url.resolve(this.baseUrl, destination);
if (this.ignoreSynchronization) {
return this.driver.get(destination);
}
this.driver.get('about:blank');
this.driver.executeScript(
'window.name = "' + DEFER_LABEL + '" + window.name;' +
'window.location.assign("' + destination + '");');
// At this point, we need to make sure the new url has loaded before
// we try to execute any asynchronous scripts.
this.driver.wait(function() {
return self.driver.executeScript('return window.location.href;').then(function(url) {
return url !== 'about:blank';
});
}, timeout * 1000, 'Timed out waiting for page to load');
var assertAngularOnPage = function(arr) {
var hasAngular = arr[0];
if (!hasAngular) {
var message = arr[1];
throw new Error('Angular could not be found on the page ' +
destination + " : " + message);
}
};
// Make sure the page is an Angular page.
self.driver.executeAsyncScript(clientSideScripts.testForAngular, timeout).
then(assertAngularOnPage, function(err) {
throw 'Error while running testForAngular: ' + err.message;
});
// At this point, Angular will pause for us, until angular.resumeBootstrap
// is called.
for (var i = 0; i < this.moduleScripts_.length; ++i) {
name = this.moduleNames_[i];
this.driver.executeScript(this.moduleScripts_[i]).
then(null, function(err) {
throw 'Error wile running module script ' + name +
': ' + err.message;
});
}
return this.driver.executeScript(function() {
/* Continue to bootstrap Angular. */
angular.resumeBootstrap(arguments[0]);
}, this.moduleNames_);
};
/**
* Returns the current absolute url from AngularJS.
*/
Protractor.prototype.getLocationAbsUrl = function() {
this.waitForAngular();
return this.driver.executeScript(clientSideScripts.getLocationAbsUrl, this.rootEl);
};
/**
* Pauses the test and injects some helper functions into the browser, so that
* debugging may be done in the browser console.
*
* This should be used under node in debug mode, i.e. with
* protractor debug <configuration.js>
*
* While in the debugger, commands can be scheduled through webdriver by
* entering the repl:
* debug> repl
* Press Ctrl + C to leave rdebug repl
* > ptor.findElement(protractor.By.input('user').sendKeys('Laura'));
* > ptor.debugger();
* debug> c
*
* This will run the sendKeys command as the next task, then re-enter the
* debugger.
*/
Protractor.prototype.debugger = function() {
var clientSideScriptsList = [];
for (var script in clientSideScripts) {
clientSideScriptsList.push(
script + ': ' + clientSideScripts[script].toString());
}
this.driver.executeScript(
'window.clientSideScripts = {' + clientSideScriptsList.join(', ') + '}');
var flow = webdriver.promise.controlFlow();
flow.execute(function() {
debugger;
}, 'add breakpoint to control flow');
};
/**
* Beta (unstable) pause function for debugging webdriver tests. Use
* browser.pause() in your test to enter the protractor debugger from that
* point in the control flow.
* Does not require changes to the command line (no need to add 'debug').
*/
Protractor.prototype.pause = function() {
// Patch in a function to help us visualize what's going on in the control
// flow.
webdriver.promise.ControlFlow.prototype.getControlFlowText = function() {
var descriptions = [];
var getDescriptions = function(frameOrTask, descriptions) {
if (frameOrTask.getDescription) {
var getRelevantStack = function(stack) {
return stack.filter(function(line) {
var include = true;
for (var i = 0; i < STACK_SUBSTRINGS_TO_FILTER.length; ++i) {
if (line.toString().indexOf(STACK_SUBSTRINGS_TO_FILTER[i]) !==
-1) {
include = false;
}
}
return include;
});
};
descriptions.push({
description: frameOrTask.getDescription(),
stack: getRelevantStack(frameOrTask.snapshot_.getStacktrace())
});
} else {
for (var i = 0; i < frameOrTask.children_.length; ++i) {
getDescriptions(frameOrTask.children_[i], descriptions);
}
}
};
if (this.history_.length) {
getDescriptions(this.history_[this.history_.length - 1], descriptions);
}
if (this.activeFrame_.getPendingTask()) {
getDescriptions(this.activeFrame_.getPendingTask(), descriptions);
}
getDescriptions(this.activeFrame_.getRoot(), descriptions);
var asString = '-- WebDriver control flow schedule \n';
for (var i = 0; i < descriptions.length; ++i) {
asString += ' |- ' + descriptions[i].description;
if (descriptions[i].stack.length) {
asString += '\n |---' + descriptions[i].stack.join('\n |---');
}
if (!(i == descriptions.length - 1)) {
asString += '\n';
}