我必须使用Selenium测试应用程序。该应用程序包含广告等外部内容。在我的测试中,我有几次等待文档加载。这看起来像这样:
private static final String DOCUMENT_READY_STATE_COMPLETE = "complete";
protected void waitUntilDocumentLoaded() {
wait.until(input -> getDocumentReadyState().equals(DOCUMENT_READY_STATE_COMPLETE));
}
private String getDocumentReadyState() {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString();
}
有时,浏览器仍在加载某些资源。据我所知,如果readyState 互动 而不是 完整 ,我有时可以接受。例如,如果某些广告没有及时加载,我的测试就完全没有意义了。
是否有可能以某种方式获取尚未加载的URL的资源列表?然后我可以构建一个断言,检查哪些资源对于测试是必需的,哪些是不
我在Linux下使用Selenium Java WebDriver 2.53.1和Firefox 46。
答案 0 :(得分:2)
我可以分享3种可以检查元素是否存在的方法。希望通过使用这种方法,你可以得到平息。
[DateTime]$req.ServicePoint.Certificate.GetExpirationDateString()
/**
* Checks if is element present or not.
*
* @param element
* @return true, if is element present, otherwise return false.
* @throws Exception
*/
public boolean isElementPresent(WebElement element) throws Exception {
try {
waitForElement(element, Integer.valueOf(PropertyLoader
.loadProperty("implicit_timeout_second")));
return true;
} catch (NoSuchElementException e) {
return false;
}
}
/**
* waits for element to be present.
*
* @param element
* @param timeOut
* The timeout in seconds when an expectation is called
* @return
*/
public WebElement waitForElement(WebElement element, int timeOut) {
WebDriverWait wait = new WebDriverWait(driverW, timeOut);
wait.until(ExpectedConditions.elementToBeClickable(element));
return element;
}
答案 1 :(得分:1)
答案 2 :(得分:1)
所以这就是我自己编造的。这可以检测未加载的图像。我使用了非常酷的网络服务http://www.deelay.me和https://placekitten.com。所以至少对于图像我有一些东西。
<html>
<body>
<img src="http://deelay.me/1000/https://placekitten.com/g/500/500"/>
<img src="http://deelay.me/10000/https://placekitten.com/g/500/501"/>
<script>
function isImageOk(img) {
//Function taken from http://stackoverflow.com/a/1977898/337621
// During the onload event, IE correctly identifies any images that
// weren’t downloaded as not complete. Others should too. Gecko-based
// browsers act like NS4 in that they report this incorrectly.
if (!img.complete) {
return false;
}
// However, they do have two very useful properties: naturalWidth and
// naturalHeight. These give the true size of the image. If it failed
// to load, either of these should be zero.
if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) {
return false;
}
// No other way of checking: assume it’s ok.
return true;
}
function checkState(){
var documentState = document.readyState;
if ( documentState != "complete") {
console.log("Document is still having readystate " + documentState + ". Pending images: " );
var images = document.getElementsByTagName("img");
for(i = 0;i < images.length; i++)
{
if ( !isImageOk( images[i] ) ) {
console.log("Image URL: " + images[i].src );
}
}
setTimeout(checkState,500);
}
else {
console.log("Document is loaded successfully.");
}
}
checkState();
</script>
</body>
</html>
控制台输出:
Document is still having readystate loading. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is loaded successfully.