简体   繁体   English

将从诺言返回的值分配给全局变量

[英]Assign a value returned from a promise to a global variable

I'm trying to read the browser memory values from Protractor and store them in a global object. 我正在尝试从量角器读取浏览器内存值,并将其存储在全局对象中。 To do that I'm getting the window.performance.memory object and then resolving the promise to inspect each of the memory values. 为此,我要获取window.performance.memory对象,然后解决对每个内存值进行检查的承诺。

The problem is that I cannot seem to be able to assign the value to a global variable. 问题是我似乎无法将值分配给全局变量。 I've tried the following code, which doesn't seem to work quite well: 我已经尝试了以下代码,但似乎效果不佳:

 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);
};

This returns: 返回:

   Variable outside the promise: undefined
   Variable within the promise: 750780416

Because console.log('Variable outside the promise: ' + HeapSizeLimit); 因为console.log('Variable outside the promise: ' + HeapSizeLimit); is executed before HeapSizeLimit = memoryValues.jsHeapSizeLimit; HeapSizeLimit = memoryValues.jsHeapSizeLimit;之前执行 . If it's on the line after the promise, doesn't mean the execute order is the same. 如果在约定之后就行了,并不意味着执行顺序是相同的。

// a variable to hold a value
var heapSize;

// a promise that will assign a value to the variable
// within the context of the protractor controlFlow
var measureMemory = function() {
    browser.controlFlow().execute(function() {
        browser.driver.executeScript(function() {
            heapSize = window.performance.memory.jsHeapSizeLimit;
        });
    });
};

// a promise that will retrieve the value of the variable
// within the context of the controlFlow
var getStoredHeapSize = function() {
    return browser.controlFlow().execute(function() {
        return heapSize;
    });
};

In your test: 在您的测试中:

it('should measure the memory and use the value', function() {
    // variable is not yet defined
    expect(heapSize).toBe(undefined);
    // this is deferred
    expect(getStoredHeapSize).toBe(0);

    // assign the variable outside the controlFlow
    heapSize = 0;
    expect(heapSize).toBe(0);
    expect(getStoredHeapSize).toBe(0);

    // assign the variable within the controlFlow
    measureMemory();

    // this executes immediately
    expect(heapSize).toBe(0);
    // this is deferred
    expect(getStoredHeapSize).toBeGreaterThan(0);
};

Worth nothing: setting your variable and retrieving the value can appear to happen synchronously (outside controlFlow) or asynchronously (via the deferred executions within protractor tests). 毫无价值:设置变量和获取值似乎是同步发生(在controlFlow外部)或异步发生(通过量角器测试中的延迟执行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM