好吧,我一起砍了一些東西。我使用browser.execute(function, params, callback)
function waitForElement_NoPassFail(selector, timeout){
browser.execute(function(selector, timeout, done){
let intervalId;
var p1 = new Promise(function(resolve, reject){
intervalId = setInterval(function(){
let itemArray = document.querySelectorAll(selector);
if(itemArray.length == 1){
clearInterval(intervalId);
resolve(true); //element found
} else if(itemArray.length>1){
reject(false); //too many elements found, because of ambiguous selector
}
}, 100);
});
var p2 = new Promise(function(resolve, reject){
setTimeout(reject,timeout, false); //timeout reached
});
return Promise.race([p1, p2]).then(function(result){
done(result);
});
},
[selector, timeout],
function(result){
if(!result){
throw "Element: " + selector + " wasn't found after " + timout + " ms.";
} else {
console.log("Element found within timeout limits.") //doesn't trigger assert=ok
}
});
};
waitForElement_NoPassFail(
"cssSelector_that_Is_Valid_after_AjaxIsComplete",
10000 //timeout
);
這可以通過各種方式進行擴展,例如支持XPATH基本上發送的JavaScript控制檯中的硒。您可以在等待時使用夜間全局變量進行元素檢查頻率。如果您需要幫助,請寫評論。
事實證明,使用[nightwatch-cucumber](https://github.com/mucsi96/nightwatch-cucumber)爲我提供了使夜間測試報告更合適的必要包裝。我建議使用這個庫進行測試。 –