2017-02-09 1 views
0

protracor의 초보자는 조금 있지만 기본적으로 확인하려면 (다른 함수의) 체크를 수행 한 다음 작업을 수행 한 다음 (이전과 같은 함수에서) 동일한 검사를 수행하십시오.두 함수의 결과를 비교하십시오

나는 아래 시도했지만 unforunately 어떤 도움이 대규모 감사하겠습니다 Failed: first is not defined

checkCanvasWidth: { 
    value: function() { 
     return element(by.css("div[class='canvasWrapper'] > canvas")).getAttribute("width").then(function(width) { 
      return width; 
     }); 
    } 
}, 


zoomIn: { 
    value: function() { 
     this.checkCanvasWidth().then(function (width) { 
      var first = width; 
      console.log("before:" + width); 
     }); 

     //performs a click 
     this.clickZoomIn(); 

     this.checkCanvasWidth().then(function (width) { 
      var second = width; 
      console.log("after:" + width); 
     }); 

     expect(first).toBeGreaterThan(second); 
    } 
} 

얻을 수있다!

답변

0

this.checkCanvasWidth() 외부에서 첫 번째와 두 번째를 정의하십시오. 함수는 스코프를 생성하므로 checkCanvasWidth와 함께 사용하는 함수 만 첫 번째와 두 번째에 각각 액세스 할 수 있습니다. expect 함수가 이러한 변수를 볼 수 있도록이 함수의 범위에서 정의해야합니다.

zoomIn: { 
    value: function() { 
     var first, 
      second; 
     this.checkCanvasWidth().then(function (width) { 
      first = width; 
      console.log("before:" + width); 
     }); 

     //performs a click 
     this.clickZoomIn(); 

     this.checkCanvasWidth().then(function (width) { 
      second = width; 
      console.log("after:" + width); 
     }); 

     expect(first).toBeGreaterThan(second); 
    } 
} 

PS : checkCanvasWidth()는 약속을 반환하는 경우 제 1 및 제 2 ahve를 설정 한 후 expect() 전화를하고 싶어하기 때문에, 당신은이 모든 기능을 다시 작성해야합니다.

약속 버전 :

zoomIn: { 
    value: function() { 
     var first, 
      second; 
     this.checkCanvasWidth().then(function (width) { 
      first = width; 
      console.log("before:" + width); 
      if (first && second) { 
       expect(first).toBeGreaterThan(second); 
      } 
     }); 

     //performs a click 
     this.clickZoomIn(); 

     this.checkCanvasWidth().then(function (width) { 
      second = width; 
      if (first && second) { 
       expect(first).toBeGreaterThan(second); 
      } 
     }); 
    } 
} 
+0

감사합니다. Shilly, 나는 약속의 버전이 필요했습니다! 이것은 내가 고군분투하고 있었지만 깨닫지 못했던 것이다. –

0

당신은 함수의 변수 firstsecond 사용할 수 있도록해야합니다. 자바는 기능 범위가이므로 firstsecond을 정의한 방식대로 함수 외부에서 액세스 할 수 없습니다.

그래서 아래와 같은 코드를 작성하면 변수 초만 익명 함수에 액세스 할 수 있습니다. 그들에 액세스 할 수 있도록

this.checkCanvasWidth().then(function (width) { 
      var second = width; 
      console.log("after:" + width); 
     }); 

그래서 당신은, 외부 변수 firstsecond을 선언 할 수 있습니다 다음 값을 설정하기 위해 다음 핸들러 내부의 값을 설정합니다.

zoomIn: { 
    value: function() { 
     var first ; 
     var second ; 
     this.checkCanvasWidth().then(function (width) { 
      first = width 
      console.log("before:" + width); 
     }); 

     //performs a click 
     this.clickZoomIn(); 

     this.checkCanvasWidth().then(function (width) { 
      second = width 
      console.log("after:" + width); 
     }); 

     expect(first).toBeGreaterThan(second); 
    } 
}