我正在尝试创建一个需要 ElementFinder 作为参数的自定义对象数组。
自定义对象 StatusObj 采用 ElementFinder 对象。
我尝试创建我的对象并返回该数组的代码如下所示:
function getStatusObjs() {
var arr;
arr = $$('li').each(function(el) {
return new StatusObj(el);
}
return arr;
}
这行不通。它会锁定并超时或空间不足。我试过 '.map()' 并且我试过放置 arr = new StatusObj(el);
但是,当然,arr 不在范围内(因为缺少更好的术语)在 .each 函数中。
Alexce 更新:
这是我在尝试您的解决方案时得到的堆栈跟踪:
<--- 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
这是我的对象创建代码的问题还是我没有正确处理 promise ?
最佳答案
您需要 map()
:
function getStatusObjs() {
return $$('li').map(function(el) {
return new StatusObj(el);
});
}
现在,getStatusObjs()
调用的结果将是一个 promise,它将被解析为一个 StatusObj
对象数组:
getStatusObjs().then(function (objs) {
console.log(objs);
});
关于javascript - 如何使用 .each 或 .map 在 Protractor 中创建多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494509/