2017-02-09 98 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的函数才能分别访问第一个和第二个。您必须在这些函数上方的范围中定义它们,以便期望函数也可以看到这些变量。

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()返回一个承诺,你就必须重写这个整体功能,因为要在其后的第一和第二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可用的功能。 Javascript has function scope,所以你定义的方式firstsecond它们不能在函数外部访问。

因此,当你编写下面的代码时,变量second只能被匿名函数访问。

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