【发布时间】:2017-01-16 18:40:10
【问题描述】:
我正在尝试从 Protractor 读取浏览器内存值并将它们存储在全局对象中。为此,我获取了 window.performance.memory 对象,然后解决了检查每个内存值的承诺。
问题是我似乎无法将值分配给全局变量。我试过下面的代码,似乎效果不太好:
this.measureMemory = function () {
var HeapSizeLimit;
browser.driver.executeScript(function () {
return window.performance.memory;
}).then(function (memoryValues) {
HeapSizeLimit = memoryValues.jsHeapSizeLimit;
console.log('Variable within the promise: ' + HeapSizeLimit);
});
console.log('Variable outside the promise: ' + HeapSizeLimit);
};
这会返回:
Variable outside the promise: undefined
Variable within the promise: 750780416
【问题讨论】:
-
你当然可以在
then函数中赋值给promise之外的值,但是你不能设置它直到按时间顺序之后then函数实际运行。 -
谢谢@apsillers。这种解释对于理解问题所在非常有用。
标签: javascript angularjs google-chrome protractor