2016-03-10 35 views
0

使用實習生編寫一系列功能測試,我試圖使用之前和之後的方法來清除所有的Cookie和本地存儲的數據,以便每個測試都開始乾淨。實習生功能測試和清理之前和之後的Cookie

這裏的cleanEnvironment功能我想使用,位於一個模塊在一個名爲utils

cleanEnvironment: function(name) { 

    name = name || 'NONE'; 

    // window params 
    var defaultHeight = 768; 
    var defaultWidth = 1024; 

    if (this.remote.environmentType.webStorageEnabled === true) { 
     this.remote 
      .clearSessionStorage() 
      .clearLocalStorage(); 
    } 

    return this.remote 
     .setWindowSize(defaultWidth, defaultHeight) 
     .getCookies() 
     .then(
      function(cookies) { 
       console.log('in ', name); 
       console.log('Existing cookies that will be cleared are ', cookies); 
      } 
     ) 
     .clearCookies() 
     .then(
      function(cookies) { 
       console.log('in ', name); 
       console.log('Existing cookies are ', cookies); 
      } 
     ); 

}, 

和這裏的如何,我想它調用的前/後置方法:

after: function() { 
    console.log('timestamp for login after start is ', Date.now()); 
    utils.cleanEnvironment.call(this, 'login before'); 
    console.log('timestamp for login after finish is ', Date.now()); 
}, 

我終於意識到(並且重新閱讀說明這一點的文檔),我無法確定多個套件之前和之後的順序,因爲我沒有在這些模塊中返回一個Promise。但我努力寫一個承諾,允許我使用這個外部模塊utils,所以我不在每個套件中重複代碼。

before/after方法中的承諾會如何成功通過並將正確的引用返回給this.remote?我沒有在這些方法中找到承諾的任何示例,到目前爲止,我要麼在cleanEnvironment函數中發生錯誤,其中this.remote未定義,或者瀏覽器從不加載測試URL,我正在考慮的意思是我從未解決承諾。

這裏是我的嘗試之一:

 after: function() { 
      var self = this; 
      return new Promise(function(resolve, reject) { 
       console.log('timestamp for login after start is ', Date.now()); 
       utils.cleanEnvironment.call(self, 'login before'); 
       console.log('timestamp for login after finish is ', Date.now()); 
       resolve(); 
      }); 
     }, 

我敢肯定,我完全缺少有關承諾的東西很明顯,,而7小時,在這段代碼盯着後,我瞎不管它是什麼。

回答

0

命令類似promise,可用於異步操作,因此您可以在after方法中返回cleanEnvironment函數的結果。

after: function() { 
    return utils.cleanEnvironment(...) 
} 

你還應該注意保持在cleanEnvironment單鏈。在原始cleanEnvironment中,可能會啓動兩個單獨的鏈,並且只返回第二個鏈。如果因爲某種原因第一次比第二次花費更長的時間,Intern不會等待它完成。爲保持連鎖的連續性,請執行如下操作:

var chain = this.remote; 

if (this.remote.environmentType.webStorageEnabled === true) { 
    chain = this.remote 
     .clearSessionStorage() 
     .clearLocalStorage(); 
} 

return chain 
    .setWindowSize(defaultWidth, defaultHeight) 
    .getCookies() 
    // ... 
相關問題