Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 092fe1f

Browse files
andresdominguezjuliemr
authored andcommittedJan 10, 2014
feat(locators) Add map() function to element.all
Added a map function to element.all to apply a function to each element and return the result of the transformation. Closes #392
1 parent 5e07295 commit 092fe1f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
 

‎lib/protractor.js

+18
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ var buildElementHelper = function(ptor) {
139139
});
140140
};
141141

142+
elementArrayFinder.map = function(fn) {
143+
return ptor.findElements(locator).then(function(arr) {
144+
var list = [];
145+
arr.forEach(function(webElem) {
146+
var mapResult = fn(webElem);
147+
// Is the result a promise?
148+
if (mapResult.then) {
149+
mapResult.then(function(value) {
150+
list.push(value);
151+
});
152+
} else {
153+
list.push(mapResult);
154+
}
155+
});
156+
return list;
157+
});
158+
};
159+
142160
return elementArrayFinder;
143161
}
144162

‎spec/basic/findelements_spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,26 @@ describe('global element function', function() {
414414
});
415415
});
416416

417+
it('should map each element on array and with promises', function() {
418+
var labels = element.all(by.css('.menu li a')).map(function(elm) {
419+
return elm.getText();
420+
});
421+
browser.get('index.html#/form');
422+
423+
expect(labels).toEqual(
424+
['repeater', 'bindings', 'form', 'async', 'conflict', 'polling']);
425+
});
426+
427+
it('should map each element from a literal and promise array', function() {
428+
var i = 1;
429+
var labels = element.all(by.css('.menu li a')).map(function(elm) {
430+
return i++;
431+
});
432+
browser.get('index.html#/form');
433+
434+
expect(labels).toEqual([1, 2, 3, 4, 5, 6]);
435+
});
436+
417437
it('should export an isPresent helper', function() {
418438
expect(element(by.binding('greet')).isPresent()).toBe(true);
419439
expect(element(by.binding('nopenopenope')).isPresent()).toBe(false);

0 commit comments

Comments
 (0)
This repository has been archived.