守夜人:等待没有 pass/fail 结果的元素
Nightwatch: wait for element without pass/fail result
相关问题:
- wait for element present without error
- Nightwatchjs: how to check if element exists without creating an error/failure/exception
- Selenium WebDriver : Wait for complex page with JavaScript(JS) to load
每当我加载网页时,我都需要等待 ajax 调用完成。我目前这样做:
browser.waitForElementNotPresent(
"cssSelector_For_HTML_afterAJAXComplete",
10000 //timeout
);
不过,这为我的测试添加了一个额外的断言,并使报告的测试计数人为地大于必要值。
有什么想法吗?
好的,我一起破解了一些东西。我基本上是使用 browser.execute(function, params, callback)
将 javascript 发送到 selenium 中的控制台
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。您可以在等待时使用 nightwatch 全局变量来检查元素频率。如果您需要帮助,请发表评论。
相关问题:
- wait for element present without error
- Nightwatchjs: how to check if element exists without creating an error/failure/exception
- Selenium WebDriver : Wait for complex page with JavaScript(JS) to load
每当我加载网页时,我都需要等待 ajax 调用完成。我目前这样做:
browser.waitForElementNotPresent(
"cssSelector_For_HTML_afterAJAXComplete",
10000 //timeout
);
不过,这为我的测试添加了一个额外的断言,并使报告的测试计数人为地大于必要值。
有什么想法吗?
好的,我一起破解了一些东西。我基本上是使用 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。您可以在等待时使用 nightwatch 全局变量来检查元素频率。如果您需要帮助,请发表评论。