Machtyn Machtyn - 2 years ago 128
Javascript Question

how to use .each or .map to create multiple object in protractor

I'm trying to create an array of a custom object that requires an ElementFinder as a parameter.

The custom object, StatusObj, takes an ElementFinder object.

The code I'm trying to do create my objects and return that array looks like this:

function getStatusObjs() {
var arr;

arr = $$('li').each(function(el) {
return new StatusObj(el);

return arr;

This doesn't work. It locks up and times out or runs out of space. I've tried '.map()' and I've tried putting
arr = new StatusObj(el);
but, of course, arr isn't in scope (for lack of a better term) within the .each function.

Update for Alexce:

Here is the stack trace I get when I try your solution:

<--- Last few GCs --->

118309 ms: Mark-sweep 1254.4 (1434.1) -> 1239.4 (1434.1) MB, 2124.7 / 0 ms [allocation failure] [GC in old space requested].
120614 ms: Mark-sweep 1239.4 (1434.1) -> 1239.4 (1434.1) MB, 2305.0 / 0 ms [allocation failure] [GC in old space requested].
122702 ms: Mark-sweep 1239.4 (1434.1) -> 1239.4 (1434.1) MB, 2088.9 / 0 ms [last resort gc].
124818 ms: Mark-sweep 1239.4 (1434.1) -> 1239.4 (1434.1) MB, 2115.1 / 0 ms [last resort gc].

<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x30c99fec9fa9 <JS Object>
1: get stack [native messages.js:595] [pc=0x32edf1d73957] (this=0x82e2251cb1 <a RangeError with map 0x206aaf5b8ae9>)
3: resolve_ [/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1115] [pc=0x32edf1dfdbe5] (this=0x82e2247b99 <a Promise with map 0x206aaf58ff81>,newState=0x29909e1edd89 <String[8]: rejected>,newValue=0x82e2251cb1 <a RangeError with map 0...

FAbort trap: 6

Is this a problem with my object creation code or that I'm not handling the promise correctly?

Answer Source

You need the map():

function getStatusObjs() {
    return $$('li').map(function(el) {
        return new StatusObj(el);

Now, the result of getStatusObjs() call would be a promise that would be resolved into an array of StatusObj objects:

getStatusObjs().then(function (objs) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download