2016-09-08 2 views
1

저는 각도기에서 브라우저 메모리 값을 읽고 전역 객체에 저장하려고합니다. 이를 위해 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); 
}; 

이 반환 console.log('Variable outside the promise: ' + HeapSizeLimit);HeapSizeLimit = memoryValues.jsHeapSizeLimit; 전에 실행

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

당신은 확실히'then' 함수에서 약속 밖의 값에 할당 할 수 있지만'then' 함수가 실제로 실행 된 후에 시간순으로 설정할 수는 없습니다. – apsillers

+0

감사합니다. @apsillers. 이 설명은 문제가 무엇인지 이해하는 데 매우 유용했습니다. – Tedi

답변

4

때문입니다. 그것이 약속 이후의 라인에 있다면, 실행 순서가 동일하다는 것을 의미하지는 않습니다. 테스트에서

+0

문제를 이해해 주셔서 감사합니다. – Tedi

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

:

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

워스 아무것도 : 당신의 변수를 설정하고 값을 검색하는 (각도기 테스트 내 연기 실행을 통해) 비동기 (controlFlow 외부) 동 기적으로 발생하거나 나타날 수 있습니다.

+0

이전 값과 비교할 때 사용할 수있는 전역 변수로 필요합니다. – Tedi

+0

이 솔루션을 사용해 보았지만 '0이 0보다 커야합니다.' – Tedi

관련 문제