使用Maestro脚本报错:Cannot modify a property of a sealed object: arr
解决Maestro脚本中"Cannot modify a property of a sealed object: arr"错误
错误的核心原因是你在readPro函数中没有使用let/const声明arr、o、filtered这些变量,导致它们被挂载到全局对象上。而Maestro的Rhino运行环境中,全局对象是**密封(sealed)**的,不允许添加或修改其属性,因此触发了这个异常。
修复方案
在函数内部对所有变量添加let或const声明,将它们转为局部变量,避免污染全局作用域。
修改后的fetch.js代码
const response = http.get('http://localhost:9999/api/device-screen') const elements = json(response.body).elements function readPro(json, replaceText, props, filter, resorceIdFinal){ let arr = [], o = {} json.forEach( el => { const resorceId = el.resourceId.replace(replaceText, '') if(props.includes(resorceId)){ o[resorceId] = el.text if(resorceId === resorceIdFinal){ arr.push(o) o = {} } } } ) const filtered = arr.reverse().filter( (s => o => (k => !s.has(k) && s.add(k)) (filter.map(k => o[k]).join('|')) ) (new Set) ) return filtered } const res = readPro( elements, 'com.shazam.android:id/', ['title', 'subtitle', 'datetime'], ['title', 'subtitle'], 'datetime' ) output.result = res
额外说明:resorceId和res变量也补充了声明,养成显式声明变量的习惯可以避免更多作用域相关的潜在问题。
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

