From bc2c4f8766f493bb412f884969bafc84f906a417 Mon Sep 17 00:00:00 2001 From: andresdom Date: Wed, 22 Jan 2014 13:45:24 -0500 Subject: [PATCH 1/3] feat(locators) Improve the map() function in element.all to resolve multiple promises Resolve promises if there is an object that contains multiple promises. Added index as a second argument to the map function callback. Closes #392 --- spec/basic/findelements_spec.js | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/spec/basic/findelements_spec.js b/spec/basic/findelements_spec.js index e966d833e..f8caac61a 100644 --- a/spec/basic/findelements_spec.js +++ b/spec/basic/findelements_spec.js @@ -415,13 +415,50 @@ describe('global element function', function() { }); it('should map each element on array and with promises', function() { + var labels = element.all(by.css('.menu li a')).map(function(elm, index) { + return { + index: index, + text: elm.getText() + }; + }); + browser.get('index.html#/form'); + + expect(labels).toEqual([ + {index: 0, text: 'repeater'}, + {index: 1, text: 'bindings'}, + {index: 2, text: 'form'}, + {index: 3, text: 'async'}, + {index: 4, text: 'conflict'}, + {index: 5, text: 'polling'} + ]); + }); + + it('should map and resolve multiple promises', function() { var labels = element.all(by.css('.menu li a')).map(function(elm) { - return elm.getText(); + return { + text: elm.getText(), + inner: elm.getInnerHtml(), + outer: elm.getOuterHtml() + }; }); browser.get('index.html#/form'); - expect(labels).toEqual( - ['repeater', 'bindings', 'form', 'async', 'conflict', 'polling']); + var newExpected = function(expectedLabel) { + return { + text: expectedLabel, + inner: expectedLabel, + outer: '' + expectedLabel + '' + }; + }; + + expect(labels).toEqual([ + newExpected('repeater'), + newExpected('bindings'), + newExpected('form'), + newExpected('async'), + newExpected('conflict'), + newExpected('polling') + ]); }); it('should map each element from a literal and promise array', function() { From b147a48be47a2d6e35c00f7f732b35bbe897a1bf Mon Sep 17 00:00:00 2001 From: andresdom Date: Wed, 22 Jan 2014 14:06:43 -0500 Subject: [PATCH 2/3] feat(locators) Improve the map() function in element.all to resolve multiple promises Resolve promises if there is an object that contains multiple promises. Added index as a second argument to the map function callback. Closes #392 --- lib/protractor.js | 48 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/lib/protractor.js b/lib/protractor.js index b681424fc..354000d59 100644 --- a/lib/protractor.js +++ b/lib/protractor.js @@ -139,26 +139,50 @@ var buildElementHelper = function(ptor) { }); }; - elementArrayFinder.map = function(fn) { + /** + * 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. + * + * Usage: + * + * + * var items = element.all(by.css('.menu li')).map(function(elm, index) { + * return { + * index: index, + * text: elm.getText(), + * class: elm.getAttribute('class') + * }; + * }); + * expect(items).toEqual([ + * {index: 0, text: '1', class: 'one'}, + * {index: 0, text: '1', class: 'one'}, + * ]); + * + * @param {function(webdriver.WebElement, number)} mapFn Map function that + * will be applied to each element. + * @return {!webdriver.promise.Promise} A promise with an array of values + * returned by the map function. + */ + elementArrayFinder.map = function(mapFn) { return ptor.findElements(locator).then(function(arr) { var list = []; - arr.forEach(function(webElem) { - var mapResult = fn(webElem); - // Is the result a promise? - if (mapResult.then) { - mapResult.then(function(value) { - list.push(value); - }); - } else { - list.push(mapResult); - } + 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; }; From a5e8e238e53a1fa3479b804a9c34fe12c78d1ae6 Mon Sep 17 00:00:00 2001 From: andresdom Date: Wed, 22 Jan 2014 14:23:44 -0500 Subject: [PATCH 3/3] feat(locators) Improve the map() function in element.all to resolve multiple promises Resolve promises if there is an object that contains multiple promises. Added index as a second argument to the map function callback. Closes #392 --- lib/protractor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/protractor.js b/lib/protractor.js index 354000d59..115767bd4 100644 --- a/lib/protractor.js +++ b/lib/protractor.js @@ -164,8 +164,8 @@ var buildElementHelper = function(ptor) { * * @param {function(webdriver.WebElement, number)} mapFn Map function that * will be applied to each element. - * @return {!webdriver.promise.Promise} A promise with an array of values - * returned by the map function. + * @return {!webdriver.promise.Promise} A promise that resolves to an array + * of values returned by the map function. */ elementArrayFinder.map = function(mapFn) { return ptor.findElements(locator).then(function(arr) {