使用實習生編寫一系列功能測試,我試圖使用之前和之後的方法來清除所有的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小時,在這段代碼盯着後,我瞎不管它是什麼。