gpt4 book ai didi

javascript - 找出哪些资源未使用 Selenium 成功加载

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:52:49 25 4
gpt4 key购买 nike

我必须使用 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。

最佳答案

这就是我自己编造的。这可以检测未加载的图像。我使用了非常酷的网络服务 http://www.deelay.mehttps://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.

关于javascript - 找出哪些资源未使用 Selenium 成功加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38477116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com