2015-12-21 70 views
1

我需要使用一個變量,其值是基於CSS樣式像素確定的。 測試找到左側像素的值,然後選擇特定的單元格。但是當我運行這個測試時,這個值總是爲0而不是實際應該是的。在實習生功能測試中使用變量

'Test' : function() { 
      var left = 0; 
      var remote = this.remote; 
      return remote 
      .setFindTimeout(5000) 

      .findByXpath("//div[@class = 'grid']//div[@class = 'gridCell' and position() = 1]/div[3]") 
       .getAttribute("style") 
       .then(function(width) { 
        left = parseInt(width.substring(width.indexOf("left")+6,width.indexOf("width")-4)); 
       }).end() 
      .f_selectCell("", 0, left)    
     }, 

回答

1

儘管命令鏈中的調用將按順序執行,但是在執行開始之前,鏈表達式本身會被解析並且參數被解析。所以在

return remote 
    .findByXpath('...') 
    .getAttribute('style') 
    .then(function (width) { 
     left = parseInt(width); 
    }) 
    .f_selectCell('', 0, left); 

的情況下,鏈不斷開始執行前left參數f_selectCell進行評估。當leftthen回調被重新分配,f_selectCell是不會知道的,因爲它已經評價left爲0

相反,你需要調用一個then回調f_selectCell方法,或通過它是屬性可以分配給的object

return remote 
    // ... 
    .then(function (width) { 
     left = parseInt(width); 
    }) 
    .then(function() { 
     // I'm not entirely sure where f_selectCell is coming from... 
     return f_selectCell('', 0, left); 
    }); 

// Put all args to selectCell in this 
var selectData = {}; 

return remote 
    // ... 
    .then(function (width) { 
     selectData.left = parseInt(width); 
    }) 
    // selectCell now takes an object with all args 
    // The object is never reassigned during execution. 
    .f_selectCell(selectData);